Environment Variables
Manage configuration and secrets securely with environment variables
Environment variables let you configure your application without hardcoding values. Perfect for API keys, database URLs, and environment-specific settings.
Adding Environment Variables
Go to Service Settings
Navigate to your service and click "Environment Variables"
Add Variables
Enter key-value pairs (e.g., API_KEY=your-secret-key)
Save & Redeploy
Changes take effect on next deployment
Accessing Variables in Code
Node.js
// Access environment variablesconst port = process.env.PORT || 3000;const apiKey = process.env.API_KEY;const dbUrl = process.env.DATABASE_URL;// Use in your applicationconst app = express();app.get('/api/data', async (req, res) => {const response = await fetch('https://api.example.com/data', {headers: {'Authorization': `Bearer ${apiKey}`}});res.json(await response.json());});app.listen(port, () => {console.log(`Server running on port ${port}`);});
Python
import osfrom flask import Flaskapp = Flask(__name__)# Access environment variablesPORT = int(os.environ.get('PORT', 5000))API_KEY = os.environ.get('API_KEY')DATABASE_URL = os.environ.get('DATABASE_URL')@app.route('/api/data')def get_data():# Use environment variablesheaders = {'Authorization': f'Bearer {API_KEY}'}response = requests.get('https://api.example.com/data', headers=headers)return response.json()if __name__ == '__main__':app.run(host='0.0.0.0', port=PORT)
Go
package mainimport ("fmt""log""net/http""os")func main() {// Access environment variablesport := os.Getenv("PORT")if port == "" {port = "8080"}apiKey := os.Getenv("API_KEY")dbURL := os.Getenv("DATABASE_URL")http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, "API Key: %s", apiKey)})log.Printf("Server starting on port %s", port)log.Fatal(http.ListenAndServe(":"+port, nil))}
Common Environment Variables
| Variable | Description | Example |
|---|---|---|
| PORT | Application port (auto-set) | 3000 |
| NODE_ENV | Node.js environment | production |
| DATABASE_URL | Database connection string | postgresql://... |
| REDIS_URL | Redis connection string | redis://... |
| API_KEY | Third-party API key | sk_live_... |
| JWT_SECRET | JWT signing secret | your-secret-key |
System-Provided Variables
Replov automatically sets these variables:
PORT- The port your application should listen onReplov_SERVICE_ID- Your service's unique IDReplov_DEPLOYMENT_ID- Current deployment IDReplov_REGION- Deployment region
Build-Time vs Runtime Variables
Build-Time
Available during the build process. Embedded in your frontend bundle.
Runtime
Available when your application runs. Not embedded in frontend.
.env Files (Local Development)
For local development, use .env files:
# DatabaseDATABASE_URL=postgresql://localhost:5432/myapp# API KeysOPENAI_API_KEY=sk-...STRIPE_SECRET_KEY=sk_test_...# ApplicationNODE_ENV=developmentPORT=3000
.env to your .gitignore file. Never commit secrets to your repository..env.example Template
Create a .env.example file with placeholder values:
# DatabaseDATABASE_URL=postgresql://user:password@host:5432/database# API KeysOPENAI_API_KEY=your-openai-keySTRIPE_SECRET_KEY=your-stripe-key# ApplicationNODE_ENV=developmentPORT=3000
Secrets Management Best Practices
1. Use Strong Secrets
Generate cryptographically secure random strings:
# Generate a secure secretnode -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
2. Rotate Secrets Regularly
Change API keys and secrets periodically, especially after team member changes.
3. Use Different Secrets Per Environment
Never use production secrets in development or staging environments.
4. Limit Access
Only give team members access to secrets they need.
Validating Environment Variables
Validate required environment variables on application startup:
// Validate required environment variablesconst requiredEnvVars = ['DATABASE_URL','API_KEY','JWT_SECRET'];for (const envVar of requiredEnvVars) {if (!process.env[envVar]) {console.error(`Missing required environment variable: ${envVar}`);process.exit(1);}}// Export validated configmodule.exports = {database: {url: process.env.DATABASE_URL},api: {key: process.env.API_KEY},jwt: {secret: process.env.JWT_SECRET},port: parseInt(process.env.PORT || '3000', 10)};
Multiple Environments
Use different services for different environments:
| Environment | Service | Variables |
|---|---|---|
| Development | my-app-dev | Test API keys, dev database |
| Staging | my-app-staging | Staging API keys, staging database |
| Production | my-app-prod | Live API keys, production database |
Troubleshooting
Variable Not Available
Ensure you've redeployed after adding the variable. Check for typos in variable names (they're case-sensitive).
Build-Time Variable Not Working
Frontend variables must have the correct prefix (VITE_, NEXT_PUBLIC_, REACT_APP_) and require a rebuild.
Special Characters in Values
Wrap values with special characters in quotes. For connection strings, ensure proper URL encoding.