Backend Docs API Deployment Guide

Complete guide for deploying backend-docs-api to Railway

Backend Docs API Deployment Guide

Complete guide for deploying the backend-docs-api to Railway with MongoDB integration.

Prerequisites

  1. Railway account with CLI installed
  2. MongoDB Atlas test-modules cluster
  3. Railway project created for backend-docs-api
  4. .env.local configured with tokens

Step 1: Configure Environment Variables

Add to .env.local:

# Railway Docs API Token
RAILWAY_DOCS_TOKEN=<your-railway-project-token>
RAILWAY_PROJECTID_DOCS=<your-railway-project-id>

# MongoDB Test Modules Cluster
MONGODB_CONNECTION_STRING_TEST_MODULES=mongodb+srv://username:[email protected]/

# Shared Authentication (MUST match other APIs)
JWT_SECRET=<YOUR_JWT_SECRET_FROM_ENV_LOCAL>
SYSTEM_TOKEN=<your-system-token>

# Docs API Port (local only)
DOCS_API_PORT=3002

Step 2: Create Railway Project

  1. Go to Railway Dashboard
  2. Create new project: "backend-docs-api"
  3. Create project token: Settings → Tokens → Create Token
  4. Copy token to .env.local as RAILWAY_DOCS_TOKEN
  5. Copy project ID to .env.local as RAILWAY_PROJECTID_DOCS

Step 3: Configure Railway Environment

In Railway dashboard, add these environment variables:

NODE_ENV=production
PORT=8080  # Railway sets this automatically
RAILWAY_ENVIRONMENT=true

# MongoDB (test-modules cluster)
MONGODB_CONNECTION_STRING_TEST_MODULES=mongodb+srv://...

# Authentication (MUST match other APIs)
JWT_SECRET=<same as backend-api>
SYSTEM_TOKEN=<same as other APIs>

# CORS
ALLOWED_ORIGINS=https://gograb.space,http://localhost:5010

# Network timeouts
NPM_CONFIG_FETCH_RETRIES=5
NPM_CONFIG_FETCH_TIMEOUT=300000
YARN_NETWORK_TIMEOUT=600000

Step 4: ⚠️ Verify Railway Dashboard Settings (CRITICAL!)

Before deploying, manually verify these settings in the Railway Dashboard:

Go to Railway Dashboard → backend-docs-api service → Settings

Build Settings

Setting Must Be
Builder Dockerfile
Dockerfile Path deployment/Dockerfile.railway.docs-api

Deploy Settings

Setting Must Be
Start Command node dist/apis/backend-docs-api/main.js
Health Check Path /docs-api/health

⚠️ WARNING: If the Dockerfile Path is wrong (e.g., pointing to backend-ai), you will deploy the WRONG API!

Step 5: Deploy

# Deploy to Railway
./deployment/deploy-docs-api.sh

Step 6: Verify Deployment

⚠️ Check Logs First (CRITICAL!)

In Railway Dashboard, check the logs for your backend-docs-api service:

# ✅ CORRECT - You should see:
[DOCS-API] 1  - 12/22/2025, 3:55:09 PM ...
📚 GoGrab Backend Docs API READY on port 8080

# ❌ WRONG - If you see:
[AI-API] 1  - 12/22/2025, 3:55:09 PM ...
🤖 GoGrab Backend AI API READY on port 8080  ← WRONG API!

If logs show [AI-API] instead of [DOCS-API], the Dockerfile Path is wrong! Go back to Step 4.

Health Check

curl https://backend-docs-api-production.up.railway.app/docs-api/health

Expected response:

{
  "status": "ok",
  "timestamp": "2025-12-21T...",
  "service": "backend-docs-api",
  "database": "docs-database"
}

Test Endpoints

# Get manifest
curl https://backend-docs-api-production.up.railway.app/docs-api/sync/manifest

# Get files
curl https://backend-docs-api-production.up.railway.app/docs-api/sync/files?page=1&limit=10

Step 7: Initial Bulk Sync

After deployment, sync all local docs to MongoDB:

# Option 1: Use the prod sync script (recommended)
yarn docs:sync:prod

# Option 2: Manual with environment variables
export DOCS_API_URL=https://backend-docs-api-production.up.railway.app/docs-api
export SYSTEM_TOKEN=<your-system-token>
yarn docs:sync

Step 8: Update Frontend

Update docs-angular production environment:

// apps/docs-angular/src/environments/environment.prod.ts
export const environment = {
  production: true,
  docsApiUrl: 'https://backend-docs-api-production.up.railway.app/docs-api',
  wsUrl: 'wss://backend-docs-api-production.up.railway.app',
  enableCloudSync: true,
  enableAnalytics: true,
};

✅ This file has already been updated with the production URL.

Architecture

flowchart LR
    Local["/docs folder"] -->|watch| Watcher["File Watcher"]
    Watcher -->|POST| DocsAPI["backend-docs-api<br/>Railway"]
    DocsAPI -->|write| MongoDB["MongoDB<br/>test-modules"]
    MongoDB -->|Change Streams| DocsAPI
    DocsAPI -->|WebSocket| Frontend["docs-angular<br/>gograb.space/docs"]

Monitoring

Railway Dashboard

Monitor your deployment:

Health Checks

Railway automatically monitors /docs-api/health endpoint:

MongoDB Monitoring

Check MongoDB Atlas dashboard for:

Troubleshooting

Deployment Fails

  1. Check Railway logs for build errors
  2. Verify all environment variables are set
  3. Ensure MongoDB connection string is correct
  4. Check Dockerfile syntax

API Not Responding

  1. Check Railway service status
  2. Verify health check endpoint
  3. Review application logs
  4. Check MongoDB connection

Sync Not Working

  1. Verify SYSTEM_TOKEN matches in all environments
  2. Check file watcher is running locally
  3. Review API logs for sync errors
  4. Test manual sync: yarn docs:sync

Authentication Issues

  1. Ensure JWT_SECRET is identical across all APIs
  2. Verify token is being sent in Authorization header
  3. Check token expiration (7 days default)
  4. Test with curl: curl -H "Authorization: Bearer <token>" ...

Performance

Expected Performance

Optimization Tips

  1. Use pagination for large file lists
  2. Enable MongoDB compression
  3. Use Change Streams for real-time updates (not polling)
  4. Cache manifest on frontend

Security

Authentication

MongoDB Security

Related APIs

All three APIs share the same JWT authentication system.

Next Steps

  1. Deploy to Railway
  2. Run initial bulk sync
  3. Update frontend environment
  4. Build and deploy docs-angular static site
  5. Test end-to-end flow

Support

For issues or questions:

  1. Check Railway logs
  2. Review MongoDB Atlas logs
  3. Test locally first
  4. Verify all environment variables