Set the authentication encryption key
Application initialization
Better Auth · Pipe
secret configures the value Better Auth uses for encryption, signing, and hashing. It can be set explicitly or supplied through BETTER_AUTH_SECRET or AUTH_SECRET.
The secret option supplies the value Better Auth uses for encryption, signing, and hashing. If omitted, Better Auth checks BETTER_AUTH_SECRET and then AUTH_SECRET; production configuration must provide a secret rather than relying on the built-in fallback.
betterAuth({ secret: "your-secret-key" })| Name | Type | Description |
|---|---|---|
secret | string | A secret value used for encryption, signing, and hashing; it can be supplied as a configured string. |
A Better Auth configuration option; it does not return a value by itself.
BETTER_AUTH_SECRET is the first environment variable checked by the default lookup.AUTH_SECRET is the second environment variable checked by the default lookup.import { betterAuth } from "better-auth";
export const auth = betterAuth({
secret: process.env.BETTER_AUTH_SECRET,
});Configure the authentication instance with a secret supplied through the environment.
Application initialization
Environment-based deployment
Secret migration
secret option?The secret option is the key Better Auth uses for encryption, signing, and hashing. It can be supplied directly or through BETTER_AUTH_SECRET or AUTH_SECRET.
Follow-up: What happens if no value is configured in production? Better Auth throws an error rather than continuing with the default.
Better Auth first looks for process.env.BETTER_AUTH_SECRET, then process.env.AUTH_SECRET. If neither exists, it uses its documented fallback value, although production should configure a real secret.
Follow-up: How should a secret be generated? Use a high-entropy value; the documentation provides openssl rand -base64 32.
secret relate to secret rotation?The plural secrets option supports versioned secret rotation. During migration, the singular secret can remain as a fallback for decrypting legacy data within the same encryption purpose.
Follow-up: Does retaining the old singular secret preserve every old state artifact? No; the documentation specifically excludes pending OAuth or SAML state cookies and OAuth Proxy payloads when changing to purpose-specific keys.
secretsBETTER_AUTH_SECRETAUTH_SECRETsecurityBETTER_AUTH_SECRETS