.env · generator · Supabase

Supabase .env Generator

Supabase gives you four things per project: URL, anon key, service-role key, and JWT secret. The first two are client-safe (protected by Row Level Security). The last two are server-only and bypass RLS entirely.

.env · Supabase · 4 keys
Customize →
NEXT_PUBLIC_SUPABASE_URL=https://xxxx.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=
SUPABASE_SERVICE_ROLE_KEY=
SUPABASE_JWT_SECRET=

Secrets regenerate on every page load. Want to combine stacks? Use the full generator.

How to use this .env in Supabase

  1. 1.Open Supabase Dashboard → Project Settings → API to find all four values.
  2. 2.Paste into .env.local — NEXT_PUBLIC_ prefixes mark the client-safe ones.
  3. 3.Ensure RLS is enabled on every public table before shipping — the anon key has read/write access otherwise.
  4. 4.The service-role key must never ship to the browser. Use it only in API routes and server actions.

Gotchas

  • ⚠If you forget to enable RLS, anyone can read/write via the anon key. Always enable RLS first.
  • ⚠Rotating the service-role key logs out every server that uses it — coordinate the deploy.
  • ⚠Supabase Edge Functions read from a separate secrets vault, not your app's .env.

Common keys explained

NEXT_PUBLIC_SUPABASE_URL

https://xxx.supabase.co — safe to expose.

NEXT_PUBLIC_SUPABASE_ANON_KEY

Public key. Safe to expose — RLS protects data.

SUPABASE_SERVICE_ROLE_KEY

Admin key. Bypasses RLS. Never prefix with NEXT_PUBLIC_.

SUPABASE_JWT_SECRET

Verify or sign custom JWTs. Server-only.

Loading and validating the .env in Supabase

lib/supabase.ts
import { createClient } from "@supabase/supabase-js";

// Browser + server: anon key, protected by Row Level Security
export const supabase = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL!,
  process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
);

// Server only (route handlers, cron jobs): bypasses RLS
export const supabaseAdmin = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL!,
  process.env.SUPABASE_SERVICE_ROLE_KEY!,
  { auth: { persistSession: false } },
);

Supabase: env vars in development, staging and production

The anon key and the service role key are both JWTs signed with your project's JWT secret, but they carry different roles. anon is limited by your RLS policies. service_role ignores RLS completely. If a table has RLS disabled, the anon key alone can read and write it, so enable RLS on every table in the public schema.

For local development, supabase start runs the whole stack in Docker and prints a local API URL, anon key and service role key. Use a separate Supabase project for staging so migrations can be tested before production. Newer projects can use publishable (sb_publishable_) and secret (sb_secret_) keys instead of the JWT-based pair.

Supabase: .env errors and fixes

new row violates row-level security policy for table

The anon or user client has no INSERT policy. Add a policy for the authenticated role, or do the write from the server with the service role key.

Invalid API key

The key belongs to another project or environment (local vs hosted), or it was rotated. Copy both URL and key from the same project's API settings.

Service role key visible in the browser bundle

It was given a NEXT_PUBLIC_ prefix or imported into a client component. Rotate the key immediately and keep it server-only.

Supabase .env FAQ

Is it safe to expose the Supabase anon key?

Yes, if RLS is enabled on all tables with sensible policies. The anon key is designed for client use.

When do I need the service role key?

For trusted server tasks that must bypass RLS: admin dashboards, webhooks, background jobs. Never in client code.

What is SUPABASE_JWT_SECRET for?

Verifying Supabase-issued JWTs in your own backend. Most apps can call supabase.auth.getUser() instead and never need it.

Related tools

Other stacks