Supabase Service Role Key: Where to Find It, How to Use It

The Supabase service role key is the legacy server-only API key that maps to the service_role Postgres role and bypasses Row Level Security. You find it, and its replacement the secret key, under Settings > API Keys in the dashboard. Use it only on servers you control, never in browser or mobile code.
Key takeaways
- The service role key, and the newer secret key that replaces it, run as the
service_rolerole and skip every RLS policy. Anyone who holds one can read and change every row. - Supabase is deprecating the legacy
anonandservice_rolekeys by the end of 2026. New code should use publishable keys (sb_publishable_...) and secret keys (sb_secret_...). - All keys live in the dashboard under Settings > API Keys. There is no separate Settings > API page any more.
- The anon or publishable key is public by design. An exposed anon key is only a problem when RLS is off or too loose.
- A leaked service role key means: fix the leak, create a secret key, switch to it, deactivate the legacy keys and revoke the legacy JWT secret.
Facts below were checked against the Supabase documentation on November 13, 2026. Key names and dashboard paths have changed more than once, so each section links the page that states them.
What is the Supabase service role key?
Every request to a Supabase project carries an API key that identifies which part of your app is calling: a web page, a mobile app, a server or an Edge Function. Supabase currently supports four key types (Supabase: API keys):
The service role key resolves to the built-in service_role Postgres role, which has the BYPASSRLS attribute. Your policies never run for it. That is what makes it useful for admin jobs, backups and webhooks, and what makes a leak expensive: whoever has the key has every row.
Supabase is deprecating the anon and service_role keys by the end of 2026 and points to the new keys instead (Supabase announcement on GitHub). A practical tell: publishable and secret keys are short strings, while the legacy keys are JWTs that begin with eyJ. If a tutorial or an AI assistant tells you to paste a long eyJ key, it was written for the legacy system.
Where to find the Supabase service role key
Pick the path that matches where you work (Supabase: find your keys):
- Dashboard. Open your project, then Settings > API Keys. New keys sit on the Publishable and secret API keys tab; the legacy
anonandservice_rolekeys sit on the legacy tab (/settings/api-keys/legacy). Older projects show a Create new API keys button, which adds the new keys without disabling the old ones. - Connect dialog. The Connect button shows the project URL and publishable key for your framework, ready to paste into
.env. It does not show the secret key. - Supabase CLI.
supabase projects api-keys --remote=your-project-reflists the keys for a project or a preview branch. - Local stack.
supabase startandsupabase statusprint a local publishable and secret key. They only work against your local containers.
Copy the value straight into your server's secret store or a git-ignored .env file. Supabase's own rules for secret keys include: never in a URL or query parameter, never in chat or email, never in logs, and never in source control or CI scripts (Supabase: handling secret keys safely).
Service role key vs anon key vs secret key: which one should you use?
Decide by where the code runs, never by what the code needs to do:
If a page needs data that RLS does not allow, the answer is a better policy or a server endpoint, and never the secret key in the browser. Supabase blocks secret keys in browsers by checking the User-Agent header and returning HTTP 401, but that only breaks your page; an attacker who copied the key can still use it from any other tool.
How do you use the service role key safely in server code?
Keep the two clients apart and name the variables so a bundler cannot ship the secret. This is the layout Supabase recommends:
# .env
# Safe to expose to the browser. Prefix per your framework.
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=sb_publishable_...
# Server-only. Never prefix these, or your bundler will ship the key.
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_SECRET_KEY=sb_secret_...
Next.js copies every variable that starts with NEXT_PUBLIC_ into the JavaScript it sends to the browser (Next.js: environment variables), and Vite does the same with VITE_ (Vite: env variables). A secret key behind either prefix is public.
The admin client lives in a file that only server code imports. Because the key skips RLS, the code that uses it has to do the authorization itself. This Next.js route handler checks who is calling before it reads anything:
// app/api/admin/orders/route.ts (runs on the server only)
import { createClient } from '@supabase/supabase-js'
const supabaseAdmin = createClient(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SECRET_KEY!,
{ auth: { autoRefreshToken: false, persistSession: false } }
)
export async function GET(request: Request) {
const token = request.headers.get('Authorization')?.replace('Bearer ', '')
if (!token) return new Response('Unauthorized', { status: 401 })
// getUser(jwt) validates the caller's access token with Supabase Auth
const { data: { user }, error } = await supabaseAdmin.auth.getUser(token)
if (error || !user || user.app_metadata?.role !== 'admin') {
return new Response('Forbidden', { status: 403 })
}
// The secret key bypasses RLS, so the check above is the only gate
const { data, error: queryError } = await supabaseAdmin
.from('orders')
.select('id, total, created_at')
if (queryError) return new Response('Error', { status: 500 })
return Response.json(data)
}
Three details matter here. getUser(jwt) asks the Auth server to validate the token, so the result can be trusted on a server (supabase-js: getUser). The role is read from app_metadata, which users cannot edit themselves. And the admin client is created with persistSession: false, as in the Auth admin reference.
Do not build the admin client with an SSR helper that reads the user's cookies, and do not call signUp on it. Either puts a user token into the Authorization header, and the client then runs under that user's RLS policies (Supabase troubleshooting). Use auth.admin.createUser() for admin user creation.
Inside an Edge Function, Supabase recommends the @supabase/server SDK, which verifies the caller's secret key and hands you a privileged client, so the key never appears in your code:
import { withSupabase } from 'npm:@supabase/server@1'
export default {
fetch: withSupabase({ auth: 'secret' }, async (_req, ctx) => {
// ctx.supabaseAdmin bypasses Row Level Security
const { data } = await ctx.supabaseAdmin.from('profiles').select('email')
return Response.json({ data })
}),
}
Is the Supabase anon key safe to expose?
Yes. The anon key, and the publishable key that replaces it, are built to sit in public code: web pages, mobile apps, GitHub Actions, CLIs. Supabase lists what a publishable key does not protect against, including reverse engineering, the browser's network inspector, and cross-site scripting (Supabase: what publishable keys don't protect against). Hiding it is not the defence.
The defence is Row Level Security. A request with the public key runs as anon or, with a signed-in user, authenticated, and reaches exactly what your grants and policies allow. For a public table with RLS switched off, the Security Advisor warns that anyone with your project URL can read, edit and delete all of its data (Supabase: lint 0013). So if you searched for "supabase anon key exposed", run this check:
- Open the Security Advisor in the dashboard and read every error, especially "Table publicly accessible".
- For each table in
public, confirm RLS is enabled and that each action has its own policy. - Sign in as a second test user and try to read and change the first user's records through the API.
Loose policies are the most common way a public key turns into a breach, and they sit near the top of our list of vibe coding security risks.
How do you check whether the service role key leaked?
Check three places: your environment variables, the built frontend, and git history. Run these from the project root, with the key loaded in your shell as SUPABASE_SECRET_KEY (or the legacy value):
# 1. Is a secret variable marked public?
grep -rnE "(NEXT_PUBLIC|VITE)_[A-Z_]*(SERVICE|SECRET)" .env* src app 2>/dev/null
# 2. Does the built frontend contain the key? (.next/static for Next.js, dist for Vite)
grep -rlF "$SUPABASE_SECRET_KEY" .next/static dist 2>/dev/null
grep -rl "sb_secret_" .next/static dist 2>/dev/null
# 3. Was a key ever committed, on any branch?
git log --all -p -S "sb_secret_"
git log --all -p -S "SERVICE_ROLE"
Then open the live site, view the loaded scripts in the browser's developer tools, and search them for the start of the key. A key deleted from the code is still in every old commit. In one audit, a local environment file had been committed to git history, and the keys in it had not yet been rotated.
What should you do if your service role key was exposed?
Follow Supabase's rotation guide in this order (Supabase: rotate a leaked or compromised API key):
- Fix the cause first. Move the key out of client code, the public variable or the repository, or the new key will leak the same way. If someone is actively using the leaked key, retire it immediately and accept the downtime.
- Create a new secret key in Settings > API Keys.
- Replace the old key everywhere it is used. If a Database Webhook or
pg_netcall sends the legacy key asAuthorization: Bearer, send the new secret key on theapikeyheader instead. - If the leaked key is the legacy service_role key, also replace the
anonkey with a publishable key in your browser and mobile clients, because retiring the compromised key deactivates both legacy keys. - Retire the compromised key. Delete a leaked secret key, or deactivate the legacy keys. Do not re-enable them later: that restores the compromised key.
- Revoke the legacy JWT secret when the leaked key was the legacy service_role key, by moving to JWT signing keys. Anything that still trusts that secret can accept the leaked key.
Then review what the key could have reached: every table, every Storage file, and Auth admin functions. Database logs in the dashboard are the place to start.
How do you migrate from service_role to secret keys?
Both key systems work at the same time, so the migration can go one component at a time (Supabase: migrating to new API keys):
- Create the new keys in Settings > API Keys. They are named
default. - Swap the
anonkey for the publishable key in client code. RLS behaves the same. - Swap the
service_rolekey for a secret key in server code. Consider one secret key per backend component, so one leak forces only one rotation. - In Edge Functions, read
SUPABASE_SECRET_KEYS, a JSON object keyed by name, instead ofSUPABASE_SERVICE_ROLE_KEY. - Move webhook and
pg_netcalls to theapikeyheader, with the key stored in Vault rather than in SQL. - When nothing uses the legacy keys any more, deactivate them.
// Edge Function: before
const secretKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
// after
const secretKey = JSON.parse(Deno.env.get('SUPABASE_SECRET_KEYS')!)['default']
What good looks like: Supabase key handling checklist
What good looks like
- Browser and mobile code use only the publishable (or anon) key.
- The secret or service role key is read only by server files, from a variable with no
NEXT_PUBLIC_orVITE_prefix. - A search of the built frontend and the live site finds no trace of the key, and neither does
git log --all -S. - Every server endpoint that uses the key checks who is calling before it queries.
- The Security Advisor lists no RLS errors, so the public key reaches only what the policies allow.
- The project has moved to publishable and secret keys, and the legacy keys are deactivated.
- Every key that ever leaked has been rotated.
Keys are one item on a launch review. The production readiness checklist for vibe-coded apps lists the others, and our guide to auditing a vibe-coded app shows how to check them.
Want to know what else is exposed?
Valletta Software runs a fixed-price vibe code audit, from $199, for apps built with AI tools. We read your source code, including migrations and policies, and we never touch production or customer data. The audit covers key handling, RLS, Storage policies and the rest of the app, with a written report.
Supabase service role key FAQ
Where do I find the Supabase service role key?
Open your project in the Supabase dashboard and go to Settings > API Keys. The legacy anon and service_role keys sit on the legacy keys tab, and the newer publishable and secret keys on their own tab. From the CLI, the supabase projects api-keys command lists the keys for any project you can access.
Is it safe to expose the Supabase anon key?
Yes, by design. The anon key and its replacement, the publishable key, are meant to ship in browser and mobile code, and anyone can read them. What protects your data is Row Level Security on every exposed table. If RLS is off or a policy is loose, the public key reaches whatever the policy allows.
Does the service role key bypass RLS?
Yes. Requests made with the service role key or a secret key run as the service_role Postgres role, which has the BYPASSRLS attribute and skips every policy. The exception is a request that also carries a user access token: it then runs under that user's policies, even when the client was created with the secret key.
Is Supabase deprecating the service role key?
Yes. Supabase says it is deprecating the legacy anon and service_role keys by the end of 2026 and recommends publishable keys (sb_publishable_) and secret keys (sb_secret_) instead. Both systems work side by side until you disable the legacy keys, so you can migrate one component at a time.
What is the difference between the secret key and the service role key?
Both give full access and bypass RLS. The secret key is a short string instead of a long-lived JWT, returns HTTP 401 when used from a browser, and you can create one per backend component and delete it on its own. The service_role key is the legacy JWT version that Supabase is retiring.
What should I do if my service role key leaked?
Fix the cause of the leak first. Then create a new secret key, switch every server component to it, replace the anon key with a publishable key in your clients, and deactivate the legacy keys. Supabase also says to revoke the legacy JWT secret, because it signed the leaked key.
Why does my service role client still get RLS errors?
Something put a user session into that client. A common cause is an SSR client that reads the user's cookies, or calling signUp on the admin client. The user's token replaces the key in the Authorization header, so RLS applies. Create a separate client from supabase-js for admin work.