You’ve just built a cool new web app and need to connect it to a service using an API key. Great! But wait, how do you hide that API key in your JavaScript project so everyone doesn’t just… grab it? Keeping your API keys secure is fundamental to good programming practice, and this article will show you exactly how to protect API keys and store API keys safely in your JavaScript applications. We’ll explore using environment variables and the popular dotenv package to hide API keys in a JavaScript project effectively.
Why You Can’t Just Put API Keys Directly in Your Code
Imagine your API key is like a house key. You wouldn’t tape it to your front door for everyone to see, right? Similarly, embedding your API keys directly into your JavaScript code is a massive security risk. If you store them in plain text within your source files, anyone who gets access to your code—whether it’s through a public GitHub repository or by inspecting your deployed website—can easily steal them. This can lead to unauthorized usage, unexpected charges, and compromised services. This is especially true for client-side security where your JavaScript runs directly in the user’s browser.
The Solution: Environment Variables and .env
Files
The most common and recommended way to hide API keys in a JavaScript project is by using environment variables. Think of environment variables as special containers that hold configuration settings for your application. These variables are separate from your actual code files, providing an extra layer of abstraction and security.
For local development, the go-to tool for managing these variables is the dotenv package.
Setting Up .env
with dotenv
Install
dotenv
:
First, you need to adddotenv
to your project’s dependencies. Open your terminal in your project’s root directory and run:npm install dotenv
oryarn add dotenv
Create a
.env
FileIn the root directory of your project (the same folder as your
package.json
file), create a new file named exactly .env. This is where you’ll list your secret keys and other sensitive information, with one variable per line.# .env file<br># This is a comment. Store your keys below.<br><br>API_KEY="your_super_secret_api_key_goes_here"<br>ANOTHER_VARIABLE="some_other_value"<br>
🤫 Analogy: Think of the
.env
file as a secret sticky note that only your application can read. You never share this note with anyone else by committing it to Git.Load and Use Environment Variables
At the very top of your application’s main JavaScript file (e.g.,
index.js
orapp.js
), you need to telldotenv
to load the variables from your.env
file. This makes them available on the built-inprocess.env
object.// index.js or app.js<br><br>// Load environment variables from .env file<br>require('dotenv').config();<br><br>// Now you can access your API key<br>const apiKey = process.env.API_KEY;<br><br>// This will log your secret key<br>console.log('Your API Key is:', apiKey); <br>
Key Concept: The
process.env
object is built into Node.js and holds all the environment variables of the running process. Thedotenv
package reads your.env
file and adds its contents to this object.
What About Client-Side JavaScript?
You might be wondering, “My JavaScript runs in the browser, so how does dotenv
help?” This is a critical distinction. dotenv
is a Node.js package, meaning it runs on the server-side (or during your build process). It loads variables into process.env
before your JavaScript code is bundled and sent to the browser.
-
For Client-Side Apps (React, Vue, etc.)
Many build tools, like Create React App or Vite, allow you to define environment variables that are injected into your client-side JavaScript bundle. These variables are typically prefixed with
REACT_APP_
orVITE_
depending on your framework.For example, using Create React App:
1. Create a
.env
file in your project root:REACT_APP_API_KEY="your_publicly_safe_api_key"
2. Access it in your React code:
const apiKey = process.env.REACT_APP_API_KEY; console.log(apiKey);
⚠️ Important Note: Any variable prefixed this way (e.g.,
REACT_APP_
) is embedded into your public JavaScript files. Never use this method for truly secret keys like private API credentials. This technique is only for keys that are safe to be exposed in a user’s browser.
Don’t Forget .gitignore
!


This is perhaps the most important step after setting up your .env
file. If you commit your .env
file to a version control system like Git, your “secret” API keys will be publicly visible in your repository!
To prevent this:
Create a
.gitignore
file in your project’s root directory (if you don’t already have one).-
Add
.env
to this file:# Ignore node_modules folder node_modules # Ignore environment variables file .env # Ignore distribution or build folders dist/ build/
- Purpose of
.gitignore
: It tells Git which files and folders to ignore and not track. By adding.env
here, you ensure your sensitive configuration never makes it into your Git history.
Secret Management for Production
While .env
files are excellent for local development, secret management in production requires a more robust approach. Deploys to services like Heroku, AWS, Vercel, Netlify, or cloud platforms rely on their own methods for securely injecting environment variables into your deployed application.
- Heroku: Uses “Config Vars.”
- AWS (Lambda, EC2, etc.): Can use AWS Systems Manager Parameter Store, AWS Secrets Manager, or directly set environment variables in your service configuration.
- Vercel/Netlify: Have integrated Environment Variable UI where you can securely store and access your secrets.
These platforms provide a secure way to store your secrets that are directly accessible via process.env
on your server. You won’t have a .env
file on your production server; the platform injects these values for you. This is a key part of keeping your application secure in a live environment.
Serverless Functions and API Keys
If you’re working with serverless functions (like AWS Lambda, Netlify Functions, or Azure Functions), the principle remains the same. Your serverless function’s runtime environment will have access to environment variables.


How it works:
Best Practices Summary
Let’s recap the key ways to protect API keys:
- Never commit secrets to Git: Always add
.env
to your.gitignore
file. - Use
.env
files for local development: Install and use thedotenv
package. - Distinguish client-side vs. server-side: Only embed non-sensitive configuration into client-side bundles. Use truly secret keys for backend or serverless functions.
- Leverage platform-specific secret management: For production and deployment, use the environment variable solutions provided by your hosting platform.
- Use dedicated secrets management tools: For more complex setups, consider tools like AWS Secrets Manager or HashiCorp Vault.
FAQ
Q1: Can I put my API keys in a separate JavaScript configuration file like config.js
?
A1: While better than hardcoding directly in your main code, putting an unencrypted API key in a config.js
file is generally not recommended for sensitive keys, especially if that file is tracked by Git. If you use a config.js
file, it should ideally load its values from environment variables (using dotenv
for local development). For client-side applications, any configuration loaded and sent to the browser becomes public.
Q2: Should I use process.env.MY_API_KEY
for my API key in a React app that I’m building?
A2: It depends on the nature of the API key.
- If it’s a public API key for a service that doesn’t have strict security requirements (e.g., a public map API where usage is metered but not tied to billing in a sensitive way), then yes, you can put it in your
.env
file (prefixed withREACT_APP_
) and access it viaprocess.env.REACT_APP_MY_API_KEY
. It will be embedded in your build. - If it’s a private API key that grants access to sensitive data or has billing implications, absolutely not. You should use such keys only in your backend or serverless functions, never directly in client-side JavaScript.
Q3: How do I handle multiple API keys for different services?
A3: You can define multiple key-value pairs in your .env
file, each with a descriptive name, like STRIPE_SECRET_KEY=sk_test_...
and GOOGLE_MAPS_API_KEY=AIza...
. Then, you access them in your code using process.env.STRIPE_SECRET_KEY
and process.env.GOOGLE_MAPS_API_KEY
respectively. Remember to keep the truly secret ones out of client-side code.
Further Reading
- External Link: Express.js Documentation on Using Environment Variables (Illustrates general Node.js concepts applicable to many frameworks)
- External Link: 12-Factor App: Config (A foundational set of principles for building software as a service, including config management)
Conclusion
Mastering how to hide API keys in a JavaScript project is a fundamental skill for any developer. By understanding and implementing the use of environment variables, the .env
file, and the dotenv
package, along with diligent use of .gitignore
, you lay a strong foundation for secure secret management.
Whether you’re working locally or deploying to the cloud, keeping these practices in mind will significantly enhance the security and integrity of your applications. Keep learning and building securely!