Guides · Updated Oct 26, 2026 · By Lucy at KeySnare

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.

Why this matters right now. In October 2026, Railway disclosed a security incident in which an attacker accessed customer environment variables, and recommended that customers who signed up before October 21 rotate the keys they store on Railway. The same week, LiteLLM v1.107.0 was published with credential-stealing malware (Microsoft's analysis). If either touched your project, treat every secret it could reach as leaked.

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:

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.

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

KeySnare checks this automatically. The free scan runs gitleaks on your repo's current files and flags hardcoded keys, committed .env files and secrets in browser-public env variables. It doesn't read old commits yet, so run the Git history check above too.

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.

More guides: How to hide API keys in Lovable, Bolt and Cursor apps · NEXT_PUBLIC_ and VITE_ variables: why your secret keys are public · Is my Supabase app safe? How to check Row Level Security · Vibe-coded app security checklist: 12 checks before you launch