Guide

How to store API keys in Node.js safely

In Node.js, read secrets from process.env. Since Node 20.6 you can load a .env file without a library using the --env-file flag, and in production you should set real environment variables from your host's secret store. Check required variables at start-up so the app fails fast.

Read the key from the environment

const apiKey = process.env.STRIPE_SECRET_KEY;
if (!apiKey) {
  throw new Error("STRIPE_SECRET_KEY is not set");
}

Local development

Run node --env-file=.env app.js (Node 20.6 and later) or use the dotenv package with require("dotenv").config(). Keep .env in .gitignore and commit a .env.example with names only. The file is plaintext and readable by every process you run, including editors' extensions and AI coding agents, so keep production keys out of it.

Production

Set environment variables through your host's secret management (cloud secrets managers, Kubernetes Secrets, platform dashboards, CI encrypted variables) and do not bake them into images or bundles. Validate them at start-up. For serverless platforms, use the provider's encrypted environment settings rather than committing config files.

Mistakes to avoid

  • Bundling server code into a browser build, which can inline process.env.X values into public JavaScript; keep secrets in code that only runs on the server.
  • Logging process.env or whole config objects, which writes every secret to your log provider.
  • Committing .env files or copying them into Docker images with COPY . .; use .dockerignore and inject values at run time.
  • Putting secrets in package.json scripts or in npm lifecycle commands, where they are visible in process lists and CI logs.

When an AI agent works on your Node.js project

Coding agents run inside your project folder, so they can read the same .env file your app reads. BotPassword injects keys at run time instead: the agent calls the run_with intent through the MCP server and the process gets the variables it needs while the agent sees only redacted output. Planned: bpw run, a CLI form of the same.

Frequently asked questions

How do I use environment variables in Node.js?

Read them from process.env.NAME. Locally, load a .env file with node --env-file=.env app.js (Node 20.6+) or the dotenv package, and in production set real variables in your host.

Should I commit my .env file?

No. Add it to .gitignore and commit a .env.example listing variable names only. If a real key was ever committed, treat it as compromised and rotate it.

Should I keep API keys in a .env file for Node.js?

A .env file that is git-ignored is fine for local development, but every process and every AI coding agent running in that folder can read it. Load secrets into the environment at run time from a password manager or secrets manager instead, so no plaintext file sits in the project.

Other languages and frameworks

Give agents access. Keep the keys.

BotPassword is in early access. Request an invite and we'll email you when your spot opens.