All guidesSupabase RLS and keys

Supabase question

Where do I find the Supabase service role key?

By Ohad Krispin, founder of VibeRaven · LinkedIn

Short answer

Open your Supabase project → Settings → API Keys and copy the legacy key labeled service_role. For new server code, use an sb_secret_ key from the same page. Both bypass Row Level Security. Store them only on the server.

Use this from your agent
  • Claude Code: 7 read-only tools; public repos only.
    claude mcp add --transport http viberaven https://viberaven.dev/mcp
  • Codex: same server, from the Codex CLI.
    codex mcp add viberaven --url https://viberaven.dev/mcp
  • In your repo: writes Vercel + Supabase check rules for your agent (what it writes).
    npx -y viberaven init
Paste this to your agent
Run `npx -y viberaven@1.20.2 check` in this repo and fix service_role or sb_secret_ keys in client code, NEXT_PUBLIC_ or VITE_ variables. Help me find my own key in Settings > API Keys without printing it. Report live checks you could not run.

Open your app's repo in your coding agent, then paste this request.

Terminal output of npx viberaven check on a Next.js fixture repo, flagging NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY in .env.example as a Supabase service role key in a client-prefixed env variable.
Terminal output of npx viberaven check on a Next.js fixture repo, flagging NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY in .env.example as a Supabase service role key in a client-prefixed env variable.
On this page

Which key is the tutorial asking for?

Setting up a browser client? Copy the legacy anon key or its publishable replacement instead.

Migrating a backend job? Use the API key migration guide to check what that job sends before replacing its credential.

If your backend is on Vercel, save it as a Secret variable and select the environment where that job runs.

Two keys start with eyJ: read the label

The legacy anon key and the legacy service_role key are both JWTs that start with eyJ, and they sit next to each other on the same page. The anon key is public; service_role is not. Read the label before you copy.

If you see a Create new API keys button instead of an sb_secret_ key, the project has only legacy keys so far. Creating new ones leaves service_role working. There is no separate Settings > API page any more, so a tutorial that sends you to "Project API keys" was written for the old dashboard. Dashboard paths checked against Supabase’s API keys and migration guides on November 29, 2026.

SUPABASE_SERVICE_ROLE_KEY: where does it come from?

In your own server code it is just a variable name: you paste the key from Settings > API Keys into it, in .env.local and in your host's environment variables, with no NEXT_PUBLIC_ or VITE_ prefix. Supabase's own examples name the new key SUPABASE_SECRET_KEY.

In Supabase Edge Functions it is already set. Supabase says SUPABASE_ANON_KEY and SUPABASE_SERVICE_ROLE_KEY "still exist in the runtime, but they carry the legacy keys", and SUPABASE_SECRET_KEYS holds the secret keys. You never paste a key into a function.

On a local stack started with supabase start, run supabase status: the publishable and secret keys are under Authentication Keys, and the local secret key takes the place of the local service_role key.

Terminal
supabase projects api-keys --remote=YOUR_PROJECT_REF

Why it stays on the server

Supabase says secret keys "provide full access to your project's data, bypassing Row Level Security", lists service_role as their legacy version, and warns: "Never put one in a browser, a shipped application, or source control." A public prefix puts it there: Next.js inlines a NEXT_PUBLIC_ value "into any JavaScript sent to the browser", and Vite says VITE_ variables "will be exposed in client-side source code". A secret key sent from a browser gets 401 Unauthorized, but Supabase notes that "an attacker can still use the key from other tools".

Not sure whether yours is out? Check a public .env, your domain, your bundle and GitHub alerts.

Copy the key and store it

  1. 1
    Open your project in the Supabase Dashboard, then Settings > API Keys.
  2. 2
    For new code, take a secret key (sb_secret_) from the Publishable and secret API keys tab. If there is none yet, create one: Supabase says it is added "alongside your existing anon and service_role keys without affecting them".
  3. 3
    Store it as a server-only variable. Supabase's rule for server keys is "Never prefix these, or your bundler will ship the key":
    Environment
    # .env.local: read only in server code, API routes or Edge Functions
    SUPABASE_SERVICE_ROLE_KEY=<copied from Settings > API Keys>
    
    # Wrong: these prefixes ship the value to every visitor
    # NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY=
    # VITE_SUPABASE_SERVICE_ROLE_KEY=
  4. 4
    Set the same variable in your host's environment settings, then redeploy.

Where people put the key by mistake

We read 512 public Lovable repos with a supabase/migrations folder, each at its last commit before October 3, 2026. 29 of them (5.7%) commit a Supabase service role or secret key. The key was rarely in the app itself: it sat in the files around the app. No key was tested and no project is named.

Which key is which

The four Supabase API keys, where each one is, and whether it may reach the browser
KeyStarts withWhere in the dashboardIn the browser?
anon (legacy)eyJSettings > API KeysYes, with RLS on
publishablesb_publishable_Publishable and secret API keys tabYes, with RLS on
service_role (legacy)eyJSettings > API Keys, labelled service_roleNo
secretsb_secret_Publishable and secret API keys tabNo

Where the 29 repos had it

In our sample, 25 of the 29 repos contained only the legacy service_role key. Check one-off scripts, env templates and setup notes when moving a server credential.

Where 29 of 512 public Lovable repos commit a Supabase service role or secret key. A repo can appear in more than one row.
Where the key wasRepos
A one-off script: fix an admin account, seed data, import users12
.env.example or another env template, where a placeholder should be7
A Markdown deploy guide or setup note7
A committed .env file6
App code the browser loads3

Supabase and framework documentation

FAQ for builders before launch

Should I copy the service_role key or a secret key?

A secret key, for new code. Supabase lists service_role as the legacy version of secret keys, both bypass Row Level Security, and Supabase is deprecating the anon and service_role keys in early 2027. Secret keys (sb_secret_) are short strings, not JWTs, and you can make one per server component. A legacy key keeps working until you disable it in Settings > API Keys.

Why can't I find the service role key in local Supabase Studio?

A local stack started with supabase start prints a publishable key and a secret key, under Authentication Keys, and Supabase says the local secret key takes the place of the local service_role key. Run supabase status to print them again while the stack is running.