Leaked an API key? How to find, rotate and verify it (step by step)
Deleting a leaked key from your code doesn't fix anything. The key still works until you replace it at the provider. Here's how to find every exposed key, rotate it without breaking your app, and prove the old one is dead.
Step 1: Make a list of every secret
Before you rotate anything, write down every key, token and password your app uses and where each copy lives:
.env,.env.local,.env.productionand any config files- Your hosting dashboard's environment variables (Vercel, Railway, Netlify, Render, Fly)
- CI secrets (GitHub Actions, etc.) and any teammates' or AI agents' machines
- Third-party tools you pasted keys into (automation tools, AI coding agents, MCP servers)
A simple table works: provider · key name · where it's stored · what it can do · rotated?
Step 2: Find out which ones leaked
Current code
Search the repo for common key prefixes:
git grep -nE "sk_live_|rk_live_|sk-proj-|sk-ant-|AKIA[0-9A-Z]{16}|ghp_|github_pat_|service_role|BEGIN (RSA |EC )?PRIVATE KEY"
Full Git history (the part people miss)
A key you deleted last month is still in old commits. Anyone who clones a public repo gets them. Two ways to check:
# search every commit on every branch for a prefix
git log -p --all -S "sk_live_"
# or scan the whole history with gitleaks (free, open source)
gitleaks git -v . # older gitleaks versions: gitleaks detect -v
Also check GitHub's Security → Secret scanning tab on your repo if it's enabled.
The live site
Open your deployed app, open DevTools → Sources (or Network), and search loaded scripts for the start of each key. Anything you find there is public. See why NEXT_PUBLIC_ and VITE_ variables are public.
Platform or package incidents
If your host or a dependency was breached, you may not find the leak in your code at all. Treat every secret stored on that host, or readable by that package's environment, as leaked.
Step 3: Rotate in the right order
The safe pattern for every provider is the same: create new → update env vars → redeploy → confirm it works → revoke old. Revoking first takes your app down. Start with keys that can spend money or read user data.
- Stripe: Dashboard → Developers → API keys → Roll key on the secret or restricted key. Stripe lets you keep the old key working for a short window while you deploy. Roll webhook signing secrets too if they were exposed.
- OpenAI: API keys page in the OpenAI platform → create a new secret key → deploy it → delete the old key. Check Usage for spikes.
- Anthropic: Console → API keys → create a new key → deploy it → disable or delete the old one.
- Supabase: Project Settings → API keys. With the newer secret keys (
sb_secret_…) you can create a new one and delete the leaked one. A leaked legacyservice_rolekey means rotating the project's JWT secret, which also changes the anon key and signs users out, so plan a short maintenance window. Change the database password too if a connection string leaked. - AWS: IAM → your user → Security credentials → create a second access key → deploy it → deactivate the old key, confirm nothing breaks, then delete it.
- Google Cloud / Firebase: APIs & Services → Credentials → create a replacement key with API and app restrictions, deploy it, then delete the old key. Rotate service-account keys the same way.
- GitHub: Settings → Developer settings → Personal access tokens → delete or regenerate the token. Reconnect any app integrations that used it.
- Database passwords: change the password at your provider and update
DATABASE_URLeverywhere it's used.
Step 4: Verify the old key is dead
Don't assume. Test the old key directly. You should get an authentication error (usually HTTP 401):
# OpenAI: should return 401
curl -s -o /dev/null -w "%{http_code}\n" https://api.openai.com/v1/models \
-H "Authorization: Bearer OLD_KEY"
# Stripe: should return 401
curl -s -o /dev/null -w "%{http_code}\n" https://api.stripe.com/v1/balance -u OLD_KEY:
Then open each provider's usage, billing or audit logs and look for requests you didn't make, especially between the time of the leak and the time you rotated.
Step 5: Contain the next leak
- One key per app and per environment, so one leak doesn't force you to rotate everything.
- Keys stay on the server. Never in browser code or public env prefixes. See how to hide API keys in Lovable, Bolt and Cursor apps.
- Least privilege: use Stripe restricted keys and scoped tokens instead of all-powerful keys.
- Spend limits on AI and cloud accounts, so a stolen key can't run up an unlimited bill.
- Pin dependency versions and update deliberately, so a poisoned release doesn't land automatically.
- Turn on secret scanning (GitHub push protection, or gitleaks in CI) and rescan after every big AI-generated change.
Copy-paste prompt for your AI coding tool
Paste this into Cursor, Claude, Lovable, Bolt or whichever AI built your app:
List every API key, token, password and connection string this project uses, where each one is read from, and which provider issued it. Flag any that are hardcoded in source, committed in .env files, or exposed through browser-public env prefixes (NEXT_PUBLIC_, VITE_, REACT_APP_, EXPO_PUBLIC_). Move all of them to server-only environment variables, make sure no secret is sent to the browser, and give me a checklist of keys I must rotate at each provider, in order of risk (payments, AI, cloud, database first). Don't print any secret values.
Find leaked keys in your repo in about a minute
Paste a public GitHub repo into KeySnare for a free grade and your top 3 issues, including hardcoded keys and committed .env files, each with a copy-paste AI fix prompt.
Run a free scan →FAQ
Is deleting the key from my code enough?
No. Once a key has been in a public repo, a deployed bundle or a breached host, assume someone copied it. Create a new key, switch your app to it, then revoke the old one at the provider.
Do I need to remove the key from Git history?
Rotate first, because that is what actually stops abuse. Cleaning history (for example with git filter-repo) is optional tidy-up after the old key is revoked, and forks or clones may still have the old commits.
What order should I rotate keys in?
Start with anything that can spend money or reach user data: payment keys (Stripe), AI provider keys (OpenAI, Anthropic), cloud keys (AWS, Google Cloud), database passwords and service-role keys, then GitHub tokens and everything else.
How do I know the old key is really dead?
Call the provider's API with the old key and confirm you get an authentication error (usually HTTP 401). Then check the provider's usage or audit logs for activity you don't recognise.
Can rotating a key break my app?
Yes, if you revoke the old key before the new one is deployed. Add the new key to your hosting environment variables, redeploy, confirm the app works, and only then revoke the old key.