28 - Environment Configuration

Overview

OpenAlgo uses environment variables for configuration, managed through a .env file with validation at startup. For cloud deployments (Railway/Render), the start.sh script can auto-generate .env from environment variables.

Configuration Files

.env                # Active configuration (not in git)
.sample.env         # Reference template with all variables

Environment Variables (65+ Variables)

Version Tracking

# Configuration version - compare with .sample.env when updating
ENV_CONFIG_VERSION = '1.0.6'

Core Security (Required)

# Application secret key (required, 32+ characters)
# Generate with: python -c "import secrets; print(secrets.token_hex(32))"
APP_KEY = 'your_32_character_secret_key_here'

# Security pepper for API key hashing, password hashing, token encryption
# Generate with: python -c "import secrets; print(secrets.token_hex(32))"
API_KEY_PEPPER = 'your_32_character_pepper_here'

Broker Configuration

Database Configuration

Flask Application

WebSocket Configuration

Connection Pooling

Ngrok Configuration

Logging Configuration

Python Strategy Logging

Rate Limiting

API Configuration

CORS Configuration

Content Security Policy (CSP)

CSRF Protection

Railway/Cloud Deployment

When deploying to Railway or Render, set these environment variables in the platform dashboard:

Required Variables

Variable
Description

HOST_SERVER

Your app URL (e.g., https://your-app.up.railway.app)

REDIRECT_URL

Broker OAuth callback URL

BROKER_API_KEY

Broker API key

BROKER_API_SECRET

Broker API secret

APP_KEY

Generated secret key

API_KEY_PEPPER

Generated pepper

Auto-Generated by start.sh

When HOST_SERVER is set and no .env exists, start.sh automatically generates .env with:

  • All security settings

  • CORS configured for your domain

  • CSP with secure WebSocket URLs

  • Railway's PORT environment variable support

Validation

Startup Validation

Validation Rules

Variable
Validation

APP_KEY

Must be 32+ characters

API_KEY_PEPPER

Must be 32+ characters

*_PORT

0-65535

*_RATE_LIMIT*

Format: "X per Y"

SESSION_EXPIRY_TIME

Format: HH:MM

WEBSOCKET_URL

Starts with ws:// or wss://

LOG_LEVEL

DEBUG/INFO/WARNING/ERROR/CRITICAL

Generating Secrets

Environment Comparison

Development

Production (Local)

Production (Railway)

Security Best Practices

File Permissions

Never Commit Secrets

Version Check

Compare ENV_CONFIG_VERSION in your .env with .sample.env after updates. If they differ, copy new variables from the sample.

Key Files Reference

File
Purpose

.env

Active configuration

.sample.env

Reference template

start.sh

Auto-generates .env for cloud

utils/env_check.py

Validation logic

utils/config.py

Config helpers

Last updated