Supabase anon key vs service_role key: what can be public
Updated 5 December 2026
Supabase gives every project two kinds of keys. One is meant to be in your website's JavaScript. The other gives full access to the database and must only ever exist on a server. Mixing them up is one of the most damaging mistakes in AI-built apps — and one of the easiest to spot.
The two keys side by side
| Public key | Secret key | |
|---|---|---|
| Names | anon key, or new-style sb_publishable_… | service_role key, or new-style sb_secret_… |
| Row Level Security | Enforced — sees only what your policies allow | Bypassed — reads and writes every row of every table |
| Belongs in | Frontend code, mobile apps | Server code, Edge Functions, environment variables only |
| If it leaks | Nothing, as long as RLS is set up | Full database takeover: read, change and delete all data |
Both legacy keys are JWTs (they start with eyJ), so they look alike. Decode one at any JWT viewer, or just read the middle part: it contains "role":"anon" or "role":"service_role".
How the secret key ends up in the browser
- An AI assistant "fixes" a permission error by swapping the anon key for the service_role key — the error disappears, and so does all security.
- The key is put in a
VITE_,NEXT_PUBLIC_orREACT_APP_environment variable. Those prefixes mean "copy this into the public bundle". - A server-side helper file gets imported into a client component and the bundler includes it.
How to check your live site
Open your site, then DevTools → Sources (or Network → JS files) and search all files for service_role and sb_secret_. Or let a scanner do it: VibeCleared downloads every JavaScript file your page loads, decodes any Supabase JWT it finds and tells you which role it has. A leaked secret key is shown in the free scan, always masked.
If the service_role key leaked
- Rotate it first. Removing it from the code is not enough — it stays in old deployments, CDN caches, browser caches and git history. In the Supabase dashboard go to Project Settings → API Keys and roll or revoke the secret key, then update it in your server environment.
- Move every call that needed it to the server: a Supabase Edge Function or your backend, with the key in a server-only secret.
- Fix what the AI was trying to work around — usually a missing RLS policy. Add the policy so the public key is enough.
- Check for damage: look at the API logs in the dashboard for unexpected access, and review recent changes to your data.
My app uses the Supabase service_role key in frontend code. Move every query that
needs elevated rights into a Supabase Edge Function that reads the key from a
server secret, use only the anon/publishable key in the browser, and add the RLS
policies the frontend queries need. Do not rotate the key yourself — tell me when
to do it.
Questions
Can I hide the service_role key with obfuscation or environment variables?
No. Anything that reaches the browser can be read. Environment variables with VITE_, NEXT_PUBLIC_ or REACT_APP_ prefixes are copied into the public bundle on purpose.
Is the anon key safe if I have RLS?
Yes. The anon or publishable key is designed to be public, and Row Level Security decides what it can see. Make sure every table in exposed schemas has RLS enabled.
What are sb_publishable_ and sb_secret_ keys?
Supabase's newer API key format. The publishable key replaces the anon key for frontend use; the secret key replaces service_role and must stay on the server. They can be rotated without changing your JWT secret.
Related guides
- Is your Supabase data public? Check RLS in 5 minutes
- Leaked API keys in JavaScript: find and fix
- Launch checklist for AI-built websites
Run all of these checks on your live site in under a minute — free, no signup.
Scan my site free