NEXT_PUBLIC_ and VITE_ variables: which keys are safe in the browser
Updated 9 November 2026 · 5 min read
Frameworks need a way to tell server-only settings from ones the browser needs. Next.js uses the NEXT_PUBLIC_ prefix, Vite uses VITE_, Expo uses EXPO_PUBLIC_ and Create React App uses REACT_APP_. Any variable with that prefix is copied into the JavaScript bundle at build time, so every visitor can read it, whether or not you show it on the page.
Next.js says this plainly in its environment variables docs, and Vite does in its env and mode guide. The prefix means “public”, not “a bit safer”.
Keys that are fine in the browser
Some keys are designed to be public. They only allow what the browser is meant to do, and the real protection is somewhere else:
- Supabase anon (publishable) key: safe only if every table has row level security.
- Stripe publishable key (
pk_live_...): can create payment forms, not take money. - Firebase web config: protected by Firebase security rules.
- Map keys, analytics keys, Sentry DSN: restrict them by domain in the provider’s dashboard.
Keys that must never be there
If a variable like these has a public prefix, it’s leaked:
NEXT_PUBLIC_OPENAI_API_KEY=sk-proj-... # anyone can run up your AI bill
VITE_SUPABASE_SERVICE_ROLE_KEY=eyJ... # skips all row level security
NEXT_PUBLIC_STRIPE_SECRET_KEY=sk_live_... # full access to your Stripe account
VITE_DATABASE_URL=postgres://user:pass@... # direct database access
AI coding tools add these more often than you’d think: the browser code needs the AI call to work, so the quick fix is to make the key public.
How to fix it
- Move the call to the server. Create an API route or server action that holds the key and calls the provider. The browser calls your route instead.
- Rename the variable without the public prefix, so it can’t be bundled again.
- Rotate the key in the provider’s dashboard. It’s already been shipped to browsers and may be cached or archived.
- Check the history. If the key was ever committed in a
.envfile, it’s also in git history. See how to keep secrets out of your code.
Which rules this relates to
A secret in the browser bundle relates to GDPR Article 32 (security of processing), OWASP A04:2025 and CWE-200 Exposure of Sensitive Information. For AI keys, it’s also LLM02:2026 Sensitive Information Disclosure.
Checking it automatically
Lintsure’s secret.public-env rule flags secret-looking values in public variables and skips keys that are public by design. supabase.service-key-in-client catches the service role key in browser code. Both run locally, in your editor’s AI agent or in CI. Get started.
Check your own code
Lintsure runs these checks on your machine and never uploads your code.
npx lintsure scanThis guide is our own reading of public texts, written for software teams. It is not legal advice and it doesn't cover your whole obligation, only the parts code can show. Ask a qualified person before any audit or decision. Disclaimer