Guide · Supabase API keys · Updated
Supabase anon Key vs service_role Key: Which Key Is Safe in the Browser?
Every Supabase project gives you two kinds of keys: a public key for your frontend (sb_publishable_... or the legacy anon key) and a master key for your server (sb_secret_... or the legacy service_role key). Mixing them up is one of the fastest ways to expose every row in your database. Here is what the official Supabase docs say, why AI builders mix them up, and how to check your live app in 60 seconds.
1. anon (publishable) vs service_role (secret) at a glance
Supabase docs compare your project to a building: the publishable key is taped to the front door and only opens what your rules allow, while the secret key is the master key that stays in your pocket.
| Key type | What it looks like | Postgres role | Row Level Security (RLS) | Where it belongs |
|---|---|---|---|---|
| Publishable key | sb_publishable_... | anon (signed out) or authenticated (signed in) | Followed | Browser, mobile app, desktop app, CLI |
| Legacy anon key | eyJ... (JWT with role: "anon") | anon (signed out) or authenticated (signed in) | Followed | Browser, mobile app (legacy; migrating to sb_publishable_) |
| Secret key | sb_secret_... | service_role (has BYPASSRLS) | Bypassed completely | Server routes, Edge Functions, cron jobs. NEVER in browser |
| Legacy service_role key | eyJ... (JWT with role: "service_role") | service_role (has BYPASSRLS) | Bypassed completely | Server only. NEVER in browser (legacy; migrating to sb_secret_) |
Supabase is deprecating the legacy JWT-based anon and service_role keys (long strings starting with eyJ) by the end of 2026 in favor of short sb_publishable_... and sb_secret_... keys. Both pairs work the same way with Postgres roles and Row Level Security.
Source: Supabase: API keys
2. When the anon / publishable key is safe (and when it is not)
People often panic when they see their anon or sb_publishable_ key in Chrome DevTools. Seeing that key in the browser is normal. Every browser Supabase app ships it.
When your app uses the publishable or anon key:
- If the visitor is not signed in, Postgres runs the request as the
anonrole. - If the visitor is signed in through Supabase Auth, your app still sends the publishable key as the
apikeyheader, plus the user's Auth token, and Postgres runs the request as theauthenticatedrole.
Here is the catch: the anon key is safe in browser code only when Row Level Security (RLS) is enabled on every table in public (with policies that check who the user is). Supabase's RLS docs warn that a table in an exposed schema without RLS is readable and writable by any role that has a table grant on it. On projects where tables in public grant select, insert, update and delete to anon, turning RLS off means anyone with your anon key can read, edit or delete the whole table.
Want to test what your public key can read right now? Follow our Supabase RLS check.
Sources: Supabase: API keys · Supabase: Row Level Security
3. Why service_role and sb_secret_ must never touch the frontend
Both the legacy service_role key and the newer sb_secret_... key map to the built-in service_role Postgres role. By design, that role has the BYPASSRLS attribute: it bypasses every Row Level Security policy completely.
- Never put it in
NEXT_PUBLIC_,VITE_orEXPO_PUBLIC_. Those prefixes tell Next.js, Vite and Expo to inline the key into the public JavaScript bundle sent to every visitor. See which env vars end up in the browser. - Browser block on
sb_secret_does not make a leak safe. Supabase blockssb_secret_...requests that come directly from a browser by checking theUser-Agentheader and returningHTTP 401 Unauthorized. But Supabase docs warn that ifsb_secret_is bundled into your frontend code, an attacker can still copy the string and run it fromcurl, Postman or their own script, where no browserUser-Agentstops it. - Legacy
service_roleJWTs work right in the browser. Unlikesb_secret_, the oldereyJ...service_roleJWT does not fail in a browser, so an app using it in client code looks like it works while giving every visitor admin access.
Sources: Supabase: API keys · Supabase: Bypassing Row Level Security
4. Why vibe-coded apps accidentally leak service_role
When you build with Lovable, Bolt, v0, Replit or Cursor, you rarely paste service_role into the frontend on purpose. It usually happens during debugging:
- You hit
permission denied for table (42501)or get an empty[]back. Either the table is missing aGRANT(especially after Supabase's October 30, 2026 default grants change) or RLS is on without a matching policy. - You tell the AI: "My query is failing, just make it work."
- The AI swaps in
SUPABASE_SERVICE_ROLE_KEY. To make the error disappear immediately, the AI initializescreateClient()with theservice_rolekey and renames the variable toNEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEYorVITE_SUPABASE_SERVICE_ROLE_KEYso the browser can read it.
The error disappears, the UI loads your data, and every visitor now has a master key to your database. The safe fix for 42501 or [] is always a proper GRANT plus an RLS policy for authenticated (or moving true admin work to a server route or Edge Function).
5. How to check your live app in 60 seconds + rotate a leaked key
Only check apps you own or have permission to test.
Check your live app in 60 seconds
- Fastest: paste your live link above. VibeCheck fetches your live app's public JavaScript bundles, checks if any
sb_secret_orservice_roleJWT is exposed, and tests if your public key can read open tables. - Manual check in Chrome DevTools: open your live site, right-click → Inspect, and press Ctrl+Shift+F (Cmd+Option+F on Mac) to search all loaded files. Search for
sb_secret_andservice_role. - Check
eyJJWT keys: both legacyanonandservice_rolekeys start witheyJ. Look at the middle section of the token (between the two dots) or compare the string against Settings → API Keys in your Supabase Dashboard. In base64,"role":"service_role"encodes toInJvbGUiOiJzZXJ2aWNlX3JvbGUi. If that string is in your browser bundle, yourservice_rolekey is public.
How to rotate a leaked Supabase secret or service_role key
Supabase's official rotation steps say not to rush. Fix the code leak first so your new key does not leak on the next deploy:
- Remove the secret key from client code and switch the browser client back to
sb_publishable_...(oranon). - Open Settings → API Keys in your Supabase Dashboard and create a new secret key (
sb_secret_...). - Update every server, worker or Edge Function to use the new secret key (without any
NEXT_PUBLIC_,VITE_orEXPO_PUBLIC_prefix) and redeploy. - Retire the leaked key: in Settings → API Keys, delete the leaked
sb_secret_key (permanent), or deactivate your legacy JWT keys if the oldservice_roleJWT leaked.
Also leaked an OpenAI or Stripe key? Read I leaked my API key. What do I do now?
6. Copy-paste fix prompts
Paste the matching prompt into Lovable, Bolt, v0, Replit or Cursor. After you deploy, rotate any service_role or sb_secret_ key that was ever in client code.
Search this project for service_role, SUPABASE_SERVICE_ROLE_KEY, SUPABASE_SECRET_KEY and sb_secret_. Remove every secret or service_role key from client-side code and from any NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_ variable. In the browser client, use only the Supabase publishable (or anon) key. For any query that broke when you switched back to the public key, keep Row Level Security enabled, grant only the needed privileges to authenticated (or anon if truly public), and write a policy using (select auth.uid()) = user_id. List every key you removed so I can rotate it in Supabase.
Find every place this app needs admin database access that skips Row Level Security. Move those calls into a server-only Route Handler, Server Action or Supabase Edge Function that reads SUPABASE_SECRET_KEY (no NEXT_PUBLIC_ prefix). Inside each server route, verify the signed-in user before running the query. Never return the secret key to the client.
My Supabase queries return error 42501 (permission denied) or an empty array []. Do NOT use the service_role key in the frontend and do NOT disable Row Level Security. Instead, write a SQL migration that keeps RLS enabled on each table, grants the needed select/insert/update/delete privileges to authenticated, and adds separate select, insert, update and delete policies matching rows to (select auth.uid()). Show me the SQL.
7. Check it for me
Want to check your live app right now? Paste your link into VibeCheck. In about 10 seconds it checks the JavaScript your live site sends to visitors for leaked keys (including Supabase service_role and sb_secret_ keys, OpenAI, Anthropic and Stripe secret keys), checks whether your public Supabase key can read open tables without RLS, and checks security headers, SEO basics and broken links.
It is a public, read-only check. It never asks for your source code or login, and it masks any secret key it finds.
Pricing: the problems list is free (just add your email). The $9 fix report gives you a copy-paste fix prompt for every problem found, or get the $29 10-pack (10 fix reports to use any time). No subscription.
No tool can promise your app is 100% secure. VibeCheck catches common, costly mistakes before a stranger does.
8. FAQ
Is the Supabase anon key safe to expose in the browser?
Yes, as long as Row Level Security (RLS) is turned on for every table in your public schema and your policies are tight. Supabase designed the anon key (and the newer sb_publishable_ key) to sit in browsers and mobile apps. Without RLS, though, anyone with that public key can read and change your tables.
What is the difference between the Supabase anon key and the service_role key?
The anon key (or sb_publishable_ key) is a low-privilege public key that follows your Row Level Security rules. It runs as the anon Postgres role when a visitor is signed out, and as the authenticated role when a user signs in. The service_role key (or sb_secret_ key) is a master key for servers only. Its Postgres role has the BYPASSRLS attribute, so it skips every RLS policy.
What are sb_publishable_ and sb_secret_ keys in Supabase?
They are Supabase's newer API keys that replace the legacy JWT-based anon and service_role keys (which Supabase is deprecating by the end of 2026). sb_publishable_... is the public key for client code. sb_secret_... is the secret key for servers and Edge Functions.
What happens if my Supabase service_role or secret key is in my frontend?
Anyone who opens your site can copy it from the JavaScript bundle and get full read, write and delete access to your database, skipping all Row Level Security policies. Even though newer sb_secret_ keys return HTTP 401 when sent directly from a browser User-Agent, anyone who copies the key from your frontend code can still use it from curl or a script.
Why did my AI builder put SUPABASE_SERVICE_ROLE_KEY in my client code?
When a query fails with permission denied (error 42501) or returns an empty list [] because of missing grants or RLS policies, AI builders like Lovable, Bolt, v0, Replit and Cursor sometimes swap in the service_role key to make the query work right away. The right fix is to keep the publishable/anon key in the browser and add the needed table GRANT and RLS policy.
How do I rotate a leaked Supabase service_role or secret key?
First move the secret key out of your client code so the new key does not leak the same way. Then open Settings > API Keys in the Supabase Dashboard, create a new secret key (sb_secret_), update your backend servers to use it, and delete the old secret key (or deactivate the legacy service_role JWT key).
More questions? See the VibeCheck FAQ. Related guides: Supabase RLS check · Supabase error 42501 / Oct 30 change · Is my Lovable app safe? · Is NEXT_PUBLIC_ safe for API keys? · I leaked my API key. What do I do now? · How to check if your API key is exposed · Vibe coding security checklist.