Railway Deployment
Railway is the easiest way to deploy both the bot and API together.
Why Railway?
- Supports long-running processes (perfect for the Discord bot)
- $5 free credit monthly (no credit card required to start)
- One-click database hosting
- Automatic deploys from GitHub
- Simple environment variable management
Prerequisites
- Railway account (free)
- GitHub repository with your code
- Discord bot token (from Discord Setup)
Quick Deploy
-
Go to Railway Dashboard
Visit railway.app and sign in with GitHub.
-
Create New Project
Click “New Project” → “Deploy from GitHub repo”
-
Select Repository
Choose your
discord-forum-apirepository. -
Railway auto-detects the project
It will detect the monorepo structure.
Project Structure
You’ll create two services on Railway:
Railway Project├── discord-bot (Service) # packages/bot├── discord-api (Service) # packages/api└── PostgreSQL (Optional) # DatabaseDeploying the Bot
-
Add New Service
In your Railway project, click “New Service” → “GitHub Repo”
-
Configure Service
Setting Value Name discord-botRoot Directory packages/botBuild Command cd ../.. && pnpm install && pnpm buildStart Command node dist/index.js -
Add Environment Variables
Click the service → “Variables” tab:
DISCORD_TOKEN=your_bot_tokenDISCORD_CLIENT_ID=your_client_idDATABASE_TYPE=tursoTURSO_DATABASE_URL=libsql://your-db.turso.ioTURSO_AUTH_TOKEN=your_tokenNODE_ENV=production -
Deploy
Railway will automatically deploy when you add variables.
Deploying the API
-
Add Another Service
Click “New Service” → “GitHub Repo” (same repo)
-
Configure Service
Setting Value Name discord-apiRoot Directory packages/apiBuild Command cd ../.. && pnpm install && pnpm buildStart Command node dist/index.js -
Add Environment Variables
DATABASE_TYPE=tursoTURSO_DATABASE_URL=libsql://your-db.turso.ioTURSO_AUTH_TOKEN=your_tokenAPI_PORT=3000CORS_ORIGIN=https://yourdomain.comNODE_ENV=production -
Generate Domain
Click service → “Settings” → “Generate Domain”
You’ll get a URL like
discord-api-production.up.railway.app
Using Railway’s PostgreSQL
Instead of Turso, you can use Railway’s PostgreSQL:
-
Add PostgreSQL
In your project, click “New” → “Database” → “PostgreSQL”
-
Copy Connection String
Click PostgreSQL → “Variables” → Copy
DATABASE_URL -
Update Service Variables
DATABASE_TYPE=postgresDATABASE_URL=${{Postgres.DATABASE_URL}}Using
${{Postgres.DATABASE_URL}}references the variable from your Postgres service.
Shared Variables
For variables used by both services, use Railway’s shared variables:
-
Click project settings (gear icon)
-
Go to “Shared Variables”
-
Add common variables:
DATABASE_TYPE=tursoTURSO_DATABASE_URL=libsql://your-db.turso.ioTURSO_AUTH_TOKEN=your_token -
Reference in services with
${{shared.VARIABLE_NAME}}
Custom Domain
- Click your API service
- Go to “Settings” → “Networking”
- Add custom domain (e.g.,
api.yourdomain.com) - Configure DNS as shown
- SSL is automatic
Nixpacks Configuration
Railway uses Nixpacks for builds. Create nixpacks.toml in package roots for custom configuration:
[phases.setup]nixPkgs = ['nodejs_20', 'pnpm']
[phases.install]cmds = ['cd ../.. && pnpm install']
[phases.build]cmds = ['cd ../.. && pnpm build']
[start]cmd = 'node dist/index.js'Monitoring
Logs
View logs in real-time:
- Click service
- Go to “Deployments” tab
- Click a deployment to see logs
Metrics
Railway shows:
- CPU usage
- Memory usage
- Network traffic
- Request counts
Alerts
Set up alerts for:
- Service crashes
- High resource usage
- Failed deployments
railway.json (Optional)
For more control, create railway.json:
{ "$schema": "https://railway.app/railway.schema.json", "build": { "builder": "NIXPACKS", "buildCommand": "cd ../.. && pnpm install && pnpm build" }, "deploy": { "startCommand": "node dist/index.js", "restartPolicyType": "ON_FAILURE", "restartPolicyMaxRetries": 10 }}Pricing
| Plan | Price | Includes |
|---|---|---|
| Hobby | $5/month | $5 credit, basic resources |
| Pro | $20/month | Higher limits, team features |
Typical usage for Discord Forum API:
| Resource | Estimated Usage | Cost |
|---|---|---|
| Bot service | ~$2-3/month | Low CPU/memory |
| API service | ~$2-5/month | Depends on traffic |
| PostgreSQL | ~$5-10/month | If using Railway’s DB |
Troubleshooting
Build Fails
- Check build logs for errors
- Verify Root Directory is correct
- Ensure dependencies install properly
- Check Node.js version compatibility
Bot Disconnects
- Check
DISCORD_TOKENis correct - Verify bot has necessary intents
- Check logs for error messages
- Railway restarts failed services automatically
API Returns 500
- Check environment variables
- Verify database connection
- Review API logs
- Test database query locally
Out of Memory
- Upgrade to larger instance
- Add memory limits to start command
- Optimize database queries
- Add pagination to large responses
Example Complete Setup
Project structure:
Railway Project: discord-forum-api├── Service: discord-bot│ ├── Root: packages/bot│ ├── Build: cd ../.. && pnpm install && pnpm build│ ├── Start: node dist/index.js│ └── Variables:│ ├── DISCORD_TOKEN=***│ ├── DISCORD_CLIENT_ID=***│ └── ${{shared variables}}│├── Service: discord-api│ ├── Root: packages/api│ ├── Build: cd ../.. && pnpm install && pnpm build│ ├── Start: node dist/index.js│ ├── Domain: api.yourdomain.com│ └── Variables:│ ├── API_PORT=3000│ ├── CORS_ORIGIN=https://yourdomain.com│ └── ${{shared variables}}│└── Shared Variables: ├── DATABASE_TYPE=turso ├── TURSO_DATABASE_URL=libsql://... ├── TURSO_AUTH_TOKEN=*** └── NODE_ENV=production