Guides · Updated 2026-10-21
I leaked my API key. What now? A step-by-step fix for vibe-coded apps
Short answer: Treat the key as compromised and replace it: create a new key, switch your app to it, then revoke the old one in the provider's dashboard. Deleting the key from your code is not enough, because it stays in your git history, old deploys and any copy of your JavaScript someone already downloaded. Then check the provider's usage and logs for activity you don't recognize, and fix the root cause: secret keys belong only in server-side code (an API route, server function or edge function), never in a VITE_ or NEXT_PUBLIC_ variable or anything the browser loads. Publishable keys (Stripe pk_, Supabase anon or sb_publishable_) are meant to be public and don't need rotating.
First: is it actually a secret key?
Some keys are designed to sit in your frontend. Seeing one of these in your JavaScript is normal and not a leak:
- Supabase publishable key (sb_publishable_...) or the legacy anon key. Supabase says it is safe to expose; Row Level Security decides what it can reach (https://supabase.com/docs/guides/api/api-keys).
- Stripe publishable key (pk_live_... / pk_test_...). Stripe lists it as safe to expose; it can't create charges or read account data (https://docs.stripe.com/keys).
These are secret and must be treated as leaked if anyone outside your servers could see them:
- Supabase secret key (sb_secret_...) or the legacy service_role key. It bypasses every Row Level Security policy, so it gives full access to your data.
- Stripe secret (sk_...) and restricted (rk_...) keys.
- OpenAI, Anthropic, Google AI and other paid AI API keys. Someone else can run requests on your bill.
- Email/SMS keys (Resend, SendGrid, Twilio), database connection strings, webhook signing secrets, and any key whose provider calls it secret.
Why deleting it from your code isn't enough
Once a key has been in a public JavaScript file or a pushed commit, you can't take it back: it is in your git history, in previous deployments, in caches and in any copy someone already saved. GitHub's own docs say that for a leaked secret the first step is to revoke or rotate it, and that once rotated it can no longer be used, which may make rewriting history unnecessary (https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository).
Step by step
- Find where it leaked and stop the bleeding: if it's in frontend code or a VITE_ / NEXT_PUBLIC_ variable, plan to move that call to the server (step 5). Supabase recommends fixing the root cause before you rotate, so the new key doesn't leak the same way.
- Create a new key in the provider's dashboard.
- Put the new key where only your server can read it: your host's environment variables or secrets (for example Vercel project settings, Supabase Edge Function secrets), without a public prefix. Redeploy.
- Revoke or delete the old key once everything uses the new one, then check that the old key really stops working.
- Move any frontend code that used a secret key behind a server route or edge function that checks who is calling, and add rate limits so a stranger can't use your endpoint as a free proxy.
- Check the provider's usage, billing and logs for activity you don't recognize, and contact their support if you see any.
Provider-specific steps (from their docs)
- Supabase secret / service_role key: create a new secret key in Settings > API Keys, replace the old key everywhere, confirm every component uses it, then delete the compromised secret key (this can't be undone). For a legacy service_role key, deactivate the legacy keys in the same section; that step is reversible. Source: https://supabase.com/docs/guides/api/api-keys
- Stripe secret or restricted key: on the API keys page, open the key's menu and choose Rotate key. Pick an expiration: "Now" deletes the old key immediately; otherwise both keys work for up to 7 days so you can switch without downtime. For a compromised key, don't wait long. Stripe also recommends restricted keys and access policies for live keys. Source: https://docs.stripe.com/keys
- OpenAI: if you believe a key leaked, rotate it right away from the API Keys page, update your app, review the Usage page, and set monthly spend limits with hard enforcement; OpenAI notes spend alerts alone don't stop traffic. Source: https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety
- Other providers: the pattern is the same. Create a new key, switch, revoke the old one, check usage.
If the key was in a GitHub repo
Rotate first; that is what actually protects you. Rewriting history with git-filter-repo is optional and has side effects: forks and existing clones keep the old commits, commit hashes change, and collaborators have to re-clone. Then add .env to .gitignore and turn on GitHub secret scanning push protection so the next key gets blocked before it is pushed.
A prompt for Lovable, Bolt or Cursor
A secret API key (<provider>) was exposed in my frontend code. I've already created a new key. Please: (1) remove every use of the secret key from client-side code and from any VITE_ or NEXT_PUBLIC_ variable; (2) move those calls into a server-side function (API route or Supabase Edge Function) that reads the key from a server-only environment variable, checks that the caller is signed in, and rate-limits requests; (3) keep publishable keys (Supabase anon/sb_publishable_, Stripe pk_) in the frontend as they are; (4) list every file you changed and every environment variable I need to set on the server. Don't print the key and don't put it in any file.
Short checklist
- New key created and stored server-side only (no VITE_ / NEXT_PUBLIC_ prefix).
- Old key revoked or deleted, and tested to confirm it fails.
- Usage and billing checked; spend limits set where the provider offers them.
- Frontend no longer calls the provider with a secret key; a server route does, with auth and rate limits.
- .env in .gitignore and secret scanning on.
Not sure what your live site exposes? The free LeakyVibe scan above looks at your public pages and JavaScript, read-only, in about 15 seconds and flags secret-looking keys, Supabase tables that answer to your public key, and missing security headers. It's a quick first check, not a full audit, and it can't see keys that only live in a git repo.