Railway Deployment¶
Deploy SaaS LiteLLM to Railway for production use.
Overview¶
Railway deployment consists of 3 services:
- PostgreSQL Database - Managed database
- SaaS API - Main FastAPI application (port 8003)
- MkDocs Docs - Documentation site (port 8004)
- Admin Panel (optional) - Next.js dashboard (port 3000)
Prerequisites¶
- Railway account (signup)
- GitHub repository with SaasLiteLLM code
- OpenAI API key (or other LLM provider keys)
Step 1: Create Railway Project¶
- Go to Railway Dashboard
- Click "New Project"
- Select "Deploy from GitHub repo"
- Choose your SaasLiteLLM repository
- Railway will detect the project structure
Step 2: Add PostgreSQL Database¶
- In your Railway project, click "+ New"
- Select "Database" → "PostgreSQL"
- Railway automatically creates and configures the database
- Note the connection details (or use
${{Postgres.DATABASE_URL}})
Step 3: Configure SaaS API Service¶
Service Settings¶
- Name:
saas-api - Root Directory: (leave empty - uses repository root)
- Dockerfile Path:
Dockerfile
Environment Variables¶
Add these variables to the SaaS API service:
# Database (automatically provided by Railway)
DATABASE_URL=${{Postgres.DATABASE_URL}}
# LiteLLM Proxy (use your LiteLLM Cloud or self-hosted URL)
LITELLM_PROXY_BASE_URL=https://your-litellm-proxy.com
# Master Key (generate a secure key)
MASTER_KEY=sk-master-xxxxxxxxxxxxxxxxxxxxxxxx
# LLM Provider Keys
OPENAI_API_KEY=sk-your-openai-key
# Add other provider keys as needed:
# ANTHROPIC_API_KEY=sk-ant-xxxxx
# GOOGLE_API_KEY=xxxxx
# AZURE_API_KEY=xxxxx
Generate Master Key¶
Networking¶
Railway will automatically: - Assign a public URL (e.g., https://saas-api-production-xxxxx.up.railway.app) - Expose port 8003 - Enable HTTPS
Step 4: Run Database Migrations¶
After the SaaS API deploys:
- Go to the service Settings → Variables
- Add temporary variable for migration:
- Redeploy the service
- Check logs to verify migrations ran successfully
- Remove the
RUN_MIGRATIONSvariable
Or run migrations manually:
# Install Railway CLI
npm install -g @railway/cli
# Login
railway login
# Link to your project
railway link
# Run migrations
railway run psql $DATABASE_URL -f scripts/migrations/001_initial_schema.sql
# Repeat for all migrations...
Step 5: Configure MkDocs Docs Service¶
Service Settings¶
- Name:
docs - Root Directory:
docs-service - Dockerfile Path:
Dockerfile
Environment Variables¶
None needed - PORT is automatically provided by Railway.
Networking¶
Railway will assign a public URL for the docs (e.g., https://docs-xxxxx.up.railway.app)
Step 6: Configure Admin Panel (Optional)¶
Service Settings¶
- Name:
admin-panel - Root Directory:
admin-panel - Build Command:
npm run build - Start Command:
npm run start
Environment Variables¶
# API URL (use your SaaS API Railway URL)
NEXT_PUBLIC_API_URL=${{saas-api.RAILWAY_PUBLIC_URL}}
# Node environment
NODE_ENV=production
Railway will automatically detect Next.js and configure the build.
Step 7: Verify Deployment¶
Check Health Endpoint¶
Expected response:
Create Test Organization¶
curl -X POST https://your-saas-api-url.railway.app/api/organizations/create \
-H "Authorization: Bearer sk-master-xxxxx" \
-H "Content-Type: application/json" \
-d '{
"org_id": "test-org",
"org_name": "Test Organization"
}'
Access Documentation¶
Visit https://your-docs-url.railway.app to see the deployed documentation.
Environment Management¶
Production vs Staging¶
Create separate Railway projects for different environments:
Production: - Project name: saas-litellm-production - Branch: main - Domain: api.yourcompany.com (custom domain)
Staging: - Project name: saas-litellm-staging - Branch: develop - Domain: Railway-provided URL
Custom Domains¶
- Go to service Settings → Networking
- Click "Generate Domain" or "Custom Domain"
- Add your custom domain (e.g.,
api.yourcompany.com) - Update DNS records as instructed by Railway
Environment Variables¶
Access via Railway Dashboard: - Shared Variables: Available to all services - Service Variables: Specific to one service - Reference Variables: Use ${{service.VARIABLE_NAME}}
Scaling¶
Railway automatically scales based on usage:
- Vertical Scaling: Increase memory/CPU in service settings
- Horizontal Scaling: Available on Pro plan
- Database: Can upgrade PostgreSQL plan for more resources
Monitoring¶
View Logs¶
- Go to service in Railway Dashboard
- Click "Logs" tab
- Filter by severity, search, or time range
Metrics¶
Railway provides built-in metrics: - CPU usage - Memory usage - Network traffic - Request count
Alerts¶
Set up alerts in Railway Dashboard: - Service crashes - High resource usage - Deployment failures
Backup & Recovery¶
Database Backups¶
Railway provides automatic daily backups for PostgreSQL:
- Go to database service
- Click "Backups" tab
- View available backups
- Restore from backup if needed
Manual Backup¶
# Export database
railway run pg_dump $DATABASE_URL > backup.sql
# Restore database
railway run psql $DATABASE_URL < backup.sql
Troubleshooting¶
Deployment Failed¶
Check logs: 1. Go to service → Deployments 2. Click failed deployment 3. View build/deploy logs for errors
Common issues: - Missing environment variables - Database connection failed - Docker build errors
Database Connection Issues¶
Check: 1. DATABASE_URL is correctly set 2. PostgreSQL service is running 3. Migrations completed successfully
Test connection:
Out of Memory¶
Solution: 1. Go to service Settings 2. Increase memory allocation 3. Redeploy service
LiteLLM Proxy Unreachable¶
Check: 1. LITELLM_PROXY_BASE_URL is correct 2. LiteLLM proxy is running and accessible 3. Network connectivity from Railway
Cost Optimization¶
Railway pricing is usage-based:
Tips to reduce costs: - Use resource limits - Scale down staging environments when not in use - Optimize Docker image size - Use shared PostgreSQL for development - Monitor usage in Railway Dashboard
Typical monthly costs: - Hobby plan: $5/month + usage - Pro plan: \(20/month + usage - Database: ~\)5-20/month depending on size
Next Steps¶
- Environment Variables - Complete configuration reference
- Docker Setup - Customize Docker builds
- Monitoring - Track system health