Security

Leaked API Key? What to Do in the First Hour, Step by Step

If an API key leaks, revoke or delete it at the provider immediately, then create a new key and put it into your app. Only after that, check the provider's logs for misuse and remove the key from wherever it leaked; deleting the commit or making the repo private does not make an exposed key safe again.

The order matters. Cleaning up the leak first feels natural, but the key keeps working the whole time. OWASP's Secrets Management Cheat Sheet puts it plainly: "Keys that were exposed should undergo immediate revocation."

Step 1: Revoke the key (minutes, not hours)

Go to the provider's dashboard or API where the key was created and disable it. The wording varies: revoke, delete, deactivate, roll or regenerate. All of them make the old value stop working.

  • AWS access key (an ID like AKIAIOSFODNN7EXAMPLE plus a secret like wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY, the documented example values): IAM console > Users > the user > Security credentials > Access keys > Actions > Deactivate. From the CLI:
    aws iam update-access-key --user-name alex --access-key-id AKIAIOSFODNN7EXAMPLE --status Inactive
  • Most SaaS APIs (payments, email, maps, AI providers): Settings or Developers > API keys > revoke or roll the key. If the dashboard offers a grace period where old and new keys both work, choose immediate expiry for a leaked key.
  • A personal access token (GitHub, GitLab, npm and similar): your account's token settings page, then delete the token.

Expect something to break. Any service using the old key fails until it gets the new one. That outage is the price of closing the hole, and it is usually short if you do step 2 right away.

Why not rotate gracefully?

AWS documents a no-downtime rotation: create a second key (IAM allows two per user), move apps to it, check the old key's Last used information, and "wait several days" before deactivating. That procedure is for planned rotation. For a leaked key, waiting days leaves an attacker those days too. Deactivate first and accept the brief breakage.

Step 2: Create a new key and deploy it

  1. Create a new key. Scope it to the smallest set of permissions the app actually uses, which is a good moment to tighten an over-broad key; see the principle of least privilege.
  2. Store it where your app reads secrets: an environment variable, your hosting platform's secret settings, CI secrets, or a secrets manager. Not in source code. If you're unsure how, see how to set environment variables.
  3. Redeploy or restart every service that used the old key. OWASP notes that "Restarting the application does not itself revoke stolen credentials", which is why the revoke in step 1 has to happen at the provider, not in your app.

Step 3: Check whether the key was used

Work out the exposure window: from the moment the key became visible (the commit time, the log time, the moment the screenshot was posted) to the moment you revoked it. Then look at what happened in that window.

  • AWS: AWS's guidance is to use CloudTrail to monitor access key usage. Search CloudTrail for API calls made with the leaked key ID, and run aws iam get-access-key-last-used --access-key-id AKIAIOSFODNN7EXAMPLE to see when and where it was last used. Also check billing for unexpected resources, such as compute instances in regions you don't use.
  • Other providers: open the usage, logs or billing page and look for request spikes, unfamiliar IP addresses or endpoints your app never calls.
  • Data access: if the key could read customer data, record what it could reach. OWASP lists the questions responders should be able to answer: who had access, when the secret was used, and when it was last rotated.

If you find misuse, contact the provider's support. Unexpected charges and abuse reports go faster when you can give them the key ID, the time window and the fact that the key is already revoked.

Step 4: Remove the key from where it leaked

With the key dead, cleanup is about hygiene and about stopping scanners from flagging it forever.

Where it leakedCleanup
A commit you have not pushedgit commit --amend or reset; no one else has it
A pushed commitRemove it from history with git filter-repo and force-push; see how to remove a secret from Git history
CI or application logsDelete or redact the log lines; OWASP says logs need a process to remove secrets "while maintaining log integrity"
Issue, chat message, ticket, pasteEdit or delete the message, and ask admins to purge edit history if the platform keeps it
Screenshot or videoTake it down and replace it with a redacted copy

To find every copy in a local repository, search all of history, not just the current files. Replace the example value with the start of your own key:

git log --all --oneline -S "AKIAIOSFODNN7EXAMPLE"
git grep "AKIAIOSFODNN7EXAMPLE" $(git rev-list --all)

On GitHub, secret scanning "scans your entire Git history on all branches", and for supported providers GitHub notifies the provider so they can act, for example by revoking the key. Treat that as a safety net, not as step 1.

Step 5: Stop it happening again

  • Keep secrets out of files Git tracks. Read them from environment variables; keep local values in a .env file listed in .gitignore. AWS's own guidance says not to put access keys in application files or in your project area.
  • Turn on push protection. GitHub's push protection blocks pushes that contain recognised secrets. User-level push protection is on by default for pushes to public repositories on GitHub.com; repository-level protection has to be enabled by an admin.
  • Scan before commit. A pre-commit hook running a scanner such as gitleaks catches keys before they leave your machine. OWASP recommends detecting secrets at the developer level, before they reach the repository.
  • Prefer short-lived credentials. AWS recommends temporary credentials (IAM roles) over long-term access keys, and OWASP recommends dynamic secrets "where possible". A token that expires in an hour limits the damage of any leak.
  • Watch the less obvious channels. Debug output, error pages, notebooks with printed config, screenshots in bug reports, and keys pasted into chat tools or AI assistants are easy to overlook.

Frequently asked questions

Is it enough to delete the commit or make the repository private?
No. Once a key has been public, assume someone copied it. Making the repo private or deleting the file stops new people from seeing it, but does nothing about copies already taken. The only fix is to revoke the key at the provider so it stops working.
I leaked a key in a private repository. Do I still need to rotate it?
Usually yes. Everyone with access to the repo, every clone, every fork and every CI system that checks it out now has the key. Rotation is cheap compared with auditing who might have copied it. The exception is a test key with no access to real data or money, which you can simply delete.
Will GitHub revoke my leaked key for me?
Sometimes. GitHub secret scanning notifies supported providers when it detects their secrets, and the provider can then take action, such as revoking the credential. Don't rely on it: not every provider takes part, and you should revoke the key yourself as soon as you know.
How do I find out whether a leaked key was used?
Check the provider's own logs and usage pages. AWS records API calls in CloudTrail and shows a key's last-used time in IAM. Most API providers have a usage or billing dashboard with request counts by key. Look at the period from when the key was exposed to when you revoked it.
What counts as an API key leak?
Any copy outside the places that need it: a pushed commit, a public gist, a paste site, a CI log, an error message, a support ticket, a screenshot, a shared notebook, a chat message, or an AI chat transcript. If you can't be sure who saw it, treat it as leaked.

Sources