Supabase database

Is it safe to expose the Supabase anon key?

Yes, but only if row level security (RLS) is on for every table and its policies are right. The anon key, and the publishable key that replaces it, is built to sit in browser code because it reaches only what your RLS policies allow. The service_role key, now the secret key, is never safe to expose.

TL;DR

  • The anon key is public by design. The rules on your tables, not the key, decide what it can read.
  • Supabase’s docs call the publishable key safe to expose online and describe the anon key as its legacy version.
  • The service_role (secret) key bypasses RLS. It must never be in a browser, a repo or a bundle.
  • In the PatchLabs study, 65 of 318 apps (20%) let anyone with the public key read personal data (re-checked 24 Nov 2026). The key was fine. The rules were missing.

Why is the anon key public by design?

A Supabase app talks to the database from the browser. To do that, the page needs your project URL and a key, so both end up in your JavaScript. Anyone can open the developer tools and copy them. Supabase designs for this. A request made with the key runs as the Postgres role anon for a visitor, or authenticated for a signed-in user, and RLS policies decide what those roles can see.

So the key identifies your project and nothing more. It does not grant power. The policies grant power. Supabase’s docs say the publishable key only reaches what RLS allows, and tell you to enable RLS on every table before you deploy.

Which Supabase keys are safe to expose?

KeyIn browser code?What it does
Publishable key (sb_publishable_…)Yes. Supabase docs: safe to expose onlineRuns as anon or authenticated. Reaches only what RLS allows
anon key (legacy)Yes, under the same condition. Supabase describes it as the legacy version of the publishable keyRuns as anon. Supabase says it is deprecating the anon and service_role keys by the end of 2026
Secret key (sb_secret_…)NeverBypasses every RLS policy. Backend code only
service_role key (legacy)NeverThe legacy version of the secret key. Same rule

The safe/unsafe line is simple. If the key can be used without RLS getting a say, it must stay on a server. Supabase lists the places a secret key must never appear: client-side code, browsers (including on localhost), chat, email, URLs, and source control.

What can a stranger do with your anon key?

They can send the same requests your app sends. Run this against your own project to see what they would get:

what-a-stranger-sees.sh
# Run this against your own project only
curl "https://YOUR-PROJECT.supabase.co/rest/v1/profiles?select=*&limit=5" \
  -H "apikey: YOUR_PUBLIC_KEY"
# [] or a permission error is what you want. Rows of personal data are a leak.

With RLS on and no matching policy, you get nothing back. With RLS off, or a policy that says yes to everyone, you get the rows. A second group is easy to miss: anyone can create a throwaway account, which gives them the authenticated role. In the PatchLabs study, 65 of 318 apps let anyone with the public key read personal data, and 32 more let anyone who signs up read it (24 Nov 2026 re-check).

How do I check my key is safe to expose?

  1. Find out which key you have. Paste your code or bundle into the free key check. It tells an anon key from a service_role key and runs in your browser.
  2. Confirm RLS is on for every table. Run the query in how to check RLS on every table. (PL-008)
  3. Test as anon and as a stranger. Use the three-identity script. Personal tables should return 0 rows. (PL-001, PL-002)
  4. Search for the secret key. Check the code, the built bundle and git history. (PL-010, PL-011)
  5. Rotate anything that leaked. Roll the key first, then clean up: rotate a leaked key.

Should I hide or rotate the anon key?

Hiding it does not work, because the browser needs it. Moving it to an environment variable keeps it out of your repo but not out of your built site. Rotating it is sensible if your team wants to, but it does not change what your policies allow. If a table leaks, the new key leaks it too.

Spend the effort on the rules instead. A tested policy makes the public key harmless. A missing policy makes it a master key for that table.

Questions people ask

Is the anon key the same as the publishable key?

Not exactly. Supabase’s API keys page describes the anon key as the legacy version of the publishable key and says Supabase is deprecating the anon and service_role keys by the end of 2026. Both work at the same time until you disable the legacy ones. The same rule applies to both: RLS decides what they can read.

Can someone read my whole database with the anon key?

Only the tables whose rules allow it. If RLS is off on a table, or a policy lets everyone read, they can read it. If RLS is on with owner-only policies, they get nothing. That is why every table needs testing.

Is it safe to commit the anon key to GitHub?

It is already public in your built site, so committing it is not a new exposure. Keeping it in an environment variable makes it easier to rotate. The service_role or secret key must never be committed.

Is the service_role key ever safe in the frontend?

No. Supabase says a secret key bypasses every RLS policy and must stay in backend code you control. If you find one in client code, treat it as leaked and rotate it.

Why do AI app builders put the key in the code?

Because the browser needs it to reach the database. That is normal. The risk is not the key. It is the rules behind it, and tables created quickly can end up with none.

Sources

The checks behind this guide, each with example code, a self-check and the fix.

Free check

Check your app for this, free.

Paste your public repo. PatchLabs rebuilds your database from its migrations, tests it as a visitor, a stranger and the owner, reads the code for keys and risky patterns, and hands you the fix.

First app free, fixes includedNever touches your database

Written by Manraj Singh Dhillon. Last checked 11 Nov 2026. Code samples are synthetic and run against your own project; nothing here names a real app. Spotted a mistake? Tell us.