API Keys in the Frontend: How to Find Them and What to Do Next
Some keys belong in the browser and some never should. Learn to tell them apart, find leaked ones in your built JavaScript, and rotate them the right way.
๐ก The short version: anything in your frontend JavaScript is public. Some keys are made for that (a publishable key, limited by rules on the server). Others, like a payment secret key or a database admin key, are not, and finding one in your bundle is an emergency. Find it, replace it, rotate it.
๐ Two kinds of keys
โ Public (browser-safe)
- Examples: Supabase publishable (anon) key, Stripe publishable key, Firebase web config, a Maps key restricted by referrer.
- If a stranger copies it: it is limited by the rules you set on the server.
- Where it lives: the browser is fine.
๐ซ Secret (server only)
- Examples: Supabase secret (service_role) key, Stripe secret key, OpenAI or Anthropic API keys, email provider keys, private keys.
- If a stranger copies it: they act as you, with your money and your data.
- Where it lives: a server function or environment variable.
Supabase puts it plainly: the publishable key is safe to expose online, while the secret key must never appear in a browser, a web page or shipped source code, even on localhost.
๐ฌ How keys end up in the bundle
- A variable with a
VITE_,NEXT_PUBLIC_or similar prefix is included in the browser build on purpose. Put a secret behind that prefix and you've published it. - An AI assistant pastes a key straight into a component to make something work.
- A
.envfile or a source map gets deployed along with the site.
๐ Find them in five minutes
- Open your live site and look at the JavaScript files it loads (the Sources or Network tab in your browser's developer tools).
- Search each file for these shapes:
sk_live_orrk_live_(Stripe secret or restricted)sb_secret_, or a long token whose payload contains"role":"service_role"AKIAfollowed by 16 characters (an AWS access key id)-----BEGIN PRIVATE KEY-----sk-ant-orsk-followed by a long string (AI providers)xoxb-or ahooks.slack.com/services/address (Slack)postgres://user:password@(a database address with a password in it)
- Check whether a
.mapfile sits next to each script. Source maps show your original code, including whatever you thought was hidden.
๐จ Found one? Do these in order
- Rotate the key first. Create a new one in the provider's dashboard and revoke the old one. Removing it from your code doesn't help, because anyone who already saw it still has it.
- Move the new one to the server. Call the provider from a server function and keep the key in an environment variable.
- Check usage. Look at the provider's logs for the period the key was public.
- Redeploy and confirm the old bundle is no longer being served.
๐ค What a scanner looks for
A scan of your public site reads the page and its scripts for key formats that are almost never false alarms: AWS, Google, Stripe live secret and restricted keys, Slack, GitHub, OpenAI, Anthropic, SendGrid, private keys, and database addresses with a real password. It doesn't guess at random-looking strings, and it never shows you the whole key back, only a redacted trace so you know where to look.
Building with an AI tool? Our list of ten mistakes AI-written code keeps making covers this one and nine more.
๐ Let a scan read your bundle for you. Check your site free and see whether a key is sitting in your public JavaScript.