Common question

Is My OpenAI API Key Safe in My App?

Short answer: not if it's in your front-end code. OpenAI says never to put your key in a browser or mobile app. If your app calls OpenAI straight from the page, anyone can pull the key out and run requests on your bill. Move the calls to a server and keep the key there.

Check your live app for free

Paste your link. The free summary usually takes under a minute.

Why this comes up

You ask Lovable, Bolt, Cursor or a similar tool to "add an AI chat" or "summarize this with ChatGPT." A simple way to make that work is to call OpenAI right from the page, with the key pasted in. It works on day one. But the page's code is sent to every visitor's browser, and the key goes with it.

What OpenAI says

OpenAI's key-safety page is clear: "Never deploy your key in client-side environments like browsers or mobile apps."

And: "Exposing your OpenAI API key in client-side environments like browsers or mobile apps allows malicious users to take that key and make requests on your behalf – which may lead to unexpected charges or compromise of certain account data. Requests should always be routed through your own backend server where you can keep your API key secure."

(OpenAI Help: Best practices for API key safety)

A "backend server" just means your own server code (for example a Supabase Edge Function or a server API route) that holds the key and calls OpenAI for your app. The browser talks to your server, never to OpenAI with your key.

"But my env variable is hidden, right?"

Not always. Putting the key in an environment variable keeps it out of your code files. But in front-end tools like Vite or Next.js, some variables are copied into the code that goes to the browser.

Vite's docs say: "Variables prefixed with VITE_ will be exposed in client-side source code after Vite bundling." They also say: "VITE_* variables should not contain sensitive information such as API keys. The values of these variables are bundled into your source code at build time." (Vite: Env Variables and Modes)

Next.js lists this as a built-in feature: "Bundle environment variables for the browser by prefixing with NEXT_PUBLIC_". And: "these public environment variables will be inlined into the JavaScript bundle during next build." (Next.js: How to use environment variables)

If the page calls OpenAI directly, the key has to be in the browser for that to work, so it's public no matter where you stored it.

How to check your app

  1. Ask your AI builder. Paste this:
Find every place my app uses an OpenAI API key. For each one, tell me: is the key used in code that runs in the browser, or only in server code (an Edge Function or server API route)? Check environment variables that start with VITE_ or NEXT_PUBLIC_ too. Don't change any code yet.
  1. Run the free scan. Paste your URL in the box at the top. It looks for keys that look like OpenAI keys in the HTML tags on the page (including tag attributes), the inline scripts on that page, and up to 15 script files from your own site. More on what it can and can't see is below.

How to fix it

If the key is in your front-end code:

  1. Move the OpenAI calls to a server. Paste this:
Move all OpenAI API calls out of my front-end code into a server function (a Supabase Edge Function or a server API route). Store the OpenAI key as a server-only secret, not in any VITE_ or NEXT_PUBLIC_ variable. Have my app call my server function instead, and only let logged-in users call it. Then show me that no OpenAI key is left in any file that runs in the browser.
  1. Treat the old key as leaked. Make a new key, update your server secret, check the app works, then revoke the old key. OpenAI says: "We strongly recommend creating keys with an expiration date and establishing a regular key rotation process." And: "Before a key expires, create a replacement, update your applications to use it, and revoke the old key once you've verified that the replacement works."
  1. Put a cap on spend. OpenAI's Spend limits guide says a spend alert "Sends a notification; API traffic continues", while a hard spend limit means "Affected API requests return a 429 error". To set one for a project: go to Project settings, select Limits, then under Spend select Edit spend limit, enter the Monthly spend limit, turn on Enforce a hard limit, and select Save. OpenAI notes: "Enforcement is not instantaneous, so recorded spend can slightly exceed the configured amount." See also How do I stop a surprise bill on my vibe-coded app?

(OpenAI: Spend limits)

Things to watch for

  1. Don't put the key in Git. OpenAI says: "Committing an API key to source code is a common vector for credential compromise."
  2. Don't share one key between people. OpenAI says: "OpenAI does not support the sharing of API keys."
  3. Your server function needs limits too. Once OpenAI runs through your server, anyone who can reach that function can still run up costs. Require a login and limit how often each user can call it. See Limit how often people can try to log in.

What the rooGBotVC scan checks

Paste your URL in the box at the top. The scan reads the HTML tags on the page you scan (including tag attributes), the inline scripts on that page, and up to 15 script files from your own site.

  1. If it finds a key that looks like an OpenAI key, it shows "OpenAI API key exposed in public code" (critical). It looks for keys that start with sk-proj-, sk-svcacct- or sk-admin-, and for the older sk- pattern that includes T3BlbkFJ. Found keys are shown with most of the key hidden.

What the scan can't do:

  1. It doesn't run your JavaScript, so a key that only shows up after the page runs, or that is loaded from another site, may be missed.
  2. If your OpenAI key looks different from the patterns above, the scan may not catch it.
  3. It can't see your OpenAI account settings, so it can't tell whether a key has an expiration date, or whether a spend limit is on.
  4. It never uses or tests any key it finds.
  5. It doesn't test logins, rate limits, billing or RLS.

Related reading

  1. Exposed API keys: what to do when any key leaks.
  2. Is my Gemini API key safe in my app?: the same idea for Google's Gemini key.
  3. Is my Claude API key safe in my app?: the same idea for an Anthropic Claude key.
  4. Is my Grok (xAI) API key safe in my app?: the same idea for an xAI Grok key.
  5. How do I stop a surprise bill on my vibe-coded app?: budgets and alerts.

The bottom line

An OpenAI key in front-end code is a public key, and OpenAI says not to do it. Move OpenAI calls to your own server, make a new key, revoke the old one, and set a spend limit. Then paste your URL in the box at the top to check what your app shows the public.

This is an automated, surface-level check of what your site shows the public. It is not a full security audit.