Beyond The Vibe, home

BlogProductionAdvent of Prod · Day 3

How do I hide an API key in the frontend?

You can't hide a key in code the browser downloads. Which keys are meant to be public, where the secret ones belong, and how to check what your app ships.

Florian van der Galiën

On 28 February 2026 a founder wrote that the live secret key of his payment provider had been sitting in the frontend code of his app, which an AI tool had built. Someone found it, and 175 customers were charged 500 dollars each before he could replace the key. He reversed the charges and was still left with 2,500 dollars in processing fees.

The short answer is that you can’t. The browser downloads everything in the frontend, and whoever uses the browser can read it. A secret key has to move to a server.

Nothing in the frontend is hidden

Minifying the code doesn’t hide a key, and neither does an environment variable. Build tools make certain variables available to the code they send to the browser, and the name tells you which. A value then ships wherever that code uses it. Vite’s documentation says: “VITE_* variables should not contain sensitive information such as API keys.”

Tool Variables open to browser code
Vite names starting with VITE_
Next.js names starting with NEXT_PUBLIC_
Astro names starting with PUBLIC_

The fix has the same shape everywhere. The browser asks your own server, the server adds the key and calls the service, and only the result goes back.

Four steps: the browser asks your own server and sends no key, your server adds the secret key and calls the service, the service answers your server, and the browser gets only the result.

Depending on your hosting, that server code is a route in your app, a serverless function or an edge function.

Some keys are meant to be public

Many services hand you two keys, and only one of them is secret.

Service May be in the frontend Server only
Stripe publishable key, pk_… secret key sk_…, restricted key rk_…
Supabase publishable key, sb_publishable_…, anon secret key, sb_secret_…, service_role

Stripe’s publishable key can’t create charges or read account data. Supabase’s publishable key is safe only as far as your access rules are, which is the subject of Can other users see my users’ data? Its secret key ignores those rules. The documentation’s warning: “A secret key bypasses every Row Level Security policy you have.”

I have mixed up the two halves of a pair myself. The first mail from mailhub, my mail-sending service, stayed in the queue with 401 denied_authentication. The mail provider issues an access key ID, which only names the key, and a secret key. The variable on my server held the ID. No mail went out until I swapped the value.

What the server does with its secrets

Once a key lives on the server, the server should refuse to run without it. The sign-in library in one of my apps already refuses to start in production without its secret. For a secret that is too short, the installed version only logs a warning. The app’s own start-up check turns that into a refusal:

Set BETTER_AUTH_SECRET to at least 32 random characters.

An app that doesn’t start is noticed at once. A warning in a log is easy to miss.

A key can also leak without reaching the frontend. I once pasted a secret into a message to my coding agent. It only protected a test setup on my laptop. A production key that has been in a chat, a commit or a screenshot has to be replaced, and so does a secret key that was ever in your frontend, because old copies of the code are still out there. Supabase’s documentation: “Rotate a leaked key as soon as you fix the cause of the leak.”

Check your own app

Open the environment variables of your project, in your hosting dashboard or your .env file, and read the names. Every value whose name has one of the three prefixes above can end up in the code every visitor downloads. If you wouldn’t post that value in public, it is in the wrong place. A name without a prefix proves less, because server code can still print such a value into a page. For a further lead, open your live site with the browser’s developer tools and search the loaded files for a marker such as sk_. A hit needs a closer look and stays out of chats and screenshots. No hit on one page rules nothing out.

Then have your agent trace where each value goes:

Check whether this app can send secrets to the browser. Read code and
configuration only: do not change anything, run a build or scripts, send
requests to live services or use production credentials. Report variable
names, file names and line numbers. Never show the value of a key, token or
connection string, not in part and not in quoted code or tool output.

1. List every environment variable the code reads, by name. Does it reach
   code that runs in the browser, or a page the server renders?
2. List every request the browser makes directly to a third-party service
   and the name of the key it uses. Does the service document that kind of
   key as public?
3. Does the server refuse to start in production when a secret is missing?

Mark what you saw and what you inferred. For each secret that can reach the
browser, say what has to move to the server. I will replace that key.

This post is part of the checklist in Is my vibe-coded app production-ready?