Skip to content

Managing API keys and secrets

Where API keys leak, where they do belong, how to set up push protection, separate keys and spend limits, and what to do in the first 15 minutes after a leak.

In short

An API key is money and access: whoever has it uses it on your bill. According to GitGuardian, around 29 million new secrets ended up on public GitHub in 2025, and one stolen Gemini key turned a bill of $180 a month into $82,314 in two days. Store keys in a password manager or secret manager, give every project its own key with a spend limit, turn on push protection and revoke a leaked key within minutes.

  • Intermediate
  • 7 min
  • free
  • Fact-checked on 2 November 2026

An API key is a credit card without a PIN. With agents that read your files and write code, there are more keys lying around than ever.

After this lesson you have: your keys in a safe place, a separate key with a limit per project, push protection on, and a plan for the first 15 minutes after a leak.

In 2025, GitGuardian counted around 29 million new secrets in public GitHub commits (+34%). Keys for AI services rose by 81%, and in MCP server config files they found 24,008. Of the valid secrets from 2022, 64% still worked four years later.

Where How it goes wrong
Git A key in the code or a committed .env file. Deleting it in a later commit doesn’t help: it’s in the history.
Frontend A key in JavaScript or an app bundle. Anyone can read it out.
Agent files and logs MCP configs, session logs and .env that your coding agent reads. Infostealers specifically look there.
Stored with third parties In the OpenCode Console hack (Oct 2026), users’ stored AI keys were abused; some users reported damages of more than $1,000.
Screenshots and chats A key on screen during a demo, or pasted into a chat with an AI assistant or colleague. Vapi literally lists logs, screenshots and agent chats as places where a key must never appear.

What it costs: a Mexican startup that normally spent $180 a month on Gemini got a bill of $82,314 in about 48 hours in February 2026, after a stolen key. Google pointed to the shared responsibility model.

Situation Place How it works
Your own laptop Password manager with a CLI 1Password CLI or Bitwarden Secrets Manager only puts the key into an environment variable at startup. Your project contains just a reference.
Server or VPS Environment variables from a file outside your repo, or sops + age sops encrypts your config file, so it can safely go into git.
Hosting, CI, serverless The platform’s secret manager GitHub Actions secrets, Vercel/Netlify environment variables, cloud secret managers.

With 1Password your .env looks like this (no secret, just a reference):

Terminal window
OPENAI_API_KEY="op://Werk/OpenAI/credential"

And you start your app like this:

Terminal window
op run --env-file=.env -- npm start

With Bitwarden Secrets Manager it’s bws run -- npm start (with your access token in BWS_ACCESS_TOKEN).

  1. One key per project, client or agent. Give each key a name (“klant-bakkerij-chatbot”) and only the permissions it needs: read-only where possible, one repo for a GitHub token, an expiry date. If one leaks, you revoke only that one. Working for clients? Keep the key on the client’s account.

  2. A spend limit per project or key. Every major provider has one now, but they work differently: OpenAI per organisation and per project (only a hard stop if you turn on Enforce a hard limit), Anthropic per organisation and per workspace, Google per project in AI Studio (with up to about 10 minutes’ delay) and OpenRouter per key. Set it low and turn off automatic top-ups. As of November 2026; check the official pages: OpenAI, Anthropic, Google Gemini, OpenRouter.

  3. .env in .gitignore, before your first commit.

    Terminal window
    echo ".env" >> .gitignore
  4. Push protection on. For your personal account, GitHub turns on push protection for yourself by default for public repos; check it under Settings → Code security, heading “User”. Per repo: Settings → Advanced Security → Secret Protection → Push protection → Enable. Free for public repos; for private repos you need the paid GitHub Secret Protection.

  5. Scan what’s already there. gitleaks searches your entire git history:

    Terminal window
    gitleaks git -v

    Getting unknown command "git"? Then you have a version older than 8.19 (for example via apt on Ubuntu 24.04); use gitleaks detect -v instead.

    Keys you find are burned, even if they were only “briefly” in an old commit. Revoke them.

  1. Revoke and replace (minute 0-5). In the provider’s dashboard: revoke the old key, create a new one, put the new one in your password manager and your apps. This first, clean up afterwards. Bots find public keys within minutes.

  2. Put the brakes on costs (minute 5). Lower the limit, turn off automatic top-ups. For voice services: temporarily disable outbound calling.

  3. Check logs and costs (minute 5-10). Usage per key or project: unknown models, regions or times? For cloud keys, also check for new users or keys in IAM, because attackers often create a spare. Did the leak come from malware on your computer? Then all keys and sessions on that machine are suspect.

  4. Clean up git history (minute 10). Remove the key from the code. If it was in a pushed commit, follow GitHub’s guide Removing sensitive data (with git filter-repo). Clones and forks keep it anyway, so this is cleanup, not a fix.

  5. Report (minute 10-15). Ask the provider’s support to waive the unjustified charges; OpenAI refunded affected OpenCode users, while Google held to shared responsibility in the $82k case. Working for a client: tell them right away. Could someone access personal data? Then report it within 72 hours to the Autoriteit Persoonsgegevens (the Dutch Data Protection Authority), unless it’s unlikely to pose a risk to people (how the notification duty works).

Checklist

0/7

Your progress is only stored in this browser.

Test yourself

  1. 1You notice you pushed an API key to a public repo. What do you do first?

  2. 2Why one key per project or client?

  3. 3What is the safest place for an API key on your laptop?

Next lesson: hardening your server. Not self-hosting anything? Continue to the security check for your app.