Supabase "Forbidden use of secret API key in browser" (401) fixed
You switched to Supabase's new API keys, and now requests from your app fail with 401 Unauthorized and a message like Forbidden use of secret API key in browser. Supabase is doing you a favour. The sb_secret_… key is the new version of the service_role key: it skips Row Level Security and can read and change everything. Supabase's API key docs say a secret key sent from a browser is rejected with 401. The fix is not to get around the block. It is to stop the secret key from reaching the browser at all.
Why you're seeing it
- The secret key is in your front-end code. Most often it's in a
VITE_orNEXT_PUBLIC_variable, or pasted into the Supabase client in Lovable, Bolt or Cursor. Anything with those prefixes is bundled into the JavaScript every visitor downloads. - The AI builder "fixed" a permissions error with the secret key. An RLS error such as
new row violates row-level security policyis a common reason an AI swaps in the most powerful key it can find. - Server code that looks like a browser. Supabase decides by the request's
User-Agent. Rarely, server code that sets a browser-like User-Agent gets blocked too.
The fix
- Use the publishable key (
sb_publishable_…) in the browser. It is meant to be public, like the old anon key. - Move anything that really needs admin rights (sending emails, admin pages, changing other users' data) into a Supabase Edge Function or your own server. Keep the secret key in the function's secrets, never in a
VITE_variable. - If the browser was writing to a table and got an RLS error, write a proper policy instead of using the secret key. See "new row violates row-level security policy".
// Browser: publishable key only
const supabase = createClient(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY
);
// Edge Function: secret key from Deno.env, never shipped to visitors
const admin = createClient(
Deno.env.get('SUPABASE_URL')!,
Deno.env.get('MY_SECRET_KEY')!
);
Your secret key may already be public
The 401 stops your app from using the key from a browser. It does not stop anyone from copying it. If the key was in your front-end code, it was in the JavaScript of your live site, and anyone can call Supabase with it from a script, where no browser check applies. Treat it as leaked:
- In Supabase, open Project Settings → API Keys and create a new secret key.
- Put the new key in your Edge Function secrets and update anything server-side that used the old one.
- Delete the old secret key.
- Search your live site's JavaScript (F12 → Sources → search all files) for
sb_secret_andservice_roleto check it's gone.
More detail on cleaning up: what to do when the service_role or secret key is exposed.
A prompt to paste into Lovable, Bolt or Cursor
Supabase returns 401 "Forbidden use of secret API key in browser".
Fix it safely:
1. Find every place the sb_secret_ (or service_role) key is used.
2. The browser client must use only the publishable key.
3. Move any code that needs the secret key into a Supabase Edge
Function that checks the user with auth.getUser() first.
4. Store the secret key only in Edge Function secrets, never in a
VITE_ or NEXT_PUBLIC_ variable.
5. If a table write failed because of RLS, add a policy instead.
LeakPatrol's free scan reads the JavaScript your live site loads and flags sb_secret_, service_role and other secret keys in about a minute, so you can confirm the key is gone.
Check your app in a minute
Free, read-only, no sign-up. Every problem comes with a plain-English fix and a prompt for your AI tool.
Get the 10-point checklist
Every check we run on AI-built apps, with how to do each one yourself. Straight to your inbox.
Plus the odd security tip. Unsubscribe any time.
Questions
What does "Forbidden use of secret API key in browser" mean?
Supabase received a request with an sb_secret_ key from something that looks like a browser and rejected it with 401. Secret keys skip Row Level Security, so they must only be used on a server or in an Edge Function.
How do I fix the Supabase secret key 401 error?
Use the publishable key (sb_publishable_) in the browser, move admin actions into an Edge Function that holds the secret key, and add RLS policies for anything the browser needs to write.
Is my Supabase secret key leaked if I got this error?
If the key was in your front-end code, assume yes: it was in the JavaScript anyone can download. Create a new secret key, update your server code, and delete the old key.
Can I just change the User-Agent to get around it?
You could, but that leaves your full-access key public. The browser check is a safety net, not a security boundary. Keep secret keys on the server.
More guides
- Supabase RLS policy generator (free)
- Is this API key safe in my frontend? Free key checker
- Security scan MCP server for Claude Code, Cursor and other AI agents
- Supabase's October 30 change: what it fixes for your app's security, and what it doesn't
- Supabase "42501 permission denied for table": the safe fix (not GRANT ALL)
- Supabase "new row violates row-level security policy": 5 causes and the safe fix
- Supabase service_role key exposed? What to do in the next hour
- Supabase select returns an empty array but the data is there: the RLS fix
- Supabase "Database error saving new user": find the cause and fix it safely
- Supabase Storage upload: "new row violates row-level security policy" fixed
- Supabase "infinite recursion detected in policy for relation" fixed
- Supabase Edge Function "blocked by CORS policy" fixed
- Supabase "Invalid API key" and "Legacy API keys are disabled" fixed
- Supabase login or magic link redirects to localhost:3000 (fixed)
- Is your Supabase storage bucket public? What anyone can see, and the fix
- Lovable data exposure study: is your app one of the 24,664?
- Lovable security check: is your Lovable app safe?
- Bolt.new security check: is your Bolt app safe?
- Launch checklist for apps built with Lovable, Bolt and Cursor
- Supabase security check: is your anon key safe? (RLS checker)
- How to check if your API key is exposed on your website
- Are VITE_ env variables secret? How to hide API keys
- Stop users giving themselves your paid plan (Supabase)