Leaked API key: what to do in the first hour
Treat a leaked API key as already used. In the first hour, revoke or rotate the key first, because that is the most important step. Then check what the key did. Then cap spending. Clean the Git history last, and only once the key is dead. Deleting the key from your code, or deleting and recreating the repository, does not stop anyone from using it.
You found a key where it should not be: in a public repository, a screenshot, a chat, a log. Do not panic, and work through the steps below in order. The step people do first is the one that helps least, which is cleaning up the evidence.
01Assume the leaked API key is already in use
Treat any leaked secret as immediately compromised. Automated scanners can find publicly exposed secrets in minutes, and malicious actors can exploit them within hours. You may be the first to notice or the last, and the repository alone cannot tell you which.
Old leaks stay dangerous too: 70% of the secrets leaked in 2022 are still valid. Nobody revoked them. Waiting to see whether anything happens is the one choice that costs you, so start with the first step even if you are unsure the key was seen. If you wonder what a stolen key can spend, what an API key is shows one real bill.
02First: revoke or rotate the key
The most important step is revoking the secret with its provider. That usually means logging into the provider's portal and going to the section where the secret is managed. Exact menu names differ, so the provider pages below give the route for OpenAI and AWS. The provider is also the most reliable source on whether a secret is still valid.
Rotating means replacing the key. Create a replacement, switch your applications to it, then revoke the old one once the new one works. If downtime worries you, generate the new secret with the same permissions first.
If the key is being used against you right now, skip the careful switch: revoke it first, and the app will stop until the new key is in place.
Three details save you from mistakes:
- A revoked token cannot be restored. You create a new one. If you think your account or your hardware may be compromised, GitHub lets you revoke all your credentials at once from your account settings.
- For AWS access keys, deactivate the old key and do not delete it in that step. Even if its Last used information shows it was never used, deactivate it rather than delete it at once.
- A valid GitHub token pushed to a public repository or gist is revoked automatically. Do not rely on that for other providers.
For other providers' keys in a GitHub repository, GitHub notifies the provider so they can revoke the credential, and these secrets are not shown in your repository alerts. A repository with no alerts does not mean the key is safe.
Do this from a computer you trust. If the leak came from malware on your Mac, rotate from a trusted system and read the infostealer check.
03Second: check what the key did
A new key does not tell you what the old one already did. Look for logs or audit trails that show the secret being used. Where to look depends on the provider:
| Provider | Where to look | Page |
|---|---|---|
| OpenAI | The Usage page, where key usage can be monitored once tracking is on | OpenAI key leaked |
| AWS | CloudTrail Event history, for keys, policies, roles or temporary credentials you did not create | AWS access key leaked |
| GitHub tokens | The repository history and alerts | Exposed on GitHub |
Billing alerts are not a replacement for this: they can arrive hours late.
04Third: cap the damage for next time
Set spend alerts and a hard spend limit where the provider offers them. Restrict keys to what they need, because unrestricted keys are insecure. Give new keys an expiration date, and on OpenAI prefer a project service account to one person's key for an application. To rotate API keys on a schedule, you repeat the steps above before the date comes. Regular rotation limits the impact of a future leak. On Google Cloud, a rotation means creating a new key with the same restrictions and updating your applications to use it.
05Fourth: clean the history, after the key is dead
Only now is it worth rewriting Git history. A new commit that deletes the key does not help, because the key stays in the old commits. For a secret, revoking or rotating is the first step and may be enough on its own. Rewriting has costs: commit hashes change and forks keep the old commit. A force-push alone may leave the data reachable elsewhere. To find out who committed the secret, search the history with git log -S. The full procedure is in remove secrets from Git history.
06If the key left through your Mac
Not every leaked API key comes from a repository. On macOS, a backdoor can search the whole filesystem for credential and secret files. Then the Mac itself needs attention too, not only the key. To see whether something on the Mac itself started the leak, Artificial Immunity's free, open-source core watches program starts and reports what it finds.
07Which page to open next
If the key is in a public repository, open API key exposed on GitHub. For an OpenAI API key or an AWS access key, the provider pages list the exact checks. To stop the next leaked API key, read where to store API keys. Whatever the provider, write down which key leaked, where and when, so the next person to find one in this project knows what to look at.
FAQ
What happens if an API key is leaked?
Automated scanners can find publicly exposed secrets in minutes, and malicious actors can exploit them within hours. With a secret key they can make unauthorized charges, read customer data or disrupt your integration.
What can hackers do with my API key?
Whatever the key was allowed to do, on your account and at your cost. That is why the first move is to revoke it, not to hunt for who took it.
Why is it bad to leak an API key?
A key is account credentials, like a username and password. A leaked key lets someone else spend your money. The what-is page walks through one real bill.
What does it mean to rotate API keys?
You create a replacement, update your applications to use it, then revoke the old key once the replacement works. The steps are the same on Google Cloud and end with deleting the old key.
Do API keys need to be rotated?
Yes, regularly, to minimize the impact of any leak. An expiration date on project keys and a regular rotation process are strongly recommended.
How often should you rotate your API keys?
Set it by key lifetime: choose an expiration date when you create the key. After a leak, rotate at once, whatever the schedule.
What do I do with my API key?
Keep it out of code. Pass it through an environment variable or a secrets manager. The where to store guide shows how on a Mac.
Related guides
Sources
- GitHub docs, remediating a leaked secret
- GitHub docs, removing sensitive data from a repository
- GitHub docs, token expiration and revocation
- OpenAI docs, production best practices
- AWS IAM docs, updating access keys
- AWS re:Post, potential account compromise
- Google Cloud docs, API keys
- Stripe docs, API key best practices
- GitGuardian, State of Secrets Sprawl 2025
- Cursor, extension security notice
- Microsoft Security blog, Contagious Interview
- Artificial Immunity