Secure Environment Variables in Next.js

Feb 22, 2025

1 min read

Use .env.local for local secrets. Example:

DATABASE_URL=postgres://user:password@host:port/db
NEXT_PUBLIC_API_KEY=xyz123

const db = process.env.DATABASE_URL ?? throw new Error("Missing DB URL");

On Vercel, set secrets in the dashboard under "Environment Variables".