Environment Variables
Environment variables provide a secure way to configure your applications with sensitive data, API keys, and environment-specific settings.
What are Environment Variables?
Section titled “What are Environment Variables?”Environment variables are key-value pairs that configure your application’s runtime behavior without hardcoding values in your source code.
Benefits
Section titled “Benefits”- Security: Keep sensitive data out of your codebase
- Flexibility: Different values for different environments
- Configuration: Easy application configuration management
- Portability: Same code works across environments
Managing Variables
Section titled “Managing Variables”Via Dashboard
Section titled “Via Dashboard”- Navigate to your application dashboard
- Go to Settings → Environment Variables
- Click “Add Variable”
- Enter:
- Key: Variable name (e.g.,
DATABASE_URL) - Value: Variable value (e.g.,
postgresql://...) - Environment: Which environment(s) to apply to
- Key: Variable name (e.g.,
Via API
Section titled “Via API”curl -X POST \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "key": "DATABASE_URL", "value": "postgresql://user:pass@host:5432/db", "environment": "production" }' \ https://api.easydeploy.com/v1/applications/APP_ID/envBulk Import
Section titled “Bulk Import”Import multiple variables from a .env file:
# Upload .env filecurl -X POST \ -H "Authorization: Bearer YOUR_API_KEY" \ -F "environment=production" \ https://api.easydeploy.com/v1/applications/APP_ID/env/importCommon Use Cases
Section titled “Common Use Cases”Database Configuration
Section titled “Database Configuration”# PostgreSQLDATABASE_URL=postgresql://user:password@host:5432/databaseDB_HOST=db.example.comDB_PORT=5432DB_NAME=myappDB_USER=myuserDB_PASSWORD=secretpassword
# MongoDBMONGODB_URI=mongodb://user:password@host:27017/database
# RedisREDIS_URL=redis://user:password@host:6379API Keys and Secrets
Section titled “API Keys and Secrets”# Third-party APIsSTRIPE_SECRET_KEY=sk_live_...SENDGRID_API_KEY=SG....GOOGLE_ANALYTICS_ID=UA-...FACEBOOK_APP_ID=123456789
# JWT and encryptionJWT_SECRET=your-super-secret-jwt-keyENCRYPTION_KEY=32-character-encryption-keySESSION_SECRET=session-secret-keyApplication Configuration
Section titled “Application Configuration”# Environment settingsNODE_ENV=productionDEBUG=falseLOG_LEVEL=infoPORT=3000
# Feature flagsFEATURE_NEW_UI=trueFEATURE_BETA_API=falseMAINTENANCE_MODE=false
# External servicesCDN_URL=https://cdn.example.comAPI_BASE_URL=https://api.example.comWEBHOOK_URL=https://hooks.example.comEnvironment-Specific Variables
Section titled “Environment-Specific Variables”Development Environment
Section titled “Development Environment”NODE_ENV=developmentDEBUG=trueLOG_LEVEL=debugDATABASE_URL=postgresql://localhost:5432/myapp_devAPI_BASE_URL=http://localhost:8000STRIPE_SECRET_KEY=sk_test_...Staging Environment
Section titled “Staging Environment”NODE_ENV=stagingDEBUG=falseLOG_LEVEL=infoDATABASE_URL=postgresql://staging-db:5432/myapp_stagingAPI_BASE_URL=https://api-staging.example.comSTRIPE_SECRET_KEY=sk_test_...Production Environment
Section titled “Production Environment”NODE_ENV=productionDEBUG=falseLOG_LEVEL=errorDATABASE_URL=postgresql://prod-db:5432/myapp_prodAPI_BASE_URL=https://api.example.comSTRIPE_SECRET_KEY=sk_live_...Security Best Practices
Section titled “Security Best Practices”Sensitive Data
Section titled “Sensitive Data”- Never commit environment variables to version control
- Use strong passwords and random secrets
- Rotate keys regularly for security
- Limit access to production variables
Variable Naming
Section titled “Variable Naming”- Use UPPERCASE with underscores:
DATABASE_URL - Be descriptive:
STRIPE_SECRET_KEYvsKEY1 - Group related variables:
DB_HOST,DB_PORT,DB_NAME - Use consistent prefixes:
AWS_ACCESS_KEY,AWS_SECRET_KEY
Access Control
Section titled “Access Control”- Separate environments: Different variables for dev/staging/prod
- Team permissions: Limit who can view/edit variables
- Audit logging: Track all variable changes
- Read-only access: Some team members can view, not edit
Using Variables in Code
Section titled “Using Variables in Code”Node.js
Section titled “Node.js”// Access environment variablesconst dbUrl = process.env.DATABASE_URL;const apiKey = process.env.STRIPE_SECRET_KEY;const port = process.env.PORT || 3000;
// Database connectionconst db = require('pg').Client({ connectionString: process.env.DATABASE_URL});
// API configurationconst stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);Python
Section titled “Python”import os
# Access environment variablesdb_url = os.environ.get('DATABASE_URL')api_key = os.environ.get('STRIPE_SECRET_KEY')port = int(os.environ.get('PORT', 5000))
# Database connectionimport psycopg2conn = psycopg2.connect(os.environ['DATABASE_URL'])
# API configurationimport stripestripe.api_key = os.environ['STRIPE_SECRET_KEY']<?php// Access environment variables$dbUrl = $_ENV['DATABASE_URL'];$apiKey = $_ENV['STRIPE_SECRET_KEY'];$port = $_ENV['PORT'] ?? 8000;
// Database connection$pdo = new PDO($_ENV['DATABASE_URL']);
// API configuration\Stripe\Stripe::setApiKey($_ENV['STRIPE_SECRET_KEY']);?>package main
import ( "os" "strconv")
func main() { // Access environment variables dbURL := os.Getenv("DATABASE_URL") apiKey := os.Getenv("STRIPE_SECRET_KEY")
port, err := strconv.Atoi(os.Getenv("PORT")) if err != nil { port = 8080 }}Variable Management
Section titled “Variable Management”Updating Variables
Section titled “Updating Variables”Update variables without redeploying:
curl -X PUT \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "value": "new-secret-value" }' \ https://api.easydeploy.com/v1/applications/APP_ID/env/VARIABLE_KEYVariable History
Section titled “Variable History”- Change tracking: View all variable changes
- Rollback capability: Restore previous values
- Audit logs: Who changed what and when
Secrets Management
Section titled “Secrets Management”- Encrypted storage: All variables encrypted at rest
- Secure transmission: HTTPS for all API calls
- Access logging: Track who accessed variables
- Automatic rotation: Schedule key rotation
Configuration Files
Section titled “Configuration Files”.env File Support
Section titled “.env File Support”Create a .env file for local development:
# .env (do not commit to git!)DATABASE_URL=postgresql://localhost:5432/myapp_devSTRIPE_SECRET_KEY=sk_test_...JWT_SECRET=your-local-secretDEBUG=trueEnvironment-specific Files
Section titled “Environment-specific Files”NODE_ENV=developmentAPI_URL=http://localhost:3000
# .env.stagingNODE_ENV=stagingAPI_URL=https://api-staging.example.com
# .env.productionNODE_ENV=productionAPI_URL=https://api.example.com.gitignore
Section titled “.gitignore”Always exclude environment files from git:
# Environment variables.env.env.local.env.development.env.staging.env.production.env.*.local
# Environment configurationconfig/secrets.ymlconfig/database.ymlTroubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”Variable not available in application
- Check variable name spelling and case
- Verify environment selection
- Restart application after adding variables
Database connection fails
- Verify
DATABASE_URLformat - Check database host accessibility
- Confirm credentials are correct
API keys not working
- Ensure using correct key for environment
- Check API key permissions and quotas
- Verify key format and encoding
Debugging
Section titled “Debugging”List all environment variables:
# Node.jsconsole.log(process.env);
# Pythonimport osprint(os.environ)
# PHPprint_r($_ENV);
# Goimport "os"for _, env := range os.Environ() { fmt.Println(env)}Need help? Check our troubleshooting guide or contact support.