Skip to content
apikey.tools
Guide

Leaked an API Key? What to Do in the First Hour

A step-by-step incident checklist for an API key or secret committed to GitHub, pasted in a chat or exposed in a frontend bundle — revoke, audit, clean up history and prevent the next one, in that order.

By Abdessamad Bettal · Updated · 5 min read

Automated scanners watch public GitHub pushes continuously. A cloud credential pushed to a public repository can be found and used within minutes, and paid API keys — AI providers in particular — are abused for their quota almost as quickly. Assume the key is already in someone else's hands.

The order of the steps below matters. The instinct is to delete the commit first; that is the least important step, and doing it first wastes the minutes that count.

1. Identify exactly what leaked

Before you can revoke a key, you need to know which provider and which account it belongs to, and whether it is live or test. Paste it into the API key detector — it recognises the format of keys from dozens of providers, shows whether the key is a live or test credential, and links to the revocation page. Detection runs in your browser, so you are not leaking the key a second time.

Then ask: what can this key do? A read-only analytics token and an AWS root access key are different emergencies. The blast radius analyzer walks through what a credential can reach.

2. Revoke or rotate — immediately

Revocation is the only step that actually stops an attacker. Deleting the commit, making the repository private or force-pushing does nothing about a copy someone has already taken.

  • If nothing depends on the key, revoke it outright.
  • If production depends on it, rotate: create a new key, deploy it, then revoke the old one. Do this in minutes, not after the next release cycle. A short outage is cheaper than an abused key.

The revocation command generator produces the exact API call or dashboard path for common providers. For AWS, deactivating the key is a single CLI call:

bash
aws iam update-access-key --access-key-id AKIA… --status Inactive --user-name deploy-bot

Deactivate first, then delete once you are certain nothing legitimate still uses it.

3. Check whether it was used

Revocation stops future use; it does not tell you about the past. Look for activity between the moment of exposure and the moment of revocation:

  • AWS: CloudTrail events for the access key ID — look especially for RunInstances in regions you do not use, new IAM users or access keys, and billing spikes.
  • Stripe and payment providers: unexpected refunds, payouts or changes to payout bank accounts.
  • AI and SaaS APIs: usage dashboards and invoices for unusual volume.
  • GitHub tokens: the security log, and new deploy keys, webhooks or collaborators on repositories the token could reach.

If an attacker created new credentials while they had access, revoking the original key does not remove theirs. Look for persistence before you close the incident.

4. Remove it from history

Now — and only now — clean up the repository. Deleting the file in a new commit leaves the secret in every earlier commit. Rewrite history with git filter-repo:

bash
# Replace every occurrence of the secret across all history
echo 'sk_live_51Hq…==>REDACTED' > replacements.txt
git filter-repo --replace-text replacements.txt
git push --force --all
git push --force --tags

Then deal with the copies you do not control: forks, open pull requests, CI logs, and caches of the repository. On GitHub, ask support to purge cached views of the affected commits. This is hygiene, not remediation — the key is already revoked.

5. Find anything else that leaked with it

Secrets rarely leak alone. A committed .env file usually contains five of them. Scan the commit, the rest of the repository and any logs with the secret leak scanner and the hardcoded secret linter, and rotate everything they find.

6. Make the next one impossible

  • Ignore secret files by default. Generate a .gitignore covering .env, key files and credential exports with the gitignore generator, and commit a .env.example with placeholder values instead.
  • Turn on push protection. GitHub secret scanning push protection blocks pushes containing recognised credentials. Pre-commit hooks such as gitleaks or trufflehog catch the rest locally.
  • Never ship secrets to the browser. Anything in a frontend bundle — including variables with NEXT_PUBLIC_ or VITE_ prefixes — is public. See where to store API keys.
  • Use short-lived credentials. Cloud roles, OIDC federation from CI and expiring tokens turn a leak into a short window rather than a standing risk.
  • Use recognisable formats. If you issue keys yourself, a distinctive prefix lets scanners catch your customers' leaks for you.

The checklist

  1. Identify the provider, account and environment of the key.
  2. Revoke it, or rotate and then revoke within minutes.
  3. Audit usage between exposure and revocation; look for persistence.
  4. Rewrite git history and purge caches.
  5. Scan for and rotate every other secret exposed alongside it.
  6. Add ignore rules, push protection and pre-commit scanning.
  7. Write down what happened and what you changed.

Frequently asked questions

Is it enough to delete the commit or make the repo private?

No. The key may have been cloned, cached or scraped the moment it was pushed, and making a repository private does not affect existing forks or copies. Only revoking the key protects you; cleaning history is follow-up.

Will GitHub tell me if I push an API key?

Often. GitHub secret scanning detects many providers' token formats and, through its partner program, notifies the provider, which may revoke the key automatically or alert you. Do not rely on it: coverage depends on the provider and token format, and the notification can arrive after an attacker has already used the key.

I leaked a test-mode key. Do I need to rotate it?

Rotate it anyway — it is quick. Test keys usually cannot touch real money or data, but they can reveal account details, be used to spam your test environment, and are sometimes mislabelled. The test vs live key checker tells you which kind you have.

Can someone use my API key if they only saw it in a screenshot?

Yes. A key is just text; anyone who can read it can use it. Treat a key visible in a screenshot, a screen share or a support ticket exactly like one committed to a public repository.

Try it in your browser

Every tool referenced above runs locally — nothing you paste is uploaded.

More guides