The safest way to store an API key is to keep it out of your code and your repository, hold it in an encrypted store, scope it to the least access it needs, and make it easy to rotate. Where you put it matters less than how much damage it can do if it leaks, and how quickly you can replace it.

Why do API keys leak in the first place?

Mostly through ordinary copies. A key is created in a dashboard, pasted into a .env file, exported in a shell, added to a CI variable, dropped into a chat message to get help, and left in shell history. This is secret sprawl: the same value in many places nobody tracks, each one a way to leak and each one something you must find when you rotate.

The recorded incidents show what attackers go for. In the 2025 Nx compromise, a malicious package collected GitHub and npm tokens, SSH keys and .env files from developer machines. See the incidents list. Plain files in predictable locations are the first thing hostile code reads.

What should you check before choosing a place?

Ask four questions about each key:

  1. What can it do? A key limited to one project and a few actions is a small problem if it leaks. A full-access key is a large one.
  2. Who can read the place it lives? Everyone with repository access, every process on your machine, every CI job, every teammate?
  3. Can you replace it quickly? If rotation takes an afternoon, you will put it off.
  4. Will you know if it is used? Providers with request logs let you spot misuse. Stripe, for example, offers request logs per key in the Dashboard (/keys/stripe).

How should you store keys during local development?

Use the lowest rung that is still honest about its limits.

  1. Use a development or test key. Where the provider issues test keys (Stripe has sk_test_ keys), use them locally and keep live keys elsewhere.
  2. Load the key from an environment variable. Read it in your program from the environment instead of writing it in source. OpenAI’s API reference tells you to load the key on the server from an environment variable or a key management service, and not to put it in client-side code such as browsers or apps (/keys/openai).
  3. If you use a .env file, git-ignore it. Add it to .gitignore before you create it, and commit a .env.example that has names but no values. See what an env file is.
  4. Keep the file out of reach of tools that do not need it. AI coding tools can read project files. The guide to keeping env files away from AI agents covers the per-tool settings.
  5. Never paste a key into a chat, ticket or screenshot. Anything pasted there is copied to systems you do not control.

How should you store keys in production?

Do not read secrets from a file shipped with the code.

  1. Use your platform’s secret store or a secrets manager. The point is that the value is injected at run time and is not part of the build artifact. See secrets management.
  2. Prefer short-lived credentials over stored ones. AWS recommends using IAM roles for temporary credentials instead of user access keys where you can (/keys/aws). GitHub Actions can use OpenID Connect to get short-lived cloud credentials instead of storing long-lived keys. See the CI/CD guide.
  3. Give each service its own key. If every service shares one key, one leak compromises all of them, and revoking it breaks all of them. Anthropic, for instance, recommends service account keys for shared or automated workloads (/keys/anthropic).
  4. Restrict by network or resource where the provider allows. Stripe lets you attach an access policy with IP address ranges to a key.

How do you scope a key?

Create the narrowest credential the provider offers. Stripe has restricted keys that carry only the permissions you grant. GitHub has fine-grained tokens limited to one owner, chosen repositories and per-permission levels, while classic tokens reach every repository the account can access (/keys/github). A leaked narrow key is an incident; a leaked broad key is a breach. This is least privilege applied to credentials.

Set an expiry when the provider lets you. Anthropic lets you choose a lifetime at creation, for example, and cannot change it afterwards.

How do you notice a leak early?

  1. Turn on secret scanning where your host offers it. GitHub documents secret scanning alerts on the repository’s security tab (/keys/github). See secret scanning.
  2. Run a scanner in CI and as a pre-commit check, so a key is caught before it is pushed. Pick a maintained open-source scanner and check its documentation.
  3. Use the key identifier at /tools/api-key-identifier to recognize what kind of key a string is by its prefix, which tells you where to revoke it. Avoid pasting a live key into any web page you do not control.
  4. Watch usage. Review request logs and billing alerts for traffic you do not recognize.

What should you never do?

  • Commit a key to git, even in a private repository or “just for now”.
  • Put a secret in client-side code. A browser or mobile app ships its code to the user.
  • Reuse one key across projects and environments.
  • Store keys in a note, spreadsheet or message thread with no access control.
  • Keep a key you no longer use. Delete it.

What if a key is already exposed?

Stop and follow what to do when an API key leaks. Revoke or rotate first, then investigate. Regular rotation also limits the life of any copy you missed: see how to rotate API keys.

Where does BotPassword fit?

BotPassword is a password manager for people and AI agents. It keeps API keys encrypted on your devices and lets a command or an agent use a key without the key being handed over as plaintext. For a command, it injects the value into just that process; for an agent, it performs the action and returns only the result. It also takes in keys from existing .env files, and the env to BotPassword tool helps with that conversion.

Frequently asked questions

Is it safe to store API keys in a .env file?

It is acceptable for local development if the file is git-ignored, never committed and holds only low-privilege development keys. It is not a secrets store: it is plaintext on disk, and any tool or script running as you can read it.

Should I put API keys in environment variables?

Environment variables keep keys out of source code, and are the usual way programs receive them. Remember that child processes inherit them, so anything you launch from that shell can read them.

What is the safest place to keep an API key?

A dedicated secrets manager or password manager that encrypts the value and releases it only to the process that needs it, combined with a narrowly scoped key that you can rotate. No single location is safe if the key itself has broad permissions.

What should I do if I committed a key to git by accident?

Treat it as exposed: revoke or rotate it first, then clean up. Deleting the commit does not help if the key was already pushed or cloned.

More guides

Give agents access. Keep the keys.

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