Guide · Keys
I leaked an API key in my frontend. What now?
Everything in your website's JavaScript is public. If a secret key ended up there, anyone can open the browser's developer tools and copy it, and bots scan for exactly this. Here is what to do, in order.
1. Rotate the key first
Do this before anything else. Deleting the key from your code does not help: it has already been served to visitors, cached, and possibly copied. The only fix is to make the old key stop working.
- Stripe: Developers → API keys → roll the secret key (
sk_live_…) and set the old key to expire now, not later. Also roll any restricted key (rk_live_…) that leaked. - OpenAI, Anthropic, OpenRouter and other AI APIs: open the API keys page, revoke the leaked key and create a new one.
- Supabase
service_role/sb_secret_: in Project Settings → API Keys, create a new secret key, switch your functions to it, then delete the old one. If your project still uses the olderservice_rolekey (a long string startingeyJ), create new publishable and secret keys, switch your app and functions to them, then disable the legacy keys on the same page. - AWS: deactivate the access key in IAM, then delete it.
2. Check whether someone already used it
- Stripe: look through recent payments, refunds, payouts and Developers → Logs for anything you didn't do.
- AI APIs: check the usage page for a spike, and set a monthly spending limit so a future leak can't run up a large bill.
- Supabase: look for new or changed rows you don't recognise, especially in user and payment tables. Check Logs (API Gateway) for unusual requests around the time of the leak.
3. Move the call to a server
A secret key belongs on a server your users can't see. The frontend asks your server, the server calls the API with the key. With Supabase, the usual place is an Edge Function:
supabase secrets set STRIPE_SECRET_KEY=sk_live_...
Then read it inside the function with Deno.env.get("STRIPE_SECRET_KEY"), and call the function from your app. If you build with Lovable or Bolt, you can ask for exactly this: "Move every call that uses the Stripe secret key into a Supabase Edge Function, read the key from a secret, and remove it from the client."
Watch the names of your environment variables. Anything starting with VITE_, NEXT_PUBLIC_, EXPO_PUBLIC_, REACT_APP_ or PUBLIC_ is put into the browser bundle on purpose. A secret key must never have one of those prefixes.
4. Clean up the rest
- Remove the old key from your code and environment files, even though it no longer works.
- If the repository is public, the key is in its history too. Rotating already made it useless; rewriting history is optional.
- Turn off source maps in production if they're on. They hand out your original source code, comments and all.
Which keys are meant to be public?
Not every key in the browser is a leak. These are designed to be there, and are protected another way:
- Supabase
anon/sb_publishable_: safe, as long as Row Level Security is right. - Stripe publishable key (
pk_live_…): safe by design. - Firebase web config
apiKey: identifies the project; your Security Rules protect the data. - Google Maps and similar browser keys: fine if restricted to your domain in the provider's console. Unrestricted ones can be used on anyone's site, on your bill.
5. Check again
After the fix is live, check the site from the outside to confirm the key is gone from what visitors get. Greenlit recognises 32 kinds of secret keys in the JavaScript your site sends, and tells you which ones are serious.
Find out what your site gives away
Paste your app's address. Greenlit reads only what any visitor's browser gets, grades it from A to F, and writes a fix prompt for each problem. Free, no sign-up.
Check your appMore guides