The Supabase anon key is public. Row Level Security is what protects you
Why the Supabase anon (publishable) key may sit in your frontend, what Row Level Security does, the SQL to switch it on and check every table, and the secret key that must never reach a browser.
TL;DR
- Yes, the anon key (now called the publishable key) is meant to be public: Supabase (opens in a new tab) says it is safe in web pages, apps and source code.
- It is safe only because Row Level Security (RLS) decides which rows it can reach: a table in the
publicschema without RLS can be read, changed and deleted by anyone who has your project URL and that key. - Turn RLS on for every exposed table, then add a policy for each thing users may do; RLS with no policy blocks everything.
- The secret key (formerly
service_role) bypasses RLS and belongs on a server only, never in the browser, aVITE_orNEXT_PUBLIC_variable, or Git.
On this page
Yes, the Supabase anon key is safe to expose, and it is meant to be. Supabase now calls it the publishable key and says it is safe in a web page, a mobile app or your source code. What makes that true is Row Level Security (RLS): the key can only reach rows that your RLS policies allow. A table without RLS is open to anyone who has your project URL and that key, and both are in every visitor's browser. So the real question is not "is my key exposed" but "does every table have RLS on, with policies that say who may do what". It is part of the production checklist for a vibe-coded app.
The Supabase facts come from Supabase's documentation, checked on 11 November 2026. The SQL and the requests below were run that day against PostgreSQL (opens in a new tab) 18.6 in Docker (opens in a new tab), behind PostgREST 14.18 (the server behind Supabase's Data API, at the version Supabase's self-hosted setup pins), with the anon and authenticated roles and a stand-in for Supabase's auth.uid() function.
Why the Supabase anon key is safe to make public
Your frontend talks to Supabase directly. Supabase generates a REST API from your database schema (it is built on PostgREST), and supabase-js calls it from the browser. Every request carries a key so that Supabase knows which project it is for, and that key is in your JavaScript, readable in the browser's developer tools. There is no way to hide it, and that is fine by design.
The key does not grant access by itself. Supabase maps the publishable key to two Postgres roles: anon for a visitor who is not signed in, and authenticated for a signed-in user, whose access token travels with the request. What those roles can see is decided inside the database, by RLS policies. An attacker with your key has exactly the access an anonymous visitor has, and no more.
The keys, old and new
Supabase has renamed its keys, and a project can have both kinds:
| Key | Format | Where it goes | What it can do |
|---|---|---|---|
| Publishable key | sb_publishable_…, a short string | Browsers, mobile apps, anywhere public | What RLS allows the anon and authenticated roles |
anon (legacy) | A long-lived JWT | The same | The same |
| Secret key | sb_secret_…, a short string | Servers, Edge Functions, back-end jobs only | Everything: the service_role role bypasses RLS |
service_role (legacy) | A long-lived JWT | The same | The same |
Supabase is deprecating the legacy anon and service_role keys by the end of 2026. Creating new keys does not switch the old ones off: they stay valid until you disable them under Settings > API Keys. Send the new keys in the apikey header, not as Authorization: Bearer, because they are not JWTs.
What Row Level Security does
RLS is a Postgres feature. A policy acts like a WHERE clause that Postgres adds to every query on the table, for the roles the policy names. Supabase's docs put the danger plainly: a table in an exposed schema without RLS is "readable and writable by any role with a grant on it". On existing projects, new tables in public get grants for anon and authenticated automatically, so that means every visitor. Supabase says it is changing this default so that exposure becomes opt-in, and lets you switch off Automatically expose new tables in the Data API settings in the meantime.
Tables you create in the Supabase Dashboard have RLS enabled by default. Tables created with SQL do not: in the SQL Editor, in a migration file or through an AI builder that writes migrations for you, you have to enable it yourself.
What an open table looks like: a test
Here is a notes table with two users' private notes and no RLS, called the way a browser would call it, with only the public key (in the test setup, a request with no token runs as anon):
curl -s http://localhost:3901/notes[{"id":1,"user_id":"11111111-1111-1111-1111-111111111111","body":"Alice's private note"},
{"id":2,"user_id":"22222222-2222-2222-2222-222222222222","body":"Bob's private note"}]Every row comes back. Writing works just as well: a PATCH to /notes?id=eq.2 with {"body":"changed by a stranger"} changed Bob's note. On Supabase the same request goes to https://<project_ref>.supabase.co/rest/v1/notes?select=* with the publishable key in the apikey header, and anyone who opens your site has both values.
Turning RLS on, and writing policies
Enable RLS on the table:
alter table public.notes enable row level security;With RLS on and no policy, the anonymous requests now get:
| Request | Answer |
|---|---|
GET /notes | [], an empty list |
POST /notes | HTTP 401, new row violates row-level security policy for table "notes" |
PATCH or DELETE on a row | No error, but no row matched and nothing changed (asking for the changed rows returns []) |
Reads and updates do not fail with an error; they simply find no rows. If your app suddenly shows empty lists after you enable RLS, that is RLS working, and the fix is a policy, not turning RLS off.
Now allow each user to manage their own notes. These follow the examples in Supabase's RLS guide:
create policy "Users can view their own notes"
on public.notes for select
to authenticated
using ( (select auth.uid()) = user_id );
create policy "Users can create their own notes"
on public.notes for insert
to authenticated
with check ( (select auth.uid()) = user_id );
create policy "Users can update their own notes"
on public.notes for update
to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );
create policy "Users can delete their own notes"
on public.notes for delete
to authenticated
using ( (select auth.uid()) = user_id );
create index notes_user_id_idx on public.notes (user_id);using filters the rows a user can see or touch; with check validates the rows they write. auth.uid() is the signed-in user's id from their access token. Wrapping it in (select …) lets Postgres evaluate it once per query instead of once per row, and the index keeps the filter fast; both are Supabase's own performance advice.
The results, with Alice's access token:
| Request | Answer |
|---|---|
Anonymous GET /notes | [] |
Alice GET /notes | Only Alice's notes |
Alice inserts a note with Bob's user_id | HTTP 403, new row violates row-level security policy |
Alice inserts a note with her own user_id | HTTP 201 |
| Alice updates Bob's note | No error, no row matched: nothing changed |
Policy mistakes that undo RLS
- A condition that is always true.
create policy "Anyone can read notes" on public.notes for select using (true);names no role, so it applies topublic, every role. In the test, the anonymous request returned every row again. Supabase's advisor flags this pattern as0024_permissive_rls_policy. - A policy on a table without RLS. It has no effect at all until RLS is enabled (advisor
0007_policy_exists_rls_disabled). - Trusting
user_metadata. Users can edit their own metadata, so a policy that reads a role from it can be bypassed (advisor0015_rls_references_user_metadata). - Views. A view created by the
postgresuser ignores the RLS of the tables under it. On Postgres 15 and later, create itwith (security_invoker = true)so the caller's policies apply. security definerfunctions. They run with the privileges of the role that owns them, oftenpostgres, so the caller's RLS policies do not limit what they read or return. Keep them out of schemas the Data API exposes, or revokeexecutefrompublic,anonandauthenticatedand grant it only where it is needed; Supabase also says to setsearch_path = ''on each one.to authenticatedalone. It means "any signed-in user", not "the owner". Anyone can sign up, so a policy that should limit rows to their owner needs theauth.uid()comparison too.
How to check every table
Run this in the SQL Editor. It lists every table in public, whether RLS is on, and how many policies it has:
select c.relname as table_name,
c.relrowsecurity as rls_enabled,
count(p.policyname) as policies
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
left join pg_policies p on p.schemaname = n.nspname and p.tablename = c.relname
where n.nspname = 'public' and c.relkind in ('r', 'p')
group by c.relname, c.relrowsecurity
order by rls_enabled, table_name;On the test database, after adding a profiles table with SQL:
table_name | rls_enabled | policies
------------+-------------+----------
profiles | f | 0
notes | t | 4profiles is open: an anonymous GET /profiles returned its rows. A table with rls_enabled true and zero policies is closed to the API, which is safe but may break the feature that uses it. If your app exposes other schemas through the Data API, change 'public' to each of them.
Then list the policies that allow everything:
select tablename, policyname, cmd, roles
from pg_policies
where schemaname = 'public'
and (qual = 'true' or with_check = 'true');Every row here deserves a second look. A public read policy can be right for data that really is public, such as published blog posts; it is never right for profiles, orders or messages.
Supabase runs the same kind of checks for you. In the dashboard, open Advisors, then Security Advisor; from a terminal, supabase db advisors. The one to fix first is 0013_rls_disabled_in_public, at level ERROR: "Anyone with your project URL can read, edit, and delete all data in this table because Row-Level Security is not enabled."
Finally, test the way an attacker would: call your own project with only the publishable key and confirm that private tables come back empty and inserts are refused, then sign in as one test user and try to read another's rows.
The secret key never leaves the server
The secret key, and the legacy service_role key, map to the service_role role, which has the Postgres BYPASSRLS attribute. Supabase says they "provide full access to your project's data, bypassing Row Level Security". Use one only in server code, such as an Edge Function or your own API, for jobs that really need to skip your policies.
Supabase refuses a secret key sent from a browser with HTTP 401, which it detects from the User-Agent header. That stops an accident, not an attacker: someone who copies the key out of your JavaScript can use it from any script. Places a secret key must never be:
- a
VITE_,NEXT_PUBLIC_or other public variable, because those are compiled into the JavaScript every visitor downloads (see environment variables in production); - client-side code of any kind, "even on localhost", in Supabase's words;
- a Git repository, including old commits.
To check a built frontend, search its output folder for the key's prefix, for example grep -r "sb_secret_" dist/. To check the repository's history, use a secret scanner: scanning a Git repository for leaked secrets shows how.
If the secret key leaked
Supabase's rotation guide gives the order: fix the cause first, so the new key does not leak the same way; create a new secret key under Settings > API Keys; move every server and function to it; confirm nothing uses the old one; then delete the old key, which cannot be undone. For a leaked legacy service_role key, also move browser and mobile clients from the legacy anon key to a publishable key, deactivate both legacy keys, and then revoke the legacy JWT secret by moving to JWT signing keys: the legacy keys are JWTs signed with that secret, so anything that still trusts it accepts the leaked key. If someone is actively using the key, retire it at once and accept the breakage.
Doing this with OpsCrew
OpsCrew's free npx opscrew scan checks a repository for leaked secrets and exposed Supabase or Firebase (opens in a new tab) data before you deploy. On a test app shaped like a Lovable (opens in a new tab) export, it reported findings such as Row-level security is not enabled on table "profiles" and Policy "Anyone can update profiles" lets anyone update "public.profiles", each with the file and line behind it. It reads the repository, so run the SQL checks above against the live database as well: a table created only in the dashboard has no file to point to. See what OpsCrew checks.
Where this fits
RLS is one item, the most important one for a Supabase app, on the production checklist for a vibe-coded app. If you plan to run Supabase yourself rather than on Supabase Cloud, the same rules apply, and self-hosted Supabase vs Supabase Cloud covers what else changes.
Key facts
| Fact | Value | Source |
|---|---|---|
| Legacy anon and service_role keys | Being deprecated by the end of 2026Replaced by publishable (sb_publishable_) and secret (sb_secret_) keys. Creating new keys does not disable the legacy ones. | Understanding API keys (opens in a new tab)Verified on |
| A secret key sent from a browser | Refused with HTTP 401Supabase recognises browsers by the User-Agent header. Other tools can still use a leaked key. | Understanding API keys (opens in a new tab)Verified on |
| RLS on tables created in the Supabase Dashboard | Enabled by defaultTables created with SQL (SQL Editor, migrations, other tools) need it enabled explicitly. | Hardening the Data API (opens in a new tab)Verified on |
| Security Advisor check for a public table without RLS | 0013_rls_disabled_in_public, level ERROR | Database Advisors (opens in a new tab)Verified on |
Frequently asked questions
What is the difference between the anon key and the publishable key?
They do the same job. The anon key is the legacy version, a long-lived JWT; the publishable key is its replacement, a short string starting with sb_publishable_, sent in the apikey header.
Can I use the service_role key in the frontend?
No. The service_role key and the new secret key bypass Row Level Security and give full access to the project's data. Supabase refuses a secret key sent from a browser with HTTP 401, but anyone who copies it from your bundle can still use it from a script. Use it only on a server, and rotate it if it was ever exposed.
Sources
- Understanding API keys (opens in a new tab)
- Row Level Security (opens in a new tab)
- Hardening the Data API (opens in a new tab)
- Database Advisors (opens in a new tab)
- Rotate a leaked or compromised API key (opens in a new tab)
- REST API (opens in a new tab)
- Security: what OpsCrew sets up and checks (opens in a new tab)
Related articles
- Deploying
A preview of every pull request, on your own server
What per-pull-request preview environments need on your own server, a tested do-it-yourself setup with Docker Compose, Caddy and GitHub Actions, and what OpsCrew's previews add.
- Security
Backups for a small production server that you can restore
What to back up on a single-server app, how to send an encrypted copy off the server with pg_dump and restic, what the provider's backups add, and the restore drill that proves it works. Every command tested.
- AI and DevOps
AI DevOps agents for small teams: what they can do safely
What an AI agent for DevOps does well for a team without an ops person, where it goes wrong, and the rules that let it work on real infrastructure: read-only first, reviewed changes, approvals bound to what you reviewed, narrow credentials, no secrets in the chat.