Skip to content

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

Quick Deploy

  1. Go to Railway Dashboard

    Visit railway.app and sign in with GitHub.

  2. Create New Project

    Click “New Project” → “Deploy from GitHub repo”

  3. Select Repository

    Choose your discord-forum-api repository.

  4. 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) # Database

Deploying the Bot

  1. Add New Service

    In your Railway project, click “New Service” → “GitHub Repo”

  2. Configure Service

    SettingValue
    Namediscord-bot
    Root Directorypackages/bot
    Build Commandcd ../.. && pnpm install && pnpm build
    Start Commandnode dist/index.js
  3. Add Environment Variables

    Click the service → “Variables” tab:

    DISCORD_TOKEN=your_bot_token
    DISCORD_CLIENT_ID=your_client_id
    DATABASE_TYPE=turso
    TURSO_DATABASE_URL=libsql://your-db.turso.io
    TURSO_AUTH_TOKEN=your_token
    NODE_ENV=production
  4. Deploy

    Railway will automatically deploy when you add variables.

Deploying the API

  1. Add Another Service

    Click “New Service” → “GitHub Repo” (same repo)

  2. Configure Service

    SettingValue
    Namediscord-api
    Root Directorypackages/api
    Build Commandcd ../.. && pnpm install && pnpm build
    Start Commandnode dist/index.js
  3. Add Environment Variables

    DATABASE_TYPE=turso
    TURSO_DATABASE_URL=libsql://your-db.turso.io
    TURSO_AUTH_TOKEN=your_token
    API_PORT=3000
    CORS_ORIGIN=https://yourdomain.com
    NODE_ENV=production
  4. 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:

  1. Add PostgreSQL

    In your project, click “New” → “Database” → “PostgreSQL”

  2. Copy Connection String

    Click PostgreSQL → “Variables” → Copy DATABASE_URL

  3. Update Service Variables

    DATABASE_TYPE=postgres
    DATABASE_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:

  1. Click project settings (gear icon)

  2. Go to “Shared Variables”

  3. Add common variables:

    DATABASE_TYPE=turso
    TURSO_DATABASE_URL=libsql://your-db.turso.io
    TURSO_AUTH_TOKEN=your_token
  4. Reference in services with ${{shared.VARIABLE_NAME}}

Custom Domain

  1. Click your API service
  2. Go to “Settings” → “Networking”
  3. Add custom domain (e.g., api.yourdomain.com)
  4. Configure DNS as shown
  5. SSL is automatic

Nixpacks Configuration

Railway uses Nixpacks for builds. Create nixpacks.toml in package roots for custom configuration:

packages/bot/nixpacks.toml
[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:

  1. Click service
  2. Go to “Deployments” tab
  3. 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

PlanPriceIncludes
Hobby$5/month$5 credit, basic resources
Pro$20/monthHigher limits, team features

Typical usage for Discord Forum API:

ResourceEstimated UsageCost
Bot service~$2-3/monthLow CPU/memory
API service~$2-5/monthDepends on traffic
PostgreSQL~$5-10/monthIf using Railway’s DB

Troubleshooting

Build Fails

  1. Check build logs for errors
  2. Verify Root Directory is correct
  3. Ensure dependencies install properly
  4. Check Node.js version compatibility

Bot Disconnects

  1. Check DISCORD_TOKEN is correct
  2. Verify bot has necessary intents
  3. Check logs for error messages
  4. Railway restarts failed services automatically

API Returns 500

  1. Check environment variables
  2. Verify database connection
  3. Review API logs
  4. Test database query locally

Out of Memory

  1. Upgrade to larger instance
  2. Add memory limits to start command
  3. Optimize database queries
  4. 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