Environment
Validated Supabase settings, with every framework spelling and no leaked values.
import { getEnv, publicEnv } from "better-supabase/env";
const env = getEnv(); // reads process.env, throws EnvValidationError
const browser = publicEnv(env); // { url, publishableKey }, safe to shipgetEnv() accepts the spellings frameworks use, the first one set wins:
| Setting | Variables |
|---|---|
url | SUPABASE_URL, with NEXT_PUBLIC_, VITE_, PUBLIC_, EXPO_PUBLIC_, NUXT_PUBLIC_ |
publishableKey | SUPABASE_PUBLISHABLE_KEY (same prefixes), SUPABASE_PUBLISHABLE_DEFAULT_KEY, SUPABASE_PUBLISHABLE_KEYS |
secretKey | SUPABASE_SECRET_KEY, SUPABASE_SECRET_KEYS |
dbUrl | SUPABASE_DB_URL, DATABASE_URL, POSTGRES_URL, POSTGRES_URL_NON_POOLING |
jwksUrl | SUPABASE_JWKS_URL, else derived from url |
jwks | SUPABASE_JWKS: inline keys as {"keys":[...]} or [...], used instead of fetching jwksUrl |
readUrl | SUPABASE_READ_URL, a read replica API URL (server only) |
jwtSecret | SUPABASE_JWT_SECRET, the HS256 secret Supabase Lite signs with (server only) |
The Vercel Supabase integration and Vercel Postgres set POSTGRES_URL (the
transaction pooler) and POSTGRES_URL_NON_POOLING. dbUrl prefers the pooled
one, which suits serverless functions and createPostgres(). Set
SUPABASE_DB_URL to the non-pooling URL when a connection needs a session,
such as LISTEN or session-level prepared statements.
What is checked
- The URL is
https, orhttponlocalhost,127.0.0.1or[::1]only. - Keys use the new format:
sb_publishable_…andsb_secret_…. LegacyeyJ…JWT keys are rejected with a pointer to the API Keys settings. - A secret key in a publishable variable is an error, not a warning.
SUPABASE_JWKSis JSON with at least one key that has akty, read the way@supabase/serverreads it.SUPABASE_JWT_SECREThas at least 32 characters. Onlybackend: 'lite'reads it.require: ['secretKey', 'dbUrl']makes optional settings mandatory.- Several
urlspellings, such asSUPABASE_URLandNEXT_PUBLIC_SUPABASE_URL, are read in that order; the first one set wins. Doctor's BS506 warns when an env file gives them different URLs, since the session cookie name comes from the URL. SUPABASE_PUBLISHABLE_KEYSandSUPABASE_SECRET_KEYSmust be JSON objects of key names to keys. All secret keys are kept inenv.secretKeys(the single key isdefault), so a server can accept named keys.
Error messages name the variables and never include their values, so they are safe to log.
Inline signing keys
Set SUPABASE_JWKS on the server so it verifies tokens without fetching the
key set when a function starts. Copy the project's public keys into it:
curl https://<project-ref>.supabase.co/auth/v1/.well-known/jwks.jsonPaste the output as the value, on one line. The keys are public, but keep the
variable server-only so a stale copy never reaches a browser bundle. Refill it
when you rotate a signing key: a token signed with a key the variable lacks
fails to verify. Leave it empty to fetch SUPABASE_JWKS_URL instead.
better-supabase doctor reports a value that doesn't parse
(BS509) and an exported value that hides the local
stack's keys (BS503).
createServer loads the env this way unless you pass one with its env
option, which also takes the variables as an object and then reads nothing
else. The order it looks up token keys in is on the
createServer page.
Standard Schema
envSchema() is the same validator as a Standard Schema,
for t3-env, framework config or any other validator slot:
import { envSchema } from "better-supabase/env";
const result = await envSchema({ require: ["secretKey"] })[
"~standard"
].validate(process.env);With @supabase/server
toServerEnv(env) returns the SupabaseEnv that withSupabase({ env })
and the @supabase/server core functions take.
Client bundles
Next.js only inlines NEXT_PUBLIC_* variables that are read literally. In
browser code, pass them explicitly:
getEnv({ NEXT_PUBLIC_SUPABASE_URL: process.env.NEXT_PUBLIC_SUPABASE_URL, … }).
Last updated on