(Note · · 5 min read)
Your API key is in the JavaScript anyone can read
A key sits in an environment variable, which feels like the safe place for it. But if the variable is read in code that runs in the browser, the build has copied its value into a file that anybody can download and read. The server-side habit of trusting environment variables does not survive the move to the client.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
The build inlines the value
Client-side code cannot read environment variables at run time, so bundlers substitute the literal value during the build. There is no lookup in the browser and nothing to protect — the secret is text in a file served to every visitor.
Framework prefixes exist to make this deliberate. A variable marked for client exposure is being published, and that is the correct reading of the prefix: not that it is safe to expose, but that you have asked for it to be.
Where it usually happens
- A variable given the public prefix so it would stop being undefined in a component.
- A secret read in a file that is imported by both server and client code, which pulls it into the client bundle.
- A key committed to the repository in a configuration or example file, where it stays in the history after being removed.
- A third-party integration whose setup instructions assume a server and gets wired into the browser instead.
- An error or debug response that includes configuration, which then appears in a client-side log or a monitoring tool.
Check rather than assume
Search the built client output for the secret's value. Not the variable name, the value — that is the only check that answers the question, and it takes one command against the build directory.
Worth making routine: a step in continuous integration that fails the build if any known secret's value appears in client output. It catches the import that quietly pulled a server module into a component, which is the version of this bug nobody writes on purpose.
Some keys are meant to be public
A publishable payment key, an analytics identifier, a maps key — these are designed to be visible, and their protection is configured at the provider: restricted to your domain, scoped to specific operations, rate limited.
So the question is never whether a key is in the bundle, but whether that key is one that can be. If it can authorise a charge, read a database, or send mail on your behalf, it belongs on a server, with the browser calling your endpoint instead.
Rotate, do not just remove
Once a secret has been served to browsers, deleting it from the code does not undo it. Assume it is compromised: issue a new one, update the server, revoke the old one, and look at the provider's logs for use you cannot account for.
If it was committed, it remains in the repository history and in every clone and fork. Rotation is the fix; rewriting history is housekeeping that does not help with copies other people already have.
Keep the boundary visible in the code
Read secrets in one server-side module, and never import that module from anything that could reach the client. The discipline that holds is structural — a boundary you can see in the file layout — rather than remembering which variables are sensitive.
On a client-side form that needs a third-party service directly, treat the key as public by definition and choose the service accordingly. This site's contact form does exactly that, because its provider's free plan blocks server-side requests — which makes the constraint an explicit design decision rather than an accident.
Common questions
Why is my environment variable visible in the browser?
Because client-side code cannot read environment variables at run time, so the bundler substitutes the literal value during the build. There is no lookup in the browser — the value is text in a file every visitor downloads.
How do I check whether a secret is in my bundle?
Search the built client output for the secret's value, not the variable name. Make it a continuous integration step that fails the build when a known secret's value appears in client output — that catches the accidental server-module import.
Are all keys in frontend code a problem?
No. Publishable payment keys, analytics identifiers and maps keys are designed to be visible, and are protected at the provider by domain restrictions, scopes and rate limits. The problem is a key that can authorise a charge, read a database or send mail.
A secret already shipped to browsers — is removing it enough?
No. Assume it is compromised: issue a new one, update the server, revoke the old, and check the provider's logs for unaccounted use. If it was committed it also remains in history and in every clone, so rotation is the fix.