You leaked an API key: the first hour
A secret was pushed to GitHub, posted in a chat or shipped to the browser. What to do first, what doesn't help, and how to stop it happening again.
Nov 15, 2026 · 2 min read
You've just realised a secret key was pushed to a public repository, pasted into a chat, or shipped in your JavaScript. Here's the order to do things in.
1. Rotate it now
Don't start by cleaning up git history. Assume the key is already being used, because automated scanners find public keys fast.
Go to the provider's dashboard, create a new key, put it in your .env (and your server's), deploy, then revoke the old one. In Laravel, run php artisan config:cache after changing .env on the server, or the app keeps using the cached old value.
If you can't rotate immediately, revoke first and accept a few minutes of downtime. Downtime is cheaper than the alternative.
2. Check what it was used for
Most providers show recent activity per key: AWS CloudTrail, Stripe's request logs, your mail provider's sending logs, GitHub's security log. Look for anything from the moment it leaked until you revoked it:
- cloud resources you didn't create (check every region)
- refunds, charges or customer data exports
- mail or SMS you didn't send
- pushes, new tokens or new deploy keys on GitHub
If something was used, treat it as an incident: the systems it touched, and the data in them, may need more than a key rotation.
3. Find out how far it spread
Search for the old value everywhere you can: other repositories, CI variables, other servers, teammates' .env files. A key that leaked once was often copied to more than one place.
What doesn't help
- Deleting the commit or force-pushing. The key has already been seen. Forks, clones and caches keep it anyway.
- Making the repository private. Same reason.
- Hoping nobody noticed. Bots did.
Rewriting history is fine as tidying up after the key is revoked. It is not a fix.
Stop the next one
- Turn on GitHub secret scanning and push protection for your organisation.
- If you use Lockhound, turn on its secret check too (paid plans, off by default). It alerts when a key appears on your default branch and fails a pull request that adds one. Its advice is the same as step 1: revoke or rotate first, then remove the key from the code.
- Add a secret scanner such as
gitleaksto CI, and to a pre-commit hook if your team will live with it. - Keep
.envin.gitignorefrom the first commit, and never give a secret a real default inconfig/*.php. - Use separate keys per environment and per app, so one leak means rotating one key, not all of them.