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

How to check your own site

  1. Open your live site, then DevTools → Sources (or Network → JS files).
  2. Search all files for sk- and sk-proj-. OpenAI secret keys start with these prefixes.
  3. Also search for other secrets: sk_live_ (Stripe), AKIA (AWS), ghp_ (GitHub), and service_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

  1. 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.
  2. Check usage in the OpenAI dashboard for spend you don't recognize, and set a monthly budget limit.
  3. 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.
  4. Add rate limiting and auth to that route so it can't become a free proxy for strangers.
  5. Redeploy and re-check the bundle to confirm the new key is not in it.

Prevent it next time

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.

Run the free Launch Check or start daily monitoring

More guides