---
title: "Environment Variables"
description: "Configure TraceRTM using environment variables"
keywords: [environment variables, configuration, settings, env]
---

# Environment Variables

Configure TraceRTM using environment variables for different environments.

## Configuration File

### .env File

Create a `.env` file in the project root:

```bash
# Copy example
cp .env.example .env

# Edit
nano .env
```

## Required Variables

### Database

```bash
# PostgreSQL connection
DATABASE_URL=postgresql://user:password@localhost:5432/tracertm_dev
```

### API Configuration

```bash
# API base URL
API_URL=http://localhost:8000

# Secret key for encryption
SECRET_KEY=your-secret-key-here

# JWT secret
JWT_SECRET=your-jwt-secret-here
```

## Optional Variables

### Authentication

```bash
# OAuth settings
OAUTH_CLIENT_ID=your-client-id
OAUTH_CLIENT_SECRET=your-client-secret
OAUTH_REDIRECT_URI=http://localhost:3000/auth/callback
```

### Email

```bash
# SMTP configuration
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USER=user@example.com
SMTP_PASSWORD=password
SMTP_FROM=noreply@example.com
```

### Storage

```bash
# Local storage
STORAGE_BACKEND=local
STORAGE_PATH=/var/tracertm/storage

# S3 storage
STORAGE_BACKEND=s3
AWS_ACCESS_KEY_ID=your-key
AWS_SECRET_ACCESS_KEY=your-secret
AWS_S3_BUCKET=tracertm-storage
AWS_REGION=us-east-1
```

### Redis (Caching)

```bash
# Redis connection
REDIS_URL=redis://localhost:6379/0
```

### Logging

```bash
# Log level
LOG_LEVEL=INFO

# Log format
LOG_FORMAT=json
```

## Environment-Specific Files

### Development (.env.development)

```bash
DATABASE_URL=postgresql://localhost:5432/tracertm_dev
API_URL=http://localhost:8000
LOG_LEVEL=DEBUG
```

### Production (.env.production)

```bash
DATABASE_URL=postgresql://prod-db:5432/tracertm_prod
API_URL=https://api.example.com
LOG_LEVEL=WARNING
SECRET_KEY=production-secret-key
```

## Loading Environment Variables

### Python

```python
import os
from dotenv import load_dotenv

# Load .env file
load_dotenv()

# Access variables
database_url = os.getenv("DATABASE_URL")
```

### Shell Scripts

```bash
# Source .env file
source .env

# Or export variables
export DATABASE_URL=postgresql://...
```

## Security Best Practices

### Never Commit Secrets

```bash
# Add to .gitignore
echo ".env" >> .gitignore
echo ".env.local" >> .gitignore
echo ".env.*.local" >> .gitignore
```

### Use Secret Management

- **AWS**: Secrets Manager, Parameter Store
- **Azure**: Key Vault
- **GCP**: Secret Manager
- **Kubernetes**: Secrets

### Rotate Secrets Regularly

```bash
# Generate new secret
python -c "import secrets; print(secrets.token_urlsafe(32))"
```

## Validation

### Check Required Variables

```bash
# Python script
python scripts/check_env.py
```

### Example Check Script

```python
import os
from dotenv import load_dotenv

load_dotenv()

required = ["DATABASE_URL", "SECRET_KEY", "JWT_SECRET"]
missing = [var for var in required if not os.getenv(var)]

if missing:
    print(f"Missing required variables: {', '.join(missing)}")
    exit(1)
```

## Related Documentation

- [Prerequisites](./01-prerequisites) - Required software
- [Database Setup](./04-database-setup) - Database configuration
- [Docker Setup](./03-docker-setup) - Container configuration
