NEXT_PUBLIC_ secret in the bundle
Last verified:
The fix
A NEXT_PUBLIC_ or VITE_ secret is in the browser bundle. Rotate the key, move the call to a server route, and keep real public keys in the client.
- 1
Rename a NEXT_PUBLIC_ secret
In
.env.local, a name that starts withNEXT_PUBLIC_and holds a secret is the leak. The anon key andsb_publishable_may keep the prefix.sb_secret_,service_role, andsk_must not. Read the secret asprocess.env.SUPABASE_SERVICE_ROLE_KEY(or the matching unprefixed name) in a Route Handler, a Server Action, or a Server Component. If the screen then says the value is undefined, that read was still in the browser. Move the call. Do not put the prefix back.# public key, only safe in the browser when RLS is on NEXT_PUBLIC_SUPABASE_URL=https://<project-ref>.supabase.co NEXT_PUBLIC_SUPABASE_ANON_KEY=<anon-or-sb_publishable> # server only. No NEXT_PUBLIC_ prefix. SUPABASE_SERVICE_ROLE_KEY=<sb_secret-or-service_role> - 2
Treat the key as compromised
Rotate it in the provider’s dashboard. Restrict the replacement (domain, IP, or usage cap) where the provider allows it. Deleting the line from the source does not invalidate a key that was already shipped.
- 3
Move the call to a server route
The browser calls your route. The route holds the secret in a normal environment variable with no public prefix. The client never sees the key.
export async function POST(req: Request) { const body = await req.json(); const response = await fetch("https://api.openai.com/v1/chat/completions", { method: "POST", headers: { Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify(body), }); return Response.json(await response.json(), { status: response.status }); } - 4
Check the built output
Search the repo for the key prefix (
sk_live_,sk_test_,service_role,sk-). Then search the production JS bundle or the browser source panel. A key that only remains in.env.localon the server is fine. A key in a chunk downloaded by the browser is not. - 5
Leave public keys public on purpose
Supabase’s anon or publishable key may stay in the client. Confirm RLS is enabled on every table that key can touch. Stripe publishable keys (
pk_) may stay in the client. Secret keys may not.
Why it happens
Anything compiled into a client component is public. NEXT_PUBLIC_ in Next.js and VITE_ in Vite exist so the browser can read them. The model reaches for those prefixes when a client component “needs” to call Stripe, OpenAI, or the database. Next.js inlines every NEXT_PUBLIC_ value from .env.local into the client bundle at build time. The file can stay gitignored and the secret is still inside JavaScript the browser downloads. A name with no prefix is undefined in client code on purpose. That missing value is a different page.
Some keys are designed to be public. The Supabase anon key and the newer publishable key (sb_publishable_) are safe in the browser only when Row Level Security is on. The service role key, a secret key (sb_secret_), Stripe secret keys (sk_), and OpenAI keys are not. A legacy Supabase JWT is three segments. The middle one is base64, not encrypted. Decode it on your machine and read role. service_role is the secret. anon is the public key. Do not paste the key into a chat. If one of the secret kinds has been in a client bundle, assume it was copied. Do not regenerate the Supabase JWT secret to rotate one key. That invalidates the current API keys immediately.
The next error you'll probably hit
Once this is fixed, the usual follow-up is process.env.NEXT_PUBLIC_ is undefined. process.env.NEXT_PUBLIC_ is undefined. The name is missing from .env.local, it has no NEXT_PUBLIC_ prefix, or Next.js has not been restarted since you added it. Do not put a secret on that prefix.
Prompt to paste back into your AI tool
Paste this above the error.
A secret API key is in frontend code or in a NEXT_PUBLIC_ / VITE_ variable. Do not keep the secret in the client. Do not only rename the variable. 1. Tell me to rotate the key. Assume the old value was copied. 2. Move the provider call into a server route that reads a non-public env var. 3. Point the client at that route. 4. If this is a Supabase anon or publishable key, say so and check RLS instead of hiding the key. 5. Do not rewrite unrelated files.
Common questions
+Is the Supabase anon key supposed to be in the browser?
Yes, if Row Level Security is enabled and the policies are correct. The service role key is the one that must never ship to the client.
+I removed the key from the file. Am I done?
No. Rotate it. The old value may be in a deployed bundle, a git history, or a screenshot of the chat. Deleting the line does not invalidate a key that already shipped.
+.env.local is gitignored. Is the key still private?
The file is not what the browser receives. Next.js copies a NEXT_PUBLIC_ value into the client JavaScript at build time. Anyone who can load the site can read that string. Rotate the key after you rename it.
+I removed the line and the app says process.env.NEXT_PUBLIC_ is undefined. What now?
That read is still in client code. Move it to the server and use the unprefixed name. Putting NEXT_PUBLIC_ back publishes the key again. A missing anon key or URL is a different failure.
+Is NEXT_PUBLIC_SUPABASE_ANON_KEY supposed to be in .env.local?
Yes. The anon key and the publishable key are meant for the browser, and only when Row Level Security is on. NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY is the one that does not belong there. The service role bypasses RLS.
Related errors
- process.env.NEXT_PUBLIC_ is undefinedprocess.env.NEXT_PUBLIC_ is undefined. The name is missing from .env.local, it has no NEXT_PUBLIC_ prefix, or Next.js has not been restarted since you added it. Do not put a secret on that prefix.CursorLovableBoltNext.jsVercelNov 3, 2026
- Rotate a leaked API key without taking the app downCreate the new key, point the host at it, redeploy, and prove a request works. Then revoke the old key. Revoking first is how production starts failing.CursorLovableBoltOpenAIStripeOct 22, 2026
- ReferenceError: process is not definedprocess is not defined in Vite, including Lovable and Bolt: browser code read process.env. Use import.meta.env.VITE_NAME and restart the dev server.CursorLovableBoltViteReactOct 16, 2026
- Invalid API keyInvalid API key. Supabase received a key this project does not accept. Copy the publishable key, or the legacy anon key, from the same project as the URL. No API key found in request means the header was missing.CursorLovableBoltSupabaseNext.jsNov 6, 2026
- You exceeded your current quotaYou exceeded your current quota: the OpenAI account has no credit. Add billing. Retrying will not help. Rate limit reached is the other 429.CursorLovableBoltOpenAINext.jsOct 14, 2026
- RLS Disabled in Public: Table public.x is public, but RLS has not been enabledRLS Disabled in Public. Table public.x is public, but RLS has not been enabled. Enable Row Level Security and add a policy in the same change. The anon key can read that table until you do.CursorLovableBoltSupabasePostgresNov 3, 2026