Skip to main content

Environment Variables

Environment variables allow you to configure your application without changing code. Deployxa provides secure, encrypted environment variable management for all your projects.

Overview

Environment variables are used to:

  • Store API keys and secrets
  • Configure database connections
  • Set application settings
  • Manage feature flags
  • Configure third-party services

Features:

  • Encrypted storage
  • Multiple scopes (Production, Preview, Development)
  • Bulk import/export
  • Version history
  • Access control

Adding Environment Variables

From Dashboard

  1. Go to ProjectEnvironment
  2. Click "Add Variable"
  3. Enter variable details:
    • Name: Variable name (e.g., DATABASE_URL)
    • Value: Variable value
    • Scope: Production, Preview, Development, or All
    • Encrypted: Toggle encryption (recommended for secrets)
  4. Click "Save"

From CLI

deployxa env add DATABASE_URL postgresql://user:pass@host:5432/db --project my-app
deployxa env add API_KEY sk_test_123 --project my-app --encrypted

From API

POST /api/projects/{projectId}/env
{
"key": "DATABASE_URL",
"value": "postgresql://user:pass@host:5432/db",
"encrypted": true,
"environment": "production"
}

Variable Naming

Naming Rules

  • Use uppercase letters
  • Use underscores to separate words
  • Start with a letter
  • No spaces or special characters (except underscores)
  • Maximum 255 characters

Valid Examples

DATABASE_URL
API_KEY
SECRET_KEY
NODE_ENV
APP_NAME
STRIPE_SECRET_KEY

Invalid Examples

database-url # ❌ Use underscores
api key # ❌ No spaces
123_API_KEY # ❌ Cannot start with number
API-KEY # ❌ Use underscores, not hyphens

Variable Scopes

Production

Variables available in production deployments:

  • Live applications
  • Custom domains
  • Production environment
DATABASE_URL=postgresql://prod-host:5432/db
API_KEY=prod_api_key_123

Preview

Variables available in preview deployments:

  • Pull request deployments
  • Preview URLs
  • Testing environment
DATABASE_URL=postgresql://preview-host:5432/db
API_KEY=preview_api_key_123

Development

Variables available in development:

  • Local development
  • Development environment
DATABASE_URL=postgresql://localhost:5432/db
API_KEY=dev_api_key_123

All Environments

Variables available in all environments:

APP_NAME=MyApp
NODE_ENV=production

Encryption

Encrypted Variables

Encrypted variables are:

  • Encrypted at rest using AES-256
  • Never logged or displayed in plain text
  • Only accessible to your application
  • Masked in the dashboard

When to encrypt:

  • API keys
  • Secret keys
  • Database passwords
  • Private keys
  • OAuth secrets

Unencrypted Variables

Unencrypted variables are:

  • Stored in plain text
  • Visible in the dashboard
  • Logged in build logs

When not to encrypt:

  • Public configuration
  • Feature flags
  • Non-sensitive settings
  • Public URLs

Toggling Encryption

  1. Go to ProjectEnvironment
  2. Find the variable
  3. Click the lock icon
  4. Toggle encryption
  5. Click "Save"

Note: Changing encryption requires redeployment.

Using Environment Variables

Node.js / JavaScript

// Access environment variable
const apiUrl = process.env.API_URL;
const dbUrl = process.env.DATABASE_URL;

// With default value
const port = process.env.PORT || 3000;

// Check if variable exists
if (process.env.API_KEY) {
// Use API key
}

Python

import os

# Access environment variable
api_url = os.environ.get('API_URL')
db_url = os.getenv('DATABASE_URL')

# With default value
port = int(os.environ.get('PORT', 8000))

# Check if variable exists
if 'API_KEY' in os.environ:
# Use API key
pass

PHP

// Access environment variable
$apiUrl = getenv('API_URL');
$dbUrl = $_ENV['DATABASE_URL'];

// With default value
$port = getenv('PORT') ?: 8000;

// Check if variable exists
if (getenv('API_KEY')) {
// Use API key
}

Ruby

# Access environment variable
api_url = ENV['API_URL']
db_url = ENV['DATABASE_URL']

# With default value
port = ENV.fetch('PORT', 8000)

# Check if variable exists
if ENV['API_KEY']
# Use API key
end

Java

// Access environment variable
String apiUrl = System.getenv("API_URL");
String dbUrl = System.getenv("DATABASE_URL");

// With default value
String port = System.getenv().getOrDefault("PORT", "8080");

// Check if variable exists
if (System.getenv("API_KEY") != null) {
// Use API key
}

Go

import "os"

// Access environment variable
apiUrl := os.Getenv("API_URL")
dbUrl := os.Getenv("DATABASE_URL")

// With default value
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}

// Check if variable exists
if _, exists := os.LookupEnv("API_KEY"); exists {
// Use API key
}

Rust

use std::env;

// Access environment variable
let api_url = env::var("API_URL").unwrap_or_default();
let db_url = env::var("DATABASE_URL").unwrap_or_default();

// With default value
let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string());

// Check if variable exists
if env::var("API_KEY").is_ok() {
// Use API key
}

Common Environment Variables

Node.js

NODE_ENV=production
PORT=3000
DATABASE_URL=postgresql://user:pass@host:5432/db
REDIS_URL=redis://user:pass@host:6379
API_KEY=your-api-key
JWT_SECRET=your-jwt-secret

Python

PYTHON_ENV=production
DATABASE_URL=postgresql://user:pass@host:5432/db
SECRET_KEY=your-secret-key
DEBUG=False
ALLOWED_HOSTS=example.com

PHP / Laravel

APP_ENV=production
APP_KEY=base64:your-app-key
APP_DEBUG=false
DB_CONNECTION=mysql
DB_HOST=host
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=user
DB_PASSWORD=pass

Ruby / Rails

RAILS_ENV=production
SECRET_KEY_BASE=your-secret-key
DATABASE_URL=postgresql://user:pass@host:5432/db
RAILS_LOG_TO_STDOUT=true
RAILS_SERVE_STATIC_FILES=true

Java / Spring Boot

SPRING_PROFILES_ACTIVE=production
SERVER_PORT=8080
SPRING_DATASOURCE_URL=jdbc:postgresql://host:5432/db
SPRING_DATASOURCE_USERNAME=user
SPRING_DATASOURCE_PASSWORD=pass

Bulk Operations

Import Variables

Import from .env file:

  1. Go to ProjectEnvironment
  2. Click "Import"
  3. Select your .env file
  4. Review variables
  5. Click "Import"

Example .env file:

DATABASE_URL=postgresql://user:pass@host:5432/db
API_KEY=your-api-key
SECRET_KEY=your-secret-key
NODE_ENV=production

Export Variables

Export to .env file:

  1. Go to ProjectEnvironment
  2. Click "Export"
  3. Select format (env, json, yaml)
  4. Select scope
  5. Click "Export"

Delete Multiple Variables

  1. Go to ProjectEnvironment
  2. Select variables (checkbox)
  3. Click "Delete Selected"
  4. Confirm deletion

Variable History

Viewing History

  1. Go to ProjectEnvironment
  2. Click on a variable
  3. Click "History"
  4. View change history

History Includes

  • Previous values
  • Change date
  • Changed by
  • Change reason (if provided)

Restoring Previous Values

  1. Go to ProjectEnvironment
  2. Click on a variable
  3. Click "History"
  4. Find the version to restore
  5. Click "Restore"
  6. Confirm restoration

Access Control

Who Can View Variables

  • Owners: Full access to all variables
  • Admins: Full access to all variables
  • Developers: View and edit variables for assigned projects

Who Can Edit Variables

  • Owners: Can edit all variables
  • Admins: Can edit all variables
  • Developers: Can edit variables for assigned projects

Audit Logs

All variable changes are logged:

  • Who made the change
  • When the change was made
  • What was changed
  • Previous value (encrypted)

Security Best Practices

Do's

✅ Use encryption for sensitive data ✅ Rotate secrets regularly ✅ Use different values per environment ✅ Document required variables ✅ Use descriptive variable names ✅ Review access permissions regularly

Don'ts

❌ Never commit .env files to Git ❌ Never share secrets in plain text ❌ Never use the same secret across environments ❌ Never log sensitive variables ❌ Never hardcode secrets in code ❌ Never use weak or predictable secrets

Secret Rotation

Rotate secrets regularly:

  1. Generate new secret
  2. Add new variable with new value
  3. Deploy application
  4. Verify application works
  5. Remove old variable
  6. Deploy again

Environment Separation

Use different values per environment:

# Production
DATABASE_URL=postgresql://prod-host:5432/db
API_KEY=prod_key_123

# Preview
DATABASE_URL=postgresql://preview-host:5432/db
API_KEY=preview_key_123

# Development
DATABASE_URL=postgresql://localhost:5432/db
API_KEY=dev_key_123

Troubleshooting

Variable Not Available

Problem: Environment variable is undefined

Solution:

  1. Check variable name is correct
  2. Verify variable is set in correct scope
  3. Redeploy application
  4. Check application code
  5. Verify variable is not misspelled

Encrypted Variable Not Decrypting

Problem: Encrypted variable shows as encrypted string

Solution:

  1. Verify encryption is enabled
  2. Check application has access
  3. Redeploy application
  4. Check encryption key is valid
  5. Contact support if issue persists

Variable Not Updating

Problem: Variable changes not reflected

Solution:

  1. Redeploy application
  2. Clear application cache
  3. Check variable scope
  4. Verify variable name
  5. Check for caching in application

Import Failed

Problem: Cannot import .env file

Solution:

  1. Check file format is correct
  2. Verify file encoding (UTF-8)
  3. Check for syntax errors
  4. Verify file permissions
  5. Try manual import

Advanced Features

Variable References

Reference other variables:

DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:5432/${DB_NAME}

Variable Validation

Validate variables before deployment:

# Required variables
DATABASE_URL
API_KEY
SECRET_KEY

# Optional variables
DEBUG=false
LOG_LEVEL=info

Variable Groups

Organize variables into groups:

# Database
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydb
DB_USER=user
DB_PASSWORD=pass

# API
API_URL=https://api.example.com
API_KEY=your-api-key
API_SECRET=your-api-secret

Dynamic Variables

Generate variables dynamically:

# Timestamp
DEPLOY_TIME=2024-01-15T12:00:00Z

# Commit hash
COMMIT_HASH=abc123def456

# Build number
BUILD_NUMBER=42

Resources


Need Help? Contact support@deployxa.com or join our community Discord.