How to Hide API Keys of Your JavaScript Projects?

While developing SPA project, we sometimes need to use API keys. Although it is possible to hide these API keys with .ENV file importing method, you must find another method on production process because of the fact that:

  • .env files’ content are visible to client when they investigate your .js files.
  • .env importing is used for when you want to hide api key on Github 🙂

Quick fix for this problem:

You can try to create a new express.js project to get request from your client then forward this request this to 3rd party endpoint with API key (on backend side).

After it gets response from 3rd party endpoint, your express.js backend project must forward this response to you.

Here you can find my solution and steps:

  1. We integrate express pack (npm install express)
  2. Install helmet pack for possible security problems (npm install helmet)
  3. Use axios for get and post steps (npm install axios)
const express = require("express");
const helmet = require("helmet");
const axios = require("axios");
const path = require('path');
var bodyParser = require("body-parser");

const app = express();


app.get("/api/address", (req, res) => {
  try {
      .then(response => {
      .catch(err => res.status(400).send("Bad Request"));
  } catch (err) {
    res.status(404).send("Backend Error");

// Thanks to @fatihyildizli for security update

const port = process.env.PORT || 4000;
console.log("App is listening on port " + port);

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

One Comment

  1. I was just searching for this information for a while. After 6 hours of continuous Googleing, finally I got it in your website. I wonder what is the lack of Google strategy that don’t rank this kind of informative websites in top of the list. Usually the top sites are full of garbage.