Forbidden use of secret API key in browser
Last verified:
The fix
Forbidden use of secret API key in browser. The request sent an sb_secret_ key with a browser User-Agent. Put the publishable key in the client, and keep the secret in a server variable with no public prefix.
- 1
Put the publishable key in the browser env
In Connect, or Settings, API Keys, copy the publishable key (
sb_publishable_...) into the client variable. The server variable has noNEXT_PUBLIC_orVITE_prefix. Redeploy after you change them. Those public prefixes are written into the bundle at build time.NEXT_PUBLIC_SUPABASE_URL=https://[PROJECT-REF].supabase.co NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=sb_publishable_... SUPABASE_SECRET_KEY=sb_secret_... - 2
Move the privileged call off the client
A browser client uses
createClientwith the publishable key. A route handler, server action, or Edge Function readsSUPABASE_SECRET_KEY. A Vite app with no server of its own calls an Edge Function instead of holding the secret. If this is Lovable Cloud and you were never given keys, tell the tool the browser client has to use the publishable key. Do not invent a secret in aVITE_variable. - 3
Rotate the secret after the bundle is clean
Search the built JS for
sb_secret_. When it is gone, create a new secret key, point the server env at it, redeploy the server, then delete the key that was in the client. Deleting first, or rotating while the old value is still inNEXT_PUBLIC_, ships the replacement the same way. - 4
Change the User-Agent when the caller is not a browser
PowerShell, curl, or a worker that sends
Mozilla/5.0trips the same check. Set a User-Agent that names the script. Leave the secret out of the repo. If you cannot tell whether a browser already downloaded the key, treat it as leaked and rotate after the client is fixed.
Why it happens
Supabase's API key guide says a secret key does not work in a browser. The gateway matches on the User-Agent and returns HTTP 401. The body people paste from that refusal is Forbidden use of secret API key in browser. The key itself was valid. The call looked like a browser, so the seatbelt tripped. Invalid API key is a different sentence: the key does not belong to this project. new row violates row-level security policy means a publishable or anon key reached Postgres and a policy said no.
sb_secret_... bypasses row level security, the same as the legacy service_role key. NEXT_PUBLIC_ and VITE_ exist so the bundler can ship the value to every visitor. A variable named NEXT_PUBLIC_SUPABASE_ANON_KEY that holds sb_secret_... is how this error shows up in a Lovable, Bolt, or Cursor app. The 401 is not a lock. The docs say someone can still use that key from a non-browser client. A script or CI job that sends a browser User-Agent gets the same 401 even when the key never entered a web page.
The next error you'll probably hit
Once this is fixed, the usual follow-up is new row violates row-level security policy. new row violates row-level security policy: RLS is on and no INSERT policy allows this role. Add a policy for the signed-in user. A missing GRANT differs.
Prompt to paste back into your AI tool
Paste this above the error.
The Supabase request failed with: Forbidden use of secret API key in browser. HTTP status is 401. The client is <Next.js or Vite or a script>. The env name holding the key is <name>. Do not keep sb_secret_ or the service_role key in a NEXT_PUBLIC_ or VITE_ variable. Do not disable RLS to get around this. Do not rotate the key before the browser bundle stops containing it. 1. If the value is in client code or a public env prefix, replace it with the publishable key (sb_publishable_...) from Connect or Settings, API Keys, and tell me to redeploy. 2. Move any call that needs to bypass RLS into a server route or Edge Function that reads SUPABASE_SECRET_KEY with no public prefix. 3. After the built JS no longer contains sb_secret_, tell me to create a new secret key, update the server env, and delete the old one. 4. If the caller is a script with a browser User-Agent and the key is not in a bundle, say so and change the User-Agent instead of pretending the key was shipped to users. Show only that diff.
Common questions
+I replaced the anon key with sb_secret_ and got Forbidden use of secret API key in browser. Why?
The secret key is refused when the User-Agent looks like a browser. The publishable key (sb_publishable_...) is the one that belongs in the client. It has the same low privileges as the legacy anon key, and row level security still applies.
+Is this the same as Invalid API key?
No. Invalid API key means the key is missing, truncated, or from another project. Forbidden use of secret API key in browser means the key was recognized as a secret and the caller looked like a browser. Swapping in a random string turns this into Invalid API key.
+The secret was only on localhost. Do I still rotate it?
The docs say a secret key must not be in a browser, including localhost, and that the 401 does not stop someone from using a leaked key in curl. If the value was in a NEXT_PUBLIC_ or VITE_ bundle, rotate it after that bundle no longer contains it.
+A GitHub Action got the same sentence and the key is in the Actions secret store. What now?
The job is sending a browser User-Agent. Set a User-Agent that names the workflow. The key can stay server-side. Rotate only if the log or the repo also printed the value.
Related errors
- new row violates row-level security policynew row violates row-level security policy: RLS is on and no INSERT policy allows this role. Add a policy for the signed-in user. A missing GRANT differs.CursorLovableBoltSupabasePostgresOct 15, 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
- NEXT_PUBLIC_ secret in the bundleA 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.CursorLovableBoltNext.jsViteNov 11, 2026
- 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