Required variables
These environment variables must be set for the application to function properly.AUTH_SECRET
Secret key used by NextAuth.js to encrypt session tokens and sign cookies.How to generate:Or use the online generator: https://generate-secret.vercel.app/32
POSTGRES_URL
PostgreSQL database connection string for storing user data, chats, messages, and documents.Format:On Vercel:
Automatically created and added when you provision a Vercel Postgres database.Documentation:
https://vercel.com/docs/postgres
BLOB_READ_WRITE_TOKEN
Access token for Vercel Blob storage used to store file uploads and attachments.On Vercel:
Automatically created and added when you provision a Vercel Blob store.Documentation:
https://vercel.com/docs/vercel-blob
REDIS_URL
Redis connection string for rate limiting and caching.On Vercel:
Automatically created and added when you provision Vercel KV (Redis).Documentation:
https://vercel.com/docs/redis
AI_GATEWAY_API_KEY
API key for accessing Vercel AI Gateway.Required for: Non-Vercel deployments onlyNot required for: Vercel deployments (uses OIDC tokens automatically)How to create:
- Visit https://vercel.com/ai-gateway
- Create a new API key
- Add the key to your environment variables
On Vercel deployments, AI Gateway authentication is handled automatically using OIDC tokens. You only need to set
AI_GATEWAY_API_KEY for non-Vercel deployments.Setting up environment variables
Local development
For local development, create a.env.local file in your project root:
.env.local
Vercel deployment
For Vercel deployments, use the Vercel CLI to manage environment variables:
Alternatively, set environment variables in the Vercel Dashboard:
- Go to your project settings
- Navigate to “Environment Variables”
- Add each variable with appropriate environment scope (Production, Preview, Development)
Other platforms
For other deployment platforms:- Docker: Use environment variables in your
docker-compose.ymlor runtime configuration - AWS/GCP/Azure: Use platform-specific secret management (Parameter Store, Secret Manager, Key Vault)
- Self-hosted: Set environment variables in your process manager (PM2, systemd) or shell profile
Environment variable precedence
Next.js loads environment variables in the following order (highest precedence first):- Process environment variables
.env.local.env.productionor.env.development(depending onNODE_ENV).env
Verifying your configuration
To verify your environment variables are correctly set:Security best practices
- Never commit
.env.localor any file containing secrets - Use different values for development, preview, and production environments
- Rotate secrets regularly, especially
AUTH_SECRET - Use platform-specific secret management when available
- Limit access to environment variables to essential team members
- Audit access logs regularly
Troubleshooting
Missing AUTH_SECRET error
Missing AUTH_SECRET error
If you see authentication errors or session issues:
- Verify
AUTH_SECRETis set in your environment - Generate a new secret:
openssl rand -base64 32 - Add it to your
.env.localor deployment platform - Restart your application
Database connection errors
Database connection errors
If you see database connection errors:
- Verify
POSTGRES_URLis correctly formatted - Check database credentials and network access
- Ensure database is running and accessible
- Test connection with:
psql $POSTGRES_URL
AI Gateway authentication failed
AI Gateway authentication failed
If you see AI Gateway authentication errors:On Vercel: OIDC authentication should work automatically. Check:
- Your project is properly deployed on Vercel
- AI Gateway is enabled for your account
- Set
AI_GATEWAY_API_KEYin your environment - Verify the API key is valid at https://vercel.com/ai-gateway
Blob storage errors
Blob storage errors
If you see file upload or storage errors:
- Verify
BLOB_READ_WRITE_TOKENis set - Check token has read and write permissions
- Ensure Blob store is provisioned in your Vercel project
- Verify token hasn’t expired
Next steps
Database setup
Configure PostgreSQL and run migrations
Deploy to Vercel
Deploy your application to production