Bologu

API key leaked in your website: how to check, and what to do right away

Updated · by the Bologu team

Ask an AI to “connect OpenAI” and the most common result is code that calls OpenAI straight from the web page, with the key embedded in it. The app works right away, but everyone who opens the site gets your key too. Bots scan for keys around the clock, and leaked keys often run up huge bills within days.

How keys end up in your web page

  • Variables with a public prefix: NEXT_PUBLIC_, VITE_, REACT_APP_, EXPO_PUBLIC_ are compiled into the JavaScript sent to every browser. Name a variable like this and anyone can read its value.
  • Keys written straight into the code: the AI puts them there “temporarily” and nobody removes them.
  • Source maps left on: .map files reveal your original source code, including comments and values no one should see.
  • .env files committed to GitHub: especially in public repos.

Which keys may be in your web page, and which may not

Not every key you see in a page is a problem; some are designed to be public:

  • Fine in a web page: the Stripe publishable key (pk_live_…), the Supabase publishable / anon key (if RLS is set up correctly), Firebase web config, and a Google Maps key restricted to your domain.
  • Never in a web page: the Stripe secret key (sk_live_…), OpenAI (sk-…), Anthropic, the Supabase secret / service_role key, GitHub tokens, keys for SMS or email services, and database passwords.

A simple rule: if a key can spend money, read all your data or skip permission checks, it belongs on the server only.

Check for leaked keys yourself

  1. Open your site in Chrome, press F12 and go to the Sources tab.
  2. Press Ctrl+Shift+F (Mac: Cmd+Option+F) to search all files, and try each of: sk-, sk_live, service_role, secret, api_key.
  3. Check whether files ending in .map can be loaded. If so, turn off source maps in production.
  4. On GitHub, search your repo for the same words and make sure there is no .env file in it.

If a key has leaked, do this in order

  1. Revoke or rotate the key at the provider first: open the provider’s API keys page and click Revoke or Roll. Deleting the key from your code alone doesn’t help; whoever has it can still use it.
  2. Check past usage: open the Usage or Billing page for activity that isn’t yours. If you find some, contact the provider right away.
  3. Set a spending cap: set a monthly limit and email alerts on the Billing page.
  4. Move the call to the server: have the page call your own API (for example /api/chat), and let the server call OpenAI with a key kept in an environment variable without a public prefix.
  5. Limit usage on the server: require login, rate-limit each user and set max_tokens; otherwise people can still use your API to get free AI.
  6. Clean git history if the key was committed: use git filter-repo or BFG, but treat the key as leaked even after cleaning. That’s why step 1 matters most.

A prompt for your AI

My frontend calls the OpenAI API directly with the key in browser code.
1. Move every AI call to a server route (e.g. /api/chat) that reads the key from a
   server-only environment variable (no NEXT_PUBLIC_ / VITE_ prefix).
2. In that route: require login, rate-limit per user, cap input length and set max_tokens.
3. Remove the key from all client code and .env files that are bundled for the browser.
Do not print or log the key anywhere.

Let Bologu check it

The website scan (free) looks for keys from 20+ common services in every JavaScript file and source map your pages load, and tells you which keys are fine to expose and which must be replaced now. The GitHub code scan finds committed keys and .env files that shouldn’t be in your repo.