Blog · November 13, 2026 · 9 min read

Supabase publishable key vs secret key, explained

Supabase publishable vs secret key (and anon vs service_role): which one is safe in the browser, how to spot a leaked secret key, and how to rotate it.

Open the API Keys page of almost any Supabase project and you will see two kinds of keys. One is fine to put in your website. The other one opens your whole database to anyone who has it. They sit right next to each other, they both start with sb_, and AI coding tools mix them up more often than you would like.

This guide explains the difference in plain words, shows how the old anon and service_role keys map onto the new ones, and gives you a ten-minute check to make sure the dangerous key is not sitting in your app's JavaScript.

The short version

Key Looks like Where it belongs What it can reach
Publishable key sb_publishable_... Browser, mobile app, anything you ship Only what your Row Level Security policies allow
Secret key sb_secret_... Servers, Edge Functions, cron jobs you control Everything. It skips Row Level Security completely
anon (legacy) Long JWT starting with eyJ Same as publishable Same as publishable
service_role (legacy) Long JWT starting with eyJ Same as secret Same as secret

Supabase's own docs put it nicely: the publishable key is taped to the front door and only opens the lobby, the secret key is the master key and stays in your pocket.

Supabase is deprecating the legacy anon and service_role keys by the end of 2026, so new tutorials and new projects use publishable and secret keys. Many apps built with Lovable, Bolt or older templates still run on the legacy pair, and both systems work side by side until you switch the old ones off.

Why the publishable key is allowed to be public

A Supabase app talks to the database straight from the browser. To do that, the browser needs your project URL and a key, so the key ends up in the JavaScript every visitor downloads. There is no way to hide it, and you don't need to.

What protects your data is not the key, it is the Postgres role behind it. Every request made with the publishable key runs as one of two roles:

Your Row Level Security (RLS) policies decide what those two roles can read and write. If RLS is on and your policies are right, a stranger holding your publishable key can only do what a logged-out visitor is supposed to do.

That is also the catch. The publishable key is only safe if RLS is on for every table that the Data API can reach. If one table has RLS turned off, or a policy says "anyone can read", your publishable key becomes a read key for that table. That is exactly what the large scans of Supabase sites this autumn found: thousands of apps with readable tables, all through the public key. If RLS still feels fuzzy, start with Supabase RLS in plain English.

Why the secret key must never leave your server

The secret key runs as the service_role Postgres role. That role has the BYPASSRLS attribute, which means none of your policies apply to it. Anyone holding it can read every row, change any user's data, and delete tables.

It is meant for code that runs somewhere you control and that does its own permission checks, for example:

The new secret keys have one extra safety net that the old service_role key did not: if a request with a secret key comes from a browser, Supabase recognises the browser's User-Agent and answers with 401 Unauthorized. That is helpful, but it is not protection. An attacker who copies the key out of your JavaScript simply uses it from a script or curl, where that browser check doesn't apply. A leaked secret key is still a full leak.

How AI tools end up shipping the secret key

We see the same few patterns again and again in vibe-coded apps:

  1. "It didn't work, so I used the other key." A query returns an empty list because RLS blocks it. The AI suggests switching to the service role or secret key, the query suddenly works, and the fix ships. The real problem was a missing policy, and now the database has no protection at all.
  2. The wrong prefix. Frameworks copy variables with certain prefixes into the browser bundle on purpose: NEXT_PUBLIC_ in Next.js and v0, VITE_ in Vite apps like Lovable and Bolt. A line like VITE_SUPABASE_SERVICE_ROLE_KEY=... or NEXT_PUBLIC_SUPABASE_SECRET_KEY=... puts the master key in every visitor's browser.
  3. One shared client file. The AI creates a single supabase.ts with the secret key "for convenience" and imports it from both server code and page components.
  4. Pasted into the code. The key is hardcoded in a file and committed to GitHub, so it lives in your git history even after you delete the line.

None of these break your app, which is why nobody notices. Everything works, it just works for strangers too.

The ten-minute check

1. Look at your environment variables

Open your .env file (and the environment settings in Vercel, Netlify, Lovable or wherever you deploy). You want to see something like this:

# Fine in the browser
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=sb_publishable_...

# Server only, no public prefix
SUPABASE_SECRET_KEY=sb_secret_...

Red flags: any variable with a NEXT_PUBLIC_ or VITE_ prefix whose value starts with sb_secret_, or whose name contains SERVICE_ROLE or SECRET.

2. Search your live site's JavaScript

Open your live site, then open the browser's developer tools. In Chrome, go to the Sources tab, press Ctrl+Shift+F (Cmd+Option+F on a Mac) and search for:

Finding sb_publishable_ or a JWT with "role": "anon" is normal. For a wider look at what else can hide in your bundle, see Your API key is probably sitting in your JavaScript.

3. Search your code and git history

In your project folder, search the whole codebase for the same strings. Then check history, because a deleted key is still in old commits:

git log -p | grep -n "sb_secret_\|service_role" | head

If anything shows up in a public repo, treat the key as leaked even if the current code is clean.

4. Check where the secret key is imported

Find every file that reads the secret key. Each one should be server-only code: an API route, a server action, an Edge Function or a worker. If a React component or a file imported by one reads it, move that logic to a server route.

If you find a leaked secret key

Fix the cause first, then rotate. Otherwise the new key leaks the same way.

  1. Remove the key from the client side. Move the logic to a server route or Edge Function, and rename the variable so it has no public prefix.
  2. Create a new secret key in your Supabase project under Settings > API Keys. You can create one key per backend component, so a future leak only forces one rotation.
  3. Update every backend that used the old key: servers, Edge Functions, cron jobs, CI secrets, Database Webhooks.
  4. Retire the old key. For a new-style secret key, delete it on the API Keys page. Deleting can't be undone, so do it after step 3 unless someone is actively abusing it. For a legacy service_role key, switch your browser and mobile clients to the publishable key as well, then deactivate the legacy keys on the same page. That turns off both anon and service_role together.
  5. Check what happened while it was exposed. Look at your tables for rows you don't recognise, changed roles or plans, and new admin users. If the table holds personal data, think about whether you need to tell your users.

While you are there: turn the publishable key into a safe key

Once the secret key is out of the browser, your security depends entirely on RLS. Three quick checks:

You can test the result from the outside with the publishable key alone: check if your Supabase database is exposed shows how to ask each table for a row count while logged out.

Quick reference

Check your app in a minute

Start with the outside view. Run the free Greenlight Scan on your live URL: it looks for secret keys in your JavaScript, including Supabase secret and service_role keys, plus weak headers and known-vulnerable libraries, and gives you a traffic-light result in about a minute. The $19 Full Scan adds deeper checks once you verify you own the site, like open Supabase or Firebase tables (counted, never downloaded), exposed files and API routes that answer without a login, with a fix prompt for your AI tool for every finding.

Some of the mistakes above live in your code, where no outside scan can see them: a secret key in your git history, a shared client file, a server route that trusts whoever calls it. If your app takes payments or stores personal data, the $79 Launch Audit connects read-only to your GitHub repo, scans the history for committed secrets and runs a deep AI review of your auth, database policies and server routes. A second AI double-checks every finding, and you get a fix plan and a launch verdict. It's automated, not a pentest, but it catches the key mix-ups AI tools make most often before someone else does.

Newsletter

The Friday leak report

One short email a week: the newest ways AI-built apps leak data, and the one-line fix for each. No spam, unsubscribe any time.