PublishedNovember 23, 2026Last updatedNovember 23, 2026Reading time4 min readDifficultyBeginner

Environment Variables: Stop Leaking Your API Keys

Your API key works perfectly—until it ends up in a public Git repository. Learn a safer way to manage configuration and secrets across development and production.

You 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.

Key takeaway: Environment variables keep configuration out of source code, but they are not a complete secrets-management system. Never commit real secrets, rotate anything that leaks, and restrict each key to the access it actually needs.

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.

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.

Important: Environment variables can still be exposed through process inspection, crash reports, debug endpoints, or misconfigured logs. Treat them as sensitive data.

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?

  1. Revoke or rotate the exposed key immediately.
  2. Check provider logs for unexpected use.
  3. Remove the secret from the current code and repository history where appropriate.
  4. Review access permissions and reduce the key's scope.
  5. 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

Before deploying:

✓ 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.