Leaked OpenAI API key in your JavaScript bundle: how to find and fix it
If your app calls the OpenAI API directly from the browser, your secret key is shipped to every visitor. Anyone can open DevTools, copy it, and run up your bill. This is one of the most common mistakes in AI-generated front-end code.
How keys end up in the bundle
- The key is read from an environment variable with a public prefix, such as
NEXT_PUBLIC_,VITE_orREACT_APP_. Build tools inline those values into client JavaScript by design. - The key is pasted straight into a component to "just get it working".
- The OpenAI SDK is used in client code with
dangerouslyAllowBrowserturned on.
How to check your own site
- Open your live site, then DevTools → Sources (or Network → JS files).
- Search all files for
sk-andsk-proj-. OpenAI secret keys start with these prefixes. - Also search for other secrets:
sk_live_(Stripe),AKIA(AWS),ghp_(GitHub), andservice_role(Supabase).
The free Launch Check below runs these searches across your public JavaScript automatically.
If you find a key: fix it in this order
- Revoke the key now in the OpenAI dashboard (API keys) and create a new one. Removing it from the code is not enough: the old bundle may be cached, archived or already copied.
- Check usage in the OpenAI dashboard for spend you don't recognize, and set a monthly budget limit.
- Move the call server-side. Create an API route or serverless function that holds the key in a non-public env var and calls OpenAI. Your front end calls your route, never OpenAI directly.
- Add rate limiting and auth to that route so it can't become a free proxy for strangers.
- Redeploy and re-check the bundle to confirm the new key is not in it.
Prevent it next time
- Never give a secret a public prefix. Public prefixes are only for values that are safe for anyone to see.
- Add a secret scanner to your repo (for example GitHub secret scanning or gitleaks).
- Re-scan your live site after each deploy. AI tools regenerate code and can reintroduce the same leak.
Check your site free
The free FlowSnitch Launch Check loads your public pages and JavaScript, looks for leaked keys, a public .env or .git folder, and missing security headers, then gives you a grade and a fix list. Read-only, about 30 seconds, no account needed.