Overview
This guide covers deploying Tresa Contafy API to production environments, with specific instructions for Railway and general deployment workflows.Prerequisites
- PostgreSQL database configured with SSL
- All environment variables configured
- Domain name and SSL certificate (recommended)
- CI/CD pipeline setup (recommended)
Pre-Deployment Checklist
Deployment Methods
Railway (Recommended)
Create Project
- Go to railway.app
- Click “New Project”
- Select “Deploy from GitHub repo”
- Connect your repository
Add PostgreSQL Database
- Click “New” → “Database” → “Add PostgreSQL”
- Railway provisions the database automatically
- Note the
DATABASE_URLis available as${{Postgres.DATABASE_URL}}
Configure Build Settings
Railway auto-detects settings, but verify:
- Build Command:
pnpm install && pnpm build - Start Command:
pnpm start - Root Directory:
/(or path to API if monorepo)
Run Migrations
After first deployment, run migrations:Or run manually via Railway CLI:
- Go to your service in Railway
- Click “Deployments” → Latest deployment → “View Logs”
- In “Settings” → “Deploy”, add a deploy command:
Manual Deployment
For VPS or custom hosting:Deployment Script
For manual deployments, create adeploy.sh script:
deploy.sh
CI/CD Pipeline
GitHub Actions Example
Create.github/workflows/deploy.yml:
Post-Deployment Verification
Check Logs
Monitor application logs for errors:Railway:
- Go to your service → “Deployments” → “View Logs”
Production Configuration
Trust Proxy (Railway)
Railway uses a reverse proxy. The application automatically enables proxy trust:src/server.ts:33
- Correct client IP addresses in logs
- Proper rate limiting per client
- Accurate request headers
Rate Limiting
Production rate limits:- General API: 1000 requests / 15 minutes per IP
- Auth endpoints: 5 requests / 15 minutes per IP
src/server.ts:67
CORS Configuration
CORS is configured based onFRONTEND_URL or APP_URL:
src/server.ts:41
Monitoring & Logging
Health Check Endpoint
- Load balancer health checks
- Uptime monitoring (UptimeRobot, Pingdom)
- Container orchestration health probes
Logging
Production uses structured JSON logging with Pino:src/utils/logger.util.ts:7
fatal- Application crasherror- Error conditionswarn- Warning conditionsinfo- Informational messages (default)debug- Debug messagestrace- Very detailed tracing
Rollback Procedure
Scaling Considerations
Horizontal Scaling
For high traffic, run multiple instances:- Railway: Increase “Replicas” in Settings
- Load Balancer: Use Nginx or cloud load balancer
- Session Management: Use Redis for session storage
Database Connection Pool
Adjust based on instance count:total_connections = instances × pool.max
Ensure PostgreSQL max_connections > total connections
Troubleshooting
Application won't start
Application won't start
Check:
- Verify
DATABASE_URLis accessible - Ensure all required env vars are set
- Check build completed successfully
- Review logs for errors
502 Bad Gateway
502 Bad Gateway
Possible causes:
- Application crashed (check logs)
- Port mismatch (ensure
PORTenv var matches) - Health check failing
Database connection errors
Database connection errors
Solutions:
- Verify SSL is enabled for production DB
- Check database is running and accessible
- Verify connection string is correct
- Check firewall rules
Rate limiting blocking legitimate users
Rate limiting blocking legitimate users
Solutions:
- Increase
API_RATE_LIMIT_MAX - Ensure
trust proxyis enabled - Check if IP detection is working correctly
Next Steps
Security
Configure security features and best practices
Monitoring
Set up monitoring and alerting