Environment Variables - Replov Docs
Skip to main content

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

1

Go to Service Settings

Navigate to your service and click "Environment Variables"

2

Add Variables

Enter key-value pairs (e.g., API_KEY=your-secret-key)

3

Save & Redeploy

Changes take effect on next deployment

Redeploy Required
Environment variable changes require a redeploy to take effect. Your running service won't see the new values until redeployed.

Accessing Variables in Code

Node.js

server.jsjavascript
// Access environment variables
const port = process.env.PORT || 3000;
const apiKey = process.env.API_KEY;
const dbUrl = process.env.DATABASE_URL;
// Use in your application
const 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

app.pypython
import os
from flask import Flask
app = Flask(__name__)
# Access environment variables
PORT = 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 variables
headers = {'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

main.gogo
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
// Access environment variables
port := 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

VariableDescriptionExample
PORTApplication port (auto-set)3000
NODE_ENVNode.js environmentproduction
DATABASE_URLDatabase connection stringpostgresql://...
REDIS_URLRedis connection stringredis://...
API_KEYThird-party API keysk_live_...
JWT_SECRETJWT signing secretyour-secret-key

System-Provided Variables

Replov automatically sets these variables:

  • PORT - The port your application should listen on
  • Replov_SERVICE_ID - Your service's unique ID
  • Replov_DEPLOYMENT_ID - Current deployment ID
  • Replov_REGION - Deployment region
System variables are read-only and cannot be overridden.

Build-Time vs Runtime Variables

Build-Time

Available during the build process. Embedded in your frontend bundle.

VITE_API_URL
NEXT_PUBLIC_API_URL
REACT_APP_API_URL

Runtime

Available when your application runs. Not embedded in frontend.

DATABASE_URL
API_KEY
JWT_SECRET
Security Warning
Build-time variables (VITE_, NEXT_PUBLIC_, REACT_APP_) are embedded in your frontend bundle and visible to users. Never use them for secrets or API keys.

.env Files (Local Development)

For local development, use .env files:

.envbash
# Database
DATABASE_URL=postgresql://localhost:5432/myapp
# API Keys
OPENAI_API_KEY=sk-...
STRIPE_SECRET_KEY=sk_test_...
# Application
NODE_ENV=development
PORT=3000
Never Commit .env Files
Add .env to your .gitignore file. Never commit secrets to your repository.

.env.example Template

Create a .env.example file with placeholder values:

.env.examplebash
# Database
DATABASE_URL=postgresql://user:password@host:5432/database
# API Keys
OPENAI_API_KEY=your-openai-key
STRIPE_SECRET_KEY=your-stripe-key
# Application
NODE_ENV=development
PORT=3000

Secrets Management Best Practices

1. Use Strong Secrets

Generate cryptographically secure random strings:

bash
# Generate a secure secret
node -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:

config.jsjavascript
// Validate required environment variables
const 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 config
module.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:

EnvironmentServiceVariables
Developmentmy-app-devTest API keys, dev database
Stagingmy-app-stagingStaging API keys, staging database
Productionmy-app-prodLive 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.