Overview
Environment variables are stored in a.env file at the root of your project. The template includes a .env.example file showing all required variables:
.env.example
Getting Started
To set up your environment variables:Required Variables
NEXT_PUBLIC_SERVER_URL
The base URL where your Next.js application is running. This is used by the ORPC client to make API requests.
The
NEXT_PUBLIC_ prefix makes this variable accessible in the browser. It’s used by the ORPC client for making RPC calls.DATABASE_URL
The connection string for your database. Supports SQLite file URLs and Turso (libSQL) remote URLs.
Optional Variables
While not shown in.env.example, the following variables are used by Better Auth when configured:
BETTER_AUTH_SECRET
Secret key used by Better Auth for signing tokens and encrypting session data. Must be a strong, random string.
src/lib/auth.ts:
src/lib/auth.ts
BETTER_AUTH_URL
The base URL for Better Auth endpoints. Should match your application’s URL.
src/lib/auth.ts:
src/lib/auth.ts
CORS_ORIGIN
The origin(s) allowed to make requests to your API. Used for CORS configuration in Better Auth.
src/lib/auth.ts:
src/lib/auth.ts
If you have multiple frontend domains (e.g., separate marketing site), you can specify multiple comma-separated origins.
Complete Example
Here’s a complete.env file example:
Development
.env
Production
.env.production
Environment-Specific Configuration
Using Multiple .env Files
Next.js supports multiple environment files:.env- Loaded in all environments.env.local- Local overrides (ignored by git).env.development- Development-specific.env.production- Production-specific
Loading Priority
Next.js loads environment files in this order (later files override earlier ones):.env.env.local.env.developmentor.env.production.env.development.localor.env.production.local
Accessing Environment Variables
Server-Side
Server-side code can access any environment variable usingprocess.env:
Client-Side
Only variables prefixed withNEXT_PUBLIC_ are accessible in the browser:
Security Best Practices
Generate Strong Secrets
Always use cryptographically secure random strings for secrets:
Never Commit Secrets
Add
.env to .gitignore and never commit sensitive values to version control.Use Different Secrets
Use unique secrets for each environment (dev, staging, production).
Rotate Regularly
Periodically rotate secrets, especially if they may have been exposed.
Deployment Platforms
How to set environment variables on popular platforms:Vercel
- Go to your project settings
- Navigate to “Environment Variables”
- Add each variable with the appropriate environment (Production, Preview, Development)
Netlify
- Go to Site settings > Environment variables
- Add each variable with values for different contexts
Railway
- Go to your project
- Click on “Variables”
- Add each environment variable
Docker
Pass environment variables using:Troubleshooting
Variables Not Loading
If environment variables aren’t loading:- Restart your development server after changing
.env - Verify the variable name is correct (case-sensitive)
- For client-side access, ensure it has the
NEXT_PUBLIC_prefix - Check that
.envis in the project root
Undefined in Production
If variables work locally but not in production:- Verify you’ve set them in your deployment platform
- Check that you’ve selected the correct environment (Production vs Preview)
- Redeploy after adding variables
CORS Errors
If you’re getting CORS errors:- Verify
CORS_ORIGINmatches your frontend URL exactly - Check for trailing slashes (be consistent)
- Ensure
BETTER_AUTH_URLmatches your deployment URL
Next Steps
Database Setup
Configure your database with the DATABASE_URL variable
Deployment
Learn how to deploy with the correct environment configuration