Configuration Guide
Complete reference for configuring B2ALABS AI Gateway using environment variables and configuration files. All settings documented from the actual codebase.
Configuration Overview
Two Configuration Methods
B2ALABS supports both environment variables and YAML configuration files
1. Environment Variables (Recommended)
Set environment variables in your .env file or container environment:
# Create .env file cp .env.example .env # Edit with your values DB_HOST=postgres DB_PORT=5432 DB_USER=b2alabs DB_PASSWORD=your_secure_password_here JWT_SECRET=your_32_character_secret_here
2. YAML Configuration File
Edit packages/gateway-core/configs/config.yaml:
server: port: 8080 environment: production host: 0.0.0.0 database: host: postgres port: 5432 user: b2alabs # password: use DB_PASSWORD env var database: b2alabs ssl_mode: disable
Important: Configuration Precedence
Values in config.yaml OVERRIDE environment variables. For production, use environment variables only and keep sensitive values out of config.yaml.
Database Configuration
PostgreSQL Settings
Configure PostgreSQL database connection (all settings from internal/config/config.go)
DB_HOSTRequiredlocalhostPostgreSQL database host address
Example:
DB_HOST=postgresDB_PORTRequired5432PostgreSQL database port
Example:
DB_PORT=5432DB_USERRequiredb2alabsPostgreSQL username
Example:
DB_USER=b2alabsDB_PASSWORDRequiredb2alabs_dev_passwordPostgreSQL password - MUST be changed in production
Example:
DB_PASSWORD=secure_random_password_32_charsDB_NAMERequiredb2alabs_prodPostgreSQL database name
Example:
DB_NAME=b2alabsDB_SSL_MODEdisablePostgreSQL SSL mode (disable, require, verify-ca, verify-full)
Example:
DB_SSL_MODE=requireComplete Connection String
postgresql://b2alabs:your_password@postgres:5432/b2alabs?sslmode=disable
Authentication & Security
JWT and Session Security
Configure authentication tokens and security settings
JWT_SECRETRequireddev-only-change-me-in-productionJWT signing secret - MUST be at least 32 characters in production
Example:
JWT_SECRET=FVrcDQITrfeehf3gTu1BlSCGlrj9Bys8JWT_EXPIRATION_MINUTES60JWT token expiration time in minutes
Example:
JWT_EXPIRATION_MINUTES=60REFRESH_TOKEN_DAYS30Refresh token expiration time in days
Example:
REFRESH_TOKEN_DAYS=30NEXTAUTH_SECRETRequired-NextAuth.js secret for session encryption
Example:
NEXTAUTH_SECRET=PNMpGSmYu9Fd7BcNTgrTxvPVFuaGId6dSecurity Validation (from config.go:327-356)
- JWT_SECRET MUST be at least 32 characters in production
- Cannot use default development values in staging/production
- CircleStackIcon password MUST be set in production
Generate Secure Secrets
# Linux/macOS
openssl rand -base64 32
# Docker (any OS)
docker run --rm alpine sh -c "head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32"
# Windows PowerShell
-join ((48..57) + (65..90) + (97..122) | Get-Random -Count 32 | % {[char]$_})Gateway Features
Gateway Core Settings
Configure server, rate limiting, and caching
GATEWAY_PORTDefault: 8080HTTP server port for the gateway
Example:
GATEWAY_PORT=8080GATEWAY_ENVDefault: developmentEnvironment mode (development, staging, production)
Example:
GATEWAY_ENV=productionGATEWAY_HOSTDefault: 0.0.0.0HTTP server bind address
Example:
GATEWAY_HOST=0.0.0.0RATE_LIMIT_ENABLEDDefault: falseEnable rate limiting globally
Example:
RATE_LIMIT_ENABLED=trueRATE_LIMIT_RPMDefault: 100Rate limit requests per minute
Example:
RATE_LIMIT_RPM=100CACHE_TYPEDefault: memoryCache backend type (memory, redis, layered)
Example:
CACHE_TYPE=redisRedis Configuration
Configure Redis for caching and rate limiting
REDIS_HOSTDefault: localhostRedis server host address
Example:
REDIS_HOST=redisREDIS_PORTDefault: 6379Redis server port
Example:
REDIS_PORT=6379REDIS_PASSWORDDefault: ""Redis authentication password (optional)
Example:
REDIS_PASSWORD=redis_passwordREDIS_DBDefault: 0Redis database number (0-15)
Example:
REDIS_DB=0AI Provider API Keys
Configure API keys for OpenAI, Anthropic, Google, and Azure
OPENAI_API_KEYOpenAI API key for GPT models
Example:
OPENAI_API_KEY=sk-proj-...ANTHROPIC_API_KEYAnthropic API key for Claude models
Example:
ANTHROPIC_API_KEY=sk-ant-...GOOGLE_API_KEYGoogle API key for Gemini models
Example:
GOOGLE_API_KEY=AIza...AZURE_OPENAI_API_KEYAzure OpenAI API key
Example:
AZURE_OPENAI_API_KEY=azure-key...AZURE_OPENAI_ENDPOINTAzure OpenAI endpoint URL
Example:
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.comObservability
Monitoring and Tracing
Configure OpenTelemetry, Prometheus, and logging
OTEL_SERVICE_NAMEDefault: b2alabs-gatewayOpenTelemetry service name for tracing
Example:
OTEL_SERVICE_NAME=b2alabs-gatewayOTEL_EXPORTER_OTLP_ENDPOINTDefault: http://localhost:4318OpenTelemetry collector endpoint
Example:
OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318PROMETHEUS_PORTDefault: 9090Prometheus metrics export port
Example:
PROMETHEUS_PORT=9090LOG_LEVELDefault: INFOLogging level (DEBUG, INFO, WARN, ERROR)
Example:
LOG_LEVEL=INFOCerbos Authorization (Optional)
Fine-Grained Access Control
Configure Cerbos for policy-based authorization
CERBOS_ENABLEDDefault: falseEnable Cerbos authorization
Example:
CERBOS_ENABLED=trueCERBOS_ADDRDefault: http://cerbos:3592Cerbos HTTP API address
Example:
CERBOS_ADDR=http://cerbos:3592CERBOS_GRPC_ADDRDefault: cerbos:3593Cerbos gRPC API address
Example:
CERBOS_GRPC_ADDR=cerbos:3593Production Setup
Production Best Practices
Essential security and performance recommendations
Security Checklist
- Set GATEWAY_ENV=production to enable security validations
- Generate strong 32+ character secrets for JWT_SECRET and NEXTAUTH_SECRET
- Use strong database passwords (minimum 32 characters)
- Enable DB_SSL_MODE=require for database connections
- Set LOG_LEVEL=INFO or WARN (not DEBUG) in production
- Enable RATE_LIMIT_ENABLED=true to prevent abuse
- Use Redis for caching (CACHE_TYPE=redis) for better performance
Environment-Specific .env Files
# Structure .env.development # Local development .env.staging # Staging environment .env.production # Production (NEVER commit!) # Docker Compose usage docker-compose --env-file .env.production up -d
Common Configuration Mistakes
- Using default passwords: Always generate new secure passwords
- Hardcoding in config.yaml: Config file values override environment variables
- Committing .env files: Always add .env to .gitignore
- Short JWT secrets: Minimum 32 characters required in production
Next Steps
Was this page helpful?
