LeakPatrol › Guides

Supabase "Forbidden use of secret API key in browser" (401) fixed

By LeakPatrol · Updated 20 November 2026

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 fix

  1. Use the publishable key (sb_publishable_…) in the browser. It is meant to be public, like the old anon key.
  2. 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.
  3. 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:

  1. In Supabase, open Project Settings → API Keys and create a new secret key.
  2. Put the new key in your Edge Function secrets and update anything server-side that used the old one.
  3. Delete the old secret key.
  4. Search your live site's JavaScript (F12 → Sources → search all files) for sb_secret_ and service_role to 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.

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