Leaked API keys in JavaScript: how to find and fix them
Updated 5 December 2026
Everything your website sends to the browser is public: HTML, CSS and every JavaScript file. If a secret API key is in there, anyone can copy it in a minute — and bots that scan sites for keys do exactly that. AI coding tools make this easy to do by accident, because calling an API directly from the page "just works".
Which keys may be public and which must not
| Service | Fine in the browser | Never in the browser |
|---|---|---|
| Stripe | pk_live_…, pk_test_… | sk_live_…, rk_live_… (and test equivalents) |
| Supabase | anon key, sb_publishable_… | service_role key, sb_secret_… |
| Firebase | The web config (apiKey: "AIza…") — protected by security rules | Service account JSON / private keys |
| Google Maps | AIza… browser key restricted to your domains and APIs | Unrestricted keys |
| OpenAI | — | sk-proj-…, sk-… |
| Anthropic | — | sk-ant-… |
| AWS | — | AKIA… access keys with their secret |
| GitHub, Slack, SendGrid, Resend, Mailgun | — | All tokens and API keys |
An AI key in the browser is the most expensive mistake: anyone can run your OpenAI or Anthropic account up to its spending limit.
How to find leaked keys on your live site
- Open your site in Chrome, press F12, go to Sources and press Ctrl+Shift+F (Cmd+Option+F on Mac) to search all loaded files.
- Search for the prefixes in the table:
sk_live,rk_live,sk-proj,sk-ant,service_role,sb_secret,AKIA,ghp_,-----BEGIN. - Repeat on pages behind navigation — bundlers split code, and a key can sit in a chunk that only loads on the dashboard.
Or run a free VibeCleared scan: it downloads every JavaScript file the page loads and checks it for 13 kinds of secret keys. Leaked secrets are always shown in the free result, masked (first 8 and last 4 characters).
How keys get there
- Public env prefixes.
VITE_,NEXT_PUBLIC_,REACT_APP_andPUBLIC_variables are copied into the bundle at build time. They are for public values only. - Calling the API from the page. "Add an AI chat to my page" often produces a direct
fetchto the provider with the key in the header. - Hard-coded keys during testing that were never removed.
The right order to fix a leak
- Revoke or roll the key first in the provider's dashboard. Deleting it from the code does not help: it is already in deployed files, CDN and browser caches, and git history.
- Check usage and billing on the provider for anything you did not do, and set a spending limit.
- Move the call to the server — a serverless or edge function (Supabase Edge Functions, Vercel, Netlify or Cloudflare functions) that reads the new key from an environment variable. The page calls your function, never the provider.
- Add protection to that function: require a signed-in user or a rate limit, otherwise the function itself becomes a free proxy to your key.
- Redeploy and re-scan to confirm no file contains the key any more.
My frontend calls the OpenAI API directly with a secret key. Create a server-side
function that holds the key in an environment variable, calls OpenAI, and returns
only the result. Require a signed-in user and add a simple rate limit. Update the
frontend to call this function and remove every reference to the key from client
code. Tell me where to set the new environment variable.
Questions
If I delete the key from my code, am I safe?
No. Deployed files, caches and git history still contain it. Revoke or roll the key with the provider first, then remove it from the code.
Is a Stripe publishable key (pk_live) a leak?
No. Publishable keys are meant for the browser. Only secret (sk_) and restricted (rk_) keys must stay on the server.
Why does VibeCleared show leaked keys in the free scan?
Because a leaked secret key should never wait behind a paywall. The key is always masked, and the full value is never stored.
Related guides
- Is your Supabase data public? Check RLS in 5 minutes
- Supabase anon vs service_role: which key is safe in the browser
- Launch checklist for AI-built websites
Run all of these checks on your live site in under a minute — free, no signup.
Scan my site free