How do I keep my OpenAI key out of my front end?

Short answer: you can't hide a secret key in code that runs in the browser. Move the call to a server or an edge function, keep the key there, and let the page talk only to your own endpoint.

Why a key in the browser is always public

Everything your page uses has to be downloaded by the visitor's browser first: the HTML, the JavaScript bundle and every value baked into it. Anyone can open the browser's developer tools, look at Sources or the Network tab, and read it. Minifying or splitting the code makes it harder to read for a person, not for a search. Keys are long, oddly shaped strings, and they're easy to find with a simple text search.

So the question isn't how to hide the key in the front end. It's how to make sure the front end never needs it.

How AI-built apps usually leak keys

The quick version that works in the preview

Ask an AI builder to "add a chat feature with OpenAI" and the fastest working answer is often a call to the OpenAI API straight from the page, with the key pasted next to it. It works in the preview, so it ships. Every visitor now has a key that bills your account.

Public environment variables

Moving the key into an environment variable feels safer, but the name decides where it ends up. Vite's docs say variables starting with VITE_ are bundled into the client code and should not contain secrets such as API keys. Next.js's docs say the same of NEXT_PUBLIC_: the value is inlined into the JavaScript sent to the browser at build time. A secret behind either prefix is as public as a secret typed into the page.

Leftovers

Keys also survive in places you've stopped looking: an old test page still deployed, a debug log in the console, a source map that contains the original code, or a config file the build copied into the public folder.

The fix: one small server-side step

The pattern is the same for OpenAI, Anthropic, Stripe and almost any paid API:

  1. Create a server-side function. In a Supabase project that's an Edge Function; in Next.js a Route Handler; on most hosts a serverless function.
  2. Store the key as a server secret. Supabase's Edge Function secrets docs show how to set it in the dashboard or the CLI and read it inside the function. In Next.js, a variable without the NEXT_PUBLIC_ prefix stays on the server.
  3. Let the page call your function, not the provider. The browser sends the user's message to your endpoint; your function adds the key, calls the provider and returns only the answer.
  4. Protect the function. A public endpoint that forwards anything to OpenAI is a free AI service for strangers, paid by you. Require a signed-in user, limit how often each user may call it, cap the size of the input, and set a monthly spending limit with the provider.
  5. Replace the old key. If the key was ever in front-end code, a public repository or a deployed build, treat it as leaked: create a new one, update the server secret, and delete the old one in the provider's dashboard. Removing it from the code doesn't remove it from copies people already downloaded.

A prompt you can give your builder: "Move every call to OpenAI into a server-side function. Store the API key as a server secret, never in front-end code or a VITE_ or NEXT_PUBLIC_ variable. Only signed-in users may call the function, with a limit per user per hour."

Which keys are fine in the browser

Some keys are designed to be public: a Supabase publishable (anon) key, a Firebase web API key, a Stripe publishable key (pk_), most analytics ids. They identify your project, and what they can do is limited by rules on the provider's side, like Row Level Security or Firebase security rules. Our guides to the Supabase anon key and the Firebase API key explain what has to be right for that to be safe. Secret keys (sk-, sk_live_, service_role or sb_secret_) never are.

How to check your live app

  1. Open the published site, not the preview. Developer tools → Sources (or Network → JS files).
  2. Search all files for sk-, sk-ant-, sk_live, service_role and sb_secret_.
  3. Check whether .map files load next to your scripts; they can contain code that the bundle dropped.
  4. Look at your repository too, if it's public. GitHub's guide to removing sensitive data says to revoke or rotate the secret first, before cleaning history.

Or let a scanner do the searching. The free Securick scan reads the JavaScript your live site serves and reports keys it finds, masked, among its 46 tests:

  • #2 OpenAI API key exposed Free
  • #3 Anthropic API key exposed Free
  • #1 Stripe live secret key exposed Free
  • #5 Supabase service key exposed Free
  • #4 AWS access key exposed Free
  • #6 GitHub token exposed Free
  • #10 Secret values in public env variables Free
  • #13 JavaScript source maps public Free

OWASP's secrets management cheat sheet covers the wider practice: secrets in a managed store, scanned for before they're committed, and rotated.

Questions

Can I encrypt or obfuscate the key in my JavaScript?

No. The code that decrypts it runs in the browser too, so anyone can run it and get the key. Only a server or edge function can keep a key secret.

My app is small. Does anyone really look for keys?

Yes. Leaked AI and payment keys are searched for automatically, because they can be used right away to run up bills or take money. Size doesn't matter; the key does.

Is a key in a .env file safe?

On a server, yes, as long as the file is never deployed where visitors can load it. In a front-end project it depends on the name: VITE_ and NEXT_PUBLIC_ values end up in the browser.

Check your live app now

Paste your app's address. The free scan runs 46 tests and shows proof for everything it finds.