API Key Leaked? A 10-Minute Emergency Checklist in 6 Steps (2026)

Blog / IP & privacy

API Key Leaked? A 10-Minute Emergency Checklist in 6 Steps (2026)

After an API key leaks, revoke it in the provider's dashboard first, then check usage for anything abnormal, and only then clean up your code and Git history. The order matters: as long as the key is still valid, deleting the whole repository does nothing, because someone may already have copied it.

The checklist below follows that order. If you've done this before, the first three steps take about ten minutes. Menu locations and spending-limit features differ by provider, so treat each provider's official docs as authoritative.

How do you confirm a key has really leaked?

Common situations:

  • You put the key in code and pushed it to a public repository, or it showed up in a screenshot, a log, or a chat history.
  • The provider sends an email or in-app notice saying your key was found in a public place. Some providers and code hosting platforms run secret scanning; check their official documentation for scope.
  • Your bill or usage suddenly jumps even though you didn't make those calls.

If you have any suspicion at all, treat it as leaked. Revoking a key is cheap, since you just issue a new one. Leaving it alone has an unbounded cost.

Step 1: Revoke first, don't delete code first

Log in to the provider's console, find the key, and disable or delete it. Once revoked, the old key stops working immediately and every program using it will return authentication errors. That is expected.

A few notes:

  • Creating the new key before revoking the old one shortens downtime. But if you've confirmed the key is being abused, accept the outage and revoke first.
  • If the key belongs to a project or organization, make sure you're revoking the right one. Key names and prefixes vary by provider; when they don't help, match on the key's label and creation time.
  • For OpenAI specifics, see OpenAI API key leaked: revoke it now, check usage, prevent a repeat. Anthropic keys are managed in the Console; for getting a key and setting permissions, see How to get a Claude API key.

Step 2: Check usage to see whether the key was abused

After revoking, look at the usage records and answer three questions:

  1. Were there calls during the leak window that you didn't make?
  2. When were they concentrated, and on which models?
  3. Is the cost higher than you expected?

Most providers let you view usage and cost per key or per project. Whether you can see the source IP of requests depends on the provider's console, so check the official docs. If you find clearly abnormal charges, keep screenshots and the time range, and contact the provider's support. Whether they waive the charges is up to the provider, so don't assume a refund.

Step 3: Set limits and permissions on the new key

Don't just recreate the key as it was. While you're at it, do three things:

  • Set a monthly budget cap and alerts (if the provider supports them).
  • Split keys by purpose: one per project, with development, testing, and production kept separate.
  • Where you can restrict permissions or the set of models, enable only what's needed.

For OpenAI project-level keys and permissions, see How to use OpenAI project API keys. For general budget and alert practices, see How to control API spending.

Step 4: Remove the leaked key from your code

If the leak happened because you pushed to GitHub, separate two things. The key is already revoked, so it's dead. Cleaning the history is so the repository doesn't keep an invalid key that confuses people, and so that any other secrets mixed in stop being exposed.

  1. First change the current code to read the key from an environment variable or a secrets manager.
  2. Check the whole history for other secrets:
git log -p -S"sk-" --all | head -50

Here sk- is only an example prefix; replace it with your key's prefix. You can also use a dedicated secret scanner such as gitleaks or trufflehog.

  1. If you need to rewrite history, use git filter-repo or BFG Repo-Cleaner to replace the content containing secrets, then force-push. Refer to the official docs of those two tools for exact commands.
  2. After rewriting history, collaborators need to re-clone, and existing forks and caches on GitHub may not disappear along with it. So don't treat history rewriting as the fix. Revocation is the fix.

Step 5: Find the source of the leak

If you only swap the key and never find the cause, the next one may leak too. Go through this list:

WhereWhat to check
Code repositoryCurrent files, past commits, deleted branches, whether .env was committed
.gitignoreWhether .env and config files are ignored
Frontend codeWhether browser-side JS or build output contains the key (frontend code should not call the API with a key embedded)
Logs and error reportsWhether request headers or stack traces printed the Authorization header
CI/CDWhether pipeline logs echoed environment variables
Screenshots and docsConfigs pasted into tutorial screenshots, tickets, or chat groups
SharingWhether one key was given to several people or machines

The last item deserves a separate mention. When several people share one key and something goes wrong, you can't tell who leaked it, and you can't revoke access for just one person. For team practices, see How teams should manage API keys.

Step 6: Confirm the service has recovered, and keep a record

After switching to the new key, check that production calls work again. If calls fail, first check the provider's status page to rule out an official outage. cdnping's API status page probes each official API endpoint from the Cloudflare edge every 5 minutes, and Is it down shows whether other people are reporting problems at the same time.

Then write a short record: when you noticed, which key, how it leaked, and what you did. Whoever takes over later will have something to go on.

How do you lower the chance of a repeat?

  • Keep keys only in environment variables or a secrets manager, never in code.
  • One key per purpose, and rotate them regularly.
  • Add a secret scan before commits locally (a pre-commit hook).
  • Set a budget cap so that a leak has a ceiling on its cost.

Using several model providers makes key management harder

Every provider you add is one more key to store, rotate, and put limits on. AGI Plan is a multi-provider LLM aggregation gateway. It is billed by monthly subscription in Chinese yuan (RMB) and doesn't require an overseas credit card. In tools like Claude Code and Codex you change just one endpoint address and can switch between multiple capability groups on the same quota, so the number of keys you manage shrinks to one place. Some models are marked as available only outside mainland China; see the AGI Plan models page for the current list. The operator states clearly that it is not an officially authorized channel. AGI Plan is a commercial partner of cdnping; for how to connect, see the AGI Plan getting-started page.

With or without a gateway, the six steps above apply: a gateway key also needs to be revoked, limited, and kept out of your repository.

Next steps

Do three things right now: log in and revoke the suspicious key; check the recent usage records; set a budget cap on the new key. The rest of the cleanup can wait, but these three come first.

Related articles

Keep reading