How to Fix a Leaked API Key: Revoke, Rotate and Verify

A provider-neutral runbook for a leaked API key: revoke first, rotate without downtime, read the usage logs, clean Git history and prove the old key returns 401.

Ashley Innocent

Ashley Innocent

30 November 2026

How to Fix a Leaked API Key: Revoke, Rotate and Verify

Apidog for Enterprise

On-Premises Deploy

SSO & RBAC

SOC 2 Compliant

Explore Apidog Enterprise

If you just found a leaked API key, do these five things in order: revoke or rotate the key at the provider, ship the new key everywhere the old one lived, read the provider’s usage and audit logs for abuse, clean the leak out of Git history and every other copy, then prove with a test that the old key now gets a 401. Deleting the commit comes last, not first.

This runbook covers OpenAI, Anthropic, Gemini, AWS, Stripe and GitHub keys, and works the same for your own internal keys.

The first 5 minutes: revoke before anything else

Revoke or rotate the key at the provider before you touch the repo. Once a key has been public, assume someone copied it. Making the repo private, deleting the file or force pushing a clean history changes your copy of the code. It does nothing to the copy a scraper already has, and the key still works until the provider says otherwise.

Some providers will beat you to it. Anthropic says that when GitHub finds a Claude API key in a public repository, GitHub notifies Anthropic and Anthropic deactivates the key automatically, then emails you. Google says Gemini auth keys come with leaked key enforcement that stops the use of leaked keys its systems detect. Don’t count on either. Revoke it yourself, then check that it’s really dead.

Here’s the race you’re in, and where each response step lands:

Sequence diagram: a leaked key is copied by a scraper and used while you revoke it, switch to the new key, read logs and verify the old key gets 401

Note the earliest time the key could have been exposed. You’ll need it when you read the logs.

How to rotate a leaked API key without breaking production

Pick your path based on one question: is there any sign the key is being abused right now?

  1. Active abuse (unknown spend, unknown IPs, new resources): revoke the old key now and accept a short outage. A few minutes of downtime costs less than an open key.
  2. No sign of abuse: create the new key first, deploy it, confirm traffic has moved, then revoke the old one. GitHub’s own remediation guide recommends this order when downtime is a concern.

Stripe builds the second path into its dashboard. When you rotate a key there, both keys work for up to 7 days, and choosing Now as the expiration deletes the old key at once. For a leaked key, pick Now.

AWS says the same for exposed access keys: create a new key, deactivate the original, check your app, then delete the original.

Then replace the key in every place it lives. Most teams miss at least one:

A key pasted into an agent’s settings can end up in a file you commit, as our Claude Code “Don’t Ask Again” post shows.

Check what the leaked key was used for

Read the usage and audit logs for the window between your exposure timestamp and the moment you revoked. You’re looking for three things: usage you don’t recognize (models, regions or endpoints you never call), requests from IPs or user agents outside your infrastructure, and new resources such as instances, users, keys or webhooks.

This table lists where to revoke and where to read usage for the common providers. Every path was checked on the provider’s own docs on November 30, 2026.

Provider Revoke or rotate Read usage and activity
OpenAI API keys page: delete the key Usage page (platform.openai.com/usage); per-key tracking is on for keys created after Dec 20, 2023
Anthropic Claude Console, API keys (platform.claude.com/settings/keys): menu, then Delete API key Usage page (platform.claude.com/usage) and Logs (platform.claude.com/logs)
Google Gemini AI Studio API keys page (aistudio.google.com/api-keys) AI Studio, Dashboard, then Usage (aistudio.google.com/usage)
AWS IAM: create a new access key, deactivate the old one, then delete it CloudTrail Event history; Cost Explorer for charges
Stripe Dashboard API keys page: rotate with expiration Now, or Expire key Workbench request logs
GitHub Settings, Developer settings, Personal access tokens: Delete Settings, Security log (last 90 days); org audit log

AWS also attaches a quarantine policy, AWSCompromisedKeyQuarantineV2, to IAM users whose keys it finds exposed, per its account compromise guide. If you see it on a user, treat that as confirmation, not as the fix.

If you find abuse, screenshot the logs and open a support ticket with your timeline. For AI keys, our LLMjacking guide covers the account lock-down controls each provider offers.

Clean up the leak source

Revoking ends the risk. Cleaning up stops scanners flagging the string and teammates copying it back.

For Git, rewrite history with git-filter-repo or BFG Repo-Cleaner, then force push. Know the limits before you do. GitHub’s sensitive data removal guide says the old commits can still be reached through clones, forks, cached views by commit SHA, and pull requests that reference them. You can’t clean other people’s clones or forks. GitHub Support can remove cached views and pull request references, but only when rotating the credential can’t mitigate the risk. That’s another reason rotation comes first.

A rewrite also breaks closed pull request diffs and strips commit signatures, so warn your team first.

Then sweep the places that aren’t Git:

Schema: one API key belongs in a secret manager but leaks through Git history, CI logs, frontend bundles, chat and coding-agent files

Prove the old key is dead with Apidog CLI

A revoked key that still returns 200 is the worst outcome, and a dashboard won’t show it. Let your coding agent write and run a test with Apidog CLI so the check lives in CI, not in your memory.

Set up the CLI once:

npm install -g apidog-cli@latest
apidog login --with-token <YOUR_TOKEN>
npx -y skills add https://apidog.com   # pick your agent when prompted

Then give your agent one prompt:

In Apidog project <projectId>, create a test scenario called "Leaked key is dead" with
two steps against <our API endpoint>: call it with {{OLD_KEY}} and assert status 401,
then call it with {{NEW_KEY}} and assert status 200. Read the CLI schema and validate
the payload before writing. Run it with both keys passed as variables, print the
result, and never echo either key in your summary.

The agent reads the schema, validates its file, writes the scenario and runs it:

apidog cli-schema get test-scenario-create
apidog test-scenario create --project <projectId> --file scenario.json
apidog cli-schema validate test-scenario-create --file steps.json
apidog test-scenario update <scenarioId> --project <projectId> --file steps.json
apidog run -t <scenarioId> --project <projectId> -e <envId> \
  --env-var "OLD_KEY=$OLD_KEY" --env-var "NEW_KEY=$NEW_KEY" -r cli,junit

Here’s that run against a stand-in API that only accepts the new key:

Apidog CLI running a sandbox scenario: the old key gets 401, the new key gets 200, both assertions pass

If your project blocks AI writes on the main branch, the CLI says so and suggests an AI branch; pass it with --branch <branchName>. Add the same apidog run ... -r junit line to your pipeline and the dead-key check runs on every deploy. The CI/CD guide shows the GitHub Actions setup, and the apidog run reference lists every flag.

A second use, if your own API issues keys: test that your revoke endpoint really works. Ask your agent for a scenario that creates a key, calls a protected endpoint (expect 200), calls your revoke endpoint, then calls the protected endpoint again right away (expect 401). If the last step passes only after a delay, your auth layer is caching key lookups, and a revoked key stays usable for the cache TTL. Run it with -n 5 to repeat it and catch a cache that only misses sometimes.

To be clear about scope: Apidog doesn’t revoke or rotate third-party keys, and it doesn’t stop leaks. It proves the fix worked and keeps proving it.

How to stop the next API key leak

Most leaks start as a key in a file Git tracks. Block it at three points:

  1. On the developer’s machine: run a pre-commit secret scanner such as Gitleaks so the commit fails before it exists.
  2. On push: turn on secret scanning with push protection on GitHub, which blocks pushes that contain supported secret types.
  3. In the code: read keys from environment variables or a vault, never from source. Our secret scanner guide compares the tools.

Then shrink the damage of the leak you didn’t catch:

Conclusion

The order matters: revoke, replace, read the logs, clean up, verify. Verify is the step teams skip, and a coding agent can automate it in one prompt.

If you’d rather hand the whole checklist out, Sharkly puts it on a board as tasks for your team’s coding agents. They run in parallel on your own machines with your own Claude Code or Codex plan, and you review what they change before it merges. It’s free to start.

FAQ

Is deleting the commit enough to fix a leaked API key? No. Clones, forks and cached views can still hold the key, and anyone who copied it can still use it. Only revoking it at the provider makes it useless.

How fast do leaked API keys get used? Assume it’s fast enough that you can’t wait. Automated scrapers watch public pushes, and some providers, such as Anthropic through GitHub’s scanning, deactivate exposed keys automatically because of that speed. Revoke first and investigate second.

Will the provider refund charges made with my leaked key? Don’t count on it. None of the provider pages linked in this guide promise a refund for usage on a leaked key. Contact support quickly with your timeline and log screenshots, and set spend limits so the next incident has a ceiling.

Do I need to rotate the key if the repo was private? Yes. Collaborators, CI systems, integrations and backups can read a private repo, and repos get made public by mistake. Rotate it.

How do I know the old key is really revoked? Call the API with it and check for a 401 or 403. Better, automate that call as a test, run it with apidog run, and keep it in CI so it fails loudly if the old key ever works again.

Explore more

MCP Skills: How MCP Servers Deliver Agent Skills (and What You Now Approve)

MCP Skills: How MCP Servers Deliver Agent Skills (and What You Now Approve)

MCP servers can now ship Agent Skills next to their tools. See how skills/list and digests work, which clients support it, and how to catch a skill that changed.

30 November 2026

Antigravity Quota: Why ACP Custom Agents Burn 4x Per Token

Antigravity Quota: Why ACP Custom Agents Burn 4x Per Token

Since Antigravity CLI 1.5.17, custom agents draw about 4x the Gemini quota per token. See the evidence, measure your own burn, and switch to setups that bill at the standard rate.

30 November 2026

Cloudflare Search for Agents: How to Turn On /.well-known/search and Test It

Cloudflare Search for Agents: How to Turn On /.well-known/search and Test It

Cloudflare Search for Agents gives every page a /.well-known/search endpoint with one toggle. See what the toggle builds, what a real GET and QUERY response looks like, and how to let your coding agent test it with Apidog CLI.

30 November 2026

Practice API Design-first in Apidog

Discover an easier way to build and use APIs

How to Fix a Leaked API Key: Revoke, Rotate and Verify