The short answer
Search the files in your project and every old commit for key patterns, with a free tool like Gitleaks. Any secret key you find there, or in your browser code, counts as leaked. Replace it at the provider first, then clean up the code.
Where do API keys leak in apps built with AI?
AI builders write working code fast, and they often put the key right where it is used. Three places catch most people out:
- In the code itself. A line like
const stripe = new Stripe("sk_live_...")puts the key in every copy of the repo. - In a committed
.envfile. A.envfile is meant to stay on your machine. If it was committed before.gitignorelisted it, it is in the repo. - In the browser code. Vite sends any variable that starts with
VITE_to the browser. Next.js does the same withNEXT_PUBLIC_. A secret key with one of those names is public, even in a private repo.
How do I ask my AI builder to check?
Your AI builder can read the whole project at once. Paste this prompt into Lovable, Bolt, Cursor or Claude Code:
Search this whole project for API keys, tokens, passwords and other secrets written directly in the code or in committed files such as .env. For each one, give the file and line, and say whether it ends up in the code sent to the browser. Do not print any full secret: show only its first 8 characters. Do not change anything yet.This only checks the files as they are now. It does not see old commits, so do the next check too.
How do I search every old commit?
Gitleaks is a free, open-source tool that knows the patterns of many common key types. It reads your git history, so it finds keys you deleted long ago.
- Install Gitleaks. On a Mac with Homebrew, run the first line below. The Gitleaks README on GitHub lists other ways to install it.
- Open a terminal in your project folder and run the second line.
- Read each finding. Gitleaks shows the file, the line, the commit and who made it.
brew install gitleaks
gitleaks git -vTo look for one kind of key by hand, ask git which commits added or removed it. This finds every commit that touched a Stripe live secret key:
git log --all -p -S "sk_live_"Which keys are safe to be public?
Some keys are made to sit in browser code. Others must never leave your server. Look at the start of the key:
- Safe in the browser: Stripe publishable keys (
pk_live_,pk_test_), the Supabase anon key and keys that start withsb_publishable_. Google Maps and Firebase browser keys are public too, but lock them to your domain in the Google Cloud console. - Secret: Stripe secret and restricted keys (
sk_live_,rk_live_), Supabase service_role keys and keys that start withsb_secret_, OpenAI keys (sk-), Anthropic keys (sk-ant-), AWS access keys (AKIA) and GitHub tokens (ghp_,github_pat_).
I found a key. What now?
Treat the key as stolen, even if you think nobody saw it. Bots search public code for keys all day.
- Replace the key at the provider first. This makes the old key useless, wherever it was copied to.
- Put the new key in your host's secrets or environment variables, on the server side only. Never paste it into code.
- Remove the old key from the code. Add
.envto.gitignore. If.envwas already committed, rungit rm --cached .envso git stops tracking it. - Check the provider's usage, logs and bill for anything you did not do.
Common questions
- Does deleting the key from my code remove the leak?
- No. Git keeps every old version of every file, so the key stays in your history. Anyone who can see the repo can find it. Replace the key at the provider so the old one stops working.
- Is my key safe if my repo is private?
- Safer, but not safe. Everyone with access to the repo, and every tool connected to it, can read the key. And if the key is in your frontend code, every visitor gets it, because that code is sent to their browser. Keep secret keys out of the repo either way.
- Is the Supabase anon key a secret?
- No. Supabase made the anon key, and the newer publishable key, to be public. Your data is safe only if row level security is on for every table. The service_role key and secret keys must never leave your server.