Scanora

Guides / General

Leaked API keys in your JavaScript - how to find and fix them

If a secret key is in your frontend code, anyone can copy it. How keys end up in the browser bundle, how to find them, and the right way to fix it (including rotation).

Updated 2026-11-18

Everything in your frontend is public. The JavaScript your site sends to the browser can be opened by anyone in their developer tools, and bots scan public sites and code for keys all the time. If a secret key is in there, assume someone already has it.

This is one of the most common problems in AI-built apps, because the quickest way to make an API call "just work" is to call the provider straight from the browser with the key.

Public keys vs secret keys

Not every key is a problem. Some are designed to be public:

  • Fine in the browser: Stripe publishable keys (pk_live_…), the Supabase anon / sb_publishable_ key, Firebase config, Google Maps keys restricted to your domain.
  • Never in the browser: Stripe secret keys (sk_live_…), OpenAI / Anthropic / Groq / OpenRouter keys, the Supabase service_role / sb_secret_ key, email service keys (Resend, SendGrid, Mailgun), database passwords, private keys, GitHub tokens.

How secrets end up in the bundle

Build tools copy certain environment variables into your JavaScript on purpose:

  • Vite: variables starting with VITE_
  • Next.js: variables starting with NEXT_PUBLIC_
  • Create React App: variables starting with REACT_APP_

If a secret has one of these prefixes, it's in every visitor's browser. Keys can also be pasted straight into code, or read from a config file that gets bundled.

How to find them

  1. Scan your live site. Load your site, open developer tools, and search the Sources panel for sk_, sk-, key, secret, token. Or let a scanner do it.
  2. Search your code for the prefixes above and for hard-coded strings that look like keys.
  3. Check your git history. A key removed from the code is still in old commits.

How to fix it properly

Step 1: Rotate the key

If a secret was ever public, revoke it and create a new one in the provider's dashboard. Removing it from the code isn't enough; the old key still works for whoever copied it.

Step 2: Move the call to the server

Put the code that needs the secret in a server-side function, for example a Next.js Route Handler or Server Action, a Supabase Edge Function, a Netlify or Vercel Function, or your own backend. It reads the key from a server-only environment variable (no VITE_ / NEXT_PUBLIC_ prefix). Your frontend calls your function, never the provider directly.

// Before (browser): the key is public
const res = await fetch('https://api.openai.com/v1/responses', {
  headers: { Authorization: `Bearer ${import.meta.env.VITE_OPENAI_KEY}` },
  /* … */
})

// After (browser): calls your own server route instead
const res = await fetch('/api/generate', { method: 'POST', body: JSON.stringify({ prompt }) })

Step 3: Protect the new endpoint

Your server route is now the thing that spends money. Require login where it makes sense and add a rate limit, so a script can't use your route as a free AI proxy.

Step 4: Restrict what can be restricted

Some providers let you limit a key to certain domains, IP addresses, endpoints or a spending cap. Use those limits even for keys that are meant to be public.

Also check source maps

Production source maps (.js.map files) publish your original code, comments and all. Turn them off for production builds:

  • Vite: build.sourcemap: false
  • Next.js: don't enable productionBrowserSourceMaps

Check your site now

Scanora scans your live site's JavaScript for leaked keys from dozens of services, including AI providers, Stripe, Supabase and email services, and checks for exposed source maps. Keys are always masked in the results.

Check your site in one minute

Scanora runs these checks for you: leaked keys, open files, headers, SSL, speed and AI-search readiness. Free, no signup.

Scan my site free

More guides