Configuration
Environment variables
8 min
What are Environment Variables?
Environment variables are key-value pairs that configure your application without hardcoding sensitive information in your source code.
When to Use Environment Variables
- API keys and secrets
- Database connection strings
- Third-party service credentials
- Feature flags
- Configuration that differs between environments
Adding Environment Variables
- Navigate to your project settings
- Click "Environment Variables"
- Click "Add Variable"
- Enter the key and value
- Select which environments should have this variable (production, preview, development)
- Save changes
Accessing Environment Variables in Code
Node.js
const apiKey = process.env.API_KEY;
const dbUrl = process.env.DATABASE_URL;
Next.js (Client-side)
// Must be prefixed with NEXT_PUBLIC_
const publicKey = process.env.NEXT_PUBLIC_API_KEY;
React (Vite)
// Must be prefixed with VITE_
const apiUrl = import.meta.env.VITE_API_URL;
Best Practices
- Never commit secrets: Add
.envto.gitignore - Use descriptive names:
DATABASE_URLnotDB - Document required variables: Create a
.env.examplefile - Use different values per environment: Development, staging, and production should have separate configurations
- Rotate secrets regularly: Update API keys and passwords periodically
Environment-Specific Variables
Set different values for each environment:
- Production: Live API keys, production database
- Preview: Test API keys, staging database
- Development: Local development values
Updating Variables
When you update environment variables, you need to redeploy for changes to take effect.
Was this article helpful?