Debug Ninjas
Secrets1 Oct 2026 · 3 min read

Your OpenAI key is visible to anyone. Here is how bots find it

API keys shipped in frontend code end up in the browser, where scrapers collect them within hours. Move them behind your server.

Admin

Founder, Debug Ninjas

You added an AI feature to your app. It works, users love it, and then your OpenAI bill arrives with usage you do not recognize. In most cases we see, the cause is the same: the API key was sitting in the website's code, and someone took it.

How the key ends up in public

Modern web apps send a bundle of JavaScript to every visitor's browser. Anything inside that bundle is public, even if it is "hidden" in an environment variable.

Frameworks make this easy to get wrong. Variables that start with VITE_, NEXT_PUBLIC_ or REACT_APP_ are deliberately copied into the browser bundle. When an AI tool sets up your OpenAI call, it often puts the key in one of those, because it is the fastest way to get the feature running.

Anyone can then open the browser's developer tools, look at network requests or the JavaScript files, and copy the key. They do not need to hack anything.

Who is looking

It is rarely a person poking around your site. Automated scrapers crawl public websites and code repositories looking for strings that match the format of known API keys. Once a key is found, it can be used to run the scraper owner's own AI workloads on your account, or resold.

The same applies to keys for Anthropic, Google, Stripe, Resend, Twilio and any other paid API. If the browser can see it, so can everyone else.

How to check your own app

  1. 01Open your live site in Chrome, right-click and choose Inspect.
  2. 02Go to the Network tab and use the AI feature. Click the request it makes. If it goes straight to api.openai.com from the browser, your key is exposed.
  3. 03In the Sources tab, search the JavaScript files for sk-. OpenAI secret keys start with that prefix.
  4. 04Check your code for environment variables with public prefixes that hold secret values.
  5. 05If your code is on GitHub, check whether the repository is public and whether a key was ever committed, even in an old commit.

How to fix it

  1. 01Rotate the key first. Create a new key in the OpenAI dashboard and delete the old one. Assume the old key is already in someone else's hands.
  2. 02Move the call to your server. Your frontend should call your own endpoint, such as an API route, a serverless function or a Supabase Edge Function. That endpoint holds the key and talks to OpenAI.
  3. 03Check who is calling. Your endpoint should confirm the user is logged in before spending money on their request, and limit how often one user can call it.
  4. 04Set spending limits. Use the usage limits and alerts in your OpenAI account so a leak can never turn into an open-ended bill.
  5. 05Keep secrets out of Git. Store them in your hosting provider's environment settings, and add .env files to .gitignore.

What it costs if you skip it

The obvious cost is the bill. The less obvious ones are that your provider may suspend the account, which takes your AI feature offline for real users, and that a stolen key tied to your account can be used for content that violates the provider's policies.

The short version

  • If a key is in the browser, it is public. Environment variable names do not change that.
  • Rotate exposed keys, move AI calls behind your own server, and set spending limits.
  • Check every paid API, not just OpenAI.

Finding exposed secrets across your code, repository and live site is one of the first things we do in a Ninja Audit. Book an Audit if you want it checked properly.

Written by Admin

Founder of Debug Ninjas. Has audited dozens of AI-built apps and still double-checks who can see what data.

One useful fix a month.

Field Notes in your inbox. No spam, unsubscribe anytime.