Skip to content

Environment Variables

This page covers every environment variable the boilerplate reads, when each is required, and how to add your own with runtime validation.

Secret key for PayloadCMS authentication. Generate one at payloadsecret.io.

Terminal window
PAYLOAD_SECRET=your-secure-secret

PostgreSQL connection string.

Terminal window
# Local (DDEV)
DATABASE_URI=postgres://db:db@db:5432/db
# Production
DATABASE_URI=postgres://user:pass@host:5432/dbname

Your site’s public URL.

Terminal window
# Local
NEXT_PUBLIC_SERVER_URL=https://yourproject.ddev.site
# Production
NEXT_PUBLIC_SERVER_URL=https://yourdomain.com

Terminal window
APP_ENV=development # development | staging | production

Secret for admin account recovery when locked out.

Terminal window
PAYLOAD_ADMIN_RECOVERY_SECRET=your-secure-secret-here

Recovery steps:

  1. Set this in .env
  2. Call the recovery endpoint:
Terminal window
curl -X POST http://localhost:3000/admin-recovery \
-H "Content-Type: application/json" \
-d '{"secret": "your-secure-secret-here", "email": "your@email.com", "password": "newpassword"}'
  1. If the email exists, their role will be updated to admin
  2. If the email doesn’t exist, a new admin user will be created (password required)

Note: If not set, recovery is disabled.

How many document versions to retain.

Terminal window
PAYLOAD_MAX_VERSIONS_TO_KEEP=10 # Default: 3

Display name for SEO and OpenGraph.

Terminal window
NEXT_PUBLIC_SITE_NAME="My Project"

VariableValue
DATABASE_URICoolify Postgres connection string
NEXT_PUBLIC_SERVER_URL$COOLIFY_URL

New variables must be registered in three places to be validated at runtime.

  1. Add to .env.example

    .env.example
    NEXT_PUBLIC_FEATURE_FLAG=false
  2. Add validation in src/env.ts

    src/env.ts
    client: {
    NEXT_PUBLIC_FEATURE_FLAG: z.boolean().default(false),
    },
    experimental__runtimeEnv: {
    NEXT_PUBLIC_FEATURE_FLAG: process.env.NEXT_PUBLIC_FEATURE_FLAG,
    },
  3. Set the local value in .env

    .env
    NEXT_PUBLIC_FEATURE_FLAG=true

  • NEXT_PUBLIC_* — Exposed to browser
  • PAYLOAD_* — PayloadCMS server-only
  • APP_* — Application settings

ErrorSolution
”Missing required variable”Check .env file exists and contains the variable
”Invalid DATABASE_URI”Verify PostgreSQL connection string format
Changes not workingRestart: ddev pnpm dev