Environment Variables: Stop Leaking Your API Keys
In this article
What are environment variables? Why API keys should not live in source code Set up environment variables in Node.js Commit an example, not your secrets Use a proper secret store in production A critical frontend warning What if a key was already leaked? Secure configuration checklistYou have an API key working on your laptop. You push the project, share the repository, and later discover that the key is now part of Git history. Deleting the line does not make the leaked secret safe again.
What are environment variables?
An environment variable is a named value provided to a process when it starts. Your application reads it at runtime instead of hard-coding it into a file that gets committed.
PORT=4000
DATABASE_URL=postgres://localhost:5432/app
API_KEY=replace-with-a-real-secret
Think of your app as a restaurant: the code is the recipe, while environment variables are the location-specific settings supplied when the kitchen opens.
Why API keys should not live in source code
Source code is copied, reviewed, logged, forked, and stored in version-control history. A secret committed once may remain accessible even after the visible line is removed.
- Do not paste secrets into frontend JavaScript: browser code is visible to visitors.
- Do not commit
.envfiles containing real credentials. - Do not print secrets in logs or error messages.
- Use separate credentials for local development, staging, and production.
Set up environment variables in Node.js
Create a local .env file and add it to .gitignore:
# .gitignore
.env
.env.*
!.env.example
Install dotenv if your Node.js version or runtime setup does not load the file automatically:
npm install dotenv
Load the variables at the application entry point:
import 'dotenv/config';
const port = process.env.PORT ?? 3000;
const apiKey = process.env.API_KEY;
if (!apiKey) {
throw new Error('API_KEY is required');
}
console.log(`Server starting on port ${port}`);
Notice that the example checks whether the key exists but never prints its value.
Commit an example, not your secrets
Teammates still need to know which settings the application expects. Provide a safe template with placeholder values:
# .env.example
PORT=3000
API_KEY=your_api_key_here
DATABASE_URL=your_database_url_here
Document how to obtain each value, but never put a working credential in the example file.
Use a proper secret store in production
For deployed applications, prefer the environment or secret-management feature provided by your hosting platform or cloud provider. Limit access by service and environment, audit access where possible, and rotate credentials regularly.
A critical frontend warning
Variables bundled into a browser application are not secret. Build tools often replace environment-variable references during compilation, meaning the value can be found in downloaded JavaScript.
// Server-side: secret can stay private
const response = await fetch('https://api.example.com/data', {
headers: { Authorization: `Bearer ${process.env.API_KEY}` }
});
Keep privileged API calls on your server. Let the browser call your backend, and let the backend use the private credential.
What if a key was already leaked?
- Revoke or rotate the exposed key immediately.
- Check provider logs for unexpected use.
- Remove the secret from the current code and repository history where appropriate.
- Review access permissions and reduce the key's scope.
- Check CI logs, build artifacts, screenshots, and deployment settings for copies.
Removing a secret from Git history is useful cleanup, but it does not replace revoking the credential.
Secure configuration checklist
✓ Real
.env files are ignored by Git.✓ An
.env.example documents required variables without real values.✓ Secrets are used only on the server.
✓ Production credentials are separate from development credentials.
✓ Logs and error responses never reveal secret values.
✓ Leaked credentials can be rotated quickly.
Good secret handling is mostly about habits: keep credentials out of code, limit their permissions, and assume that anything shipped to a browser is public.