railway r2r step uy step
Deploy R2R on Railway - Step-by-Step Guide
Section titled “Deploy R2R on Railway - Step-by-Step Guide”This guide will help you deploy R2R on Railway with proper configuration.
Prerequisites
Section titled “Prerequisites”- Railway account (free tier works)
- OpenAI API key (for embeddings and LLM)
Step 1: Create PostgreSQL Database
Section titled “Step 1: Create PostgreSQL Database”- Go to railway.app and create a New Project
- Click ”+ New” → “Database” → “PostgreSQL”
- Wait for it to deploy (takes ~30 seconds)
- Click on the PostgreSQL service
- Go to “Variables” tab and note these values:
PGHOSTPGPORTPGDATABASEPGUSERPGPASSWORD
Install pgvector Extension
Section titled “Install pgvector Extension”- Still in PostgreSQL service, click “Connect”
- Copy the connection string
- Use a PostgreSQL client (like
psqlor TablePlus) to connect:
# Using psql (install via: brew install postgresql)psql "postgresql://postgres:PASSWORD@HOST:PORT/railway"
# Run this command in psql:CREATE EXTENSION IF NOT EXISTS vector;
# Verify it worked:\dx vector
# Exit:\qOR use Railway’s built-in query computer:
- In PostgreSQL service, go to “Query” tab
- Run:
CREATE EXTENSION IF NOT EXISTS vector;
Step 2: Deploy R2R Service
Section titled “Step 2: Deploy R2R Service”Option A: Using Docker Image (Recommended)
Section titled “Option A: Using Docker Image (Recommended)”- In your Railway project, click ”+ New” → “Empty Service”
- Name it
r2r - Click on the service → “Settings”
- Under “Source”, select “Docker Image”
- Enter:
ragtoriches/r2r:latest - Click “Deploy”
Option B: Using GitHub Repository
Section titled “Option B: Using GitHub Repository”- Fork https://github.com/SciPhi-AI/R2R
- In Railway, click ”+ New” → “GitHub Repo”
- Connect your fork
- Railway will auto-detect and deploy
Step 3: Configure Environment Variables
Section titled “Step 3: Configure Environment Variables”Click on your R2R service → “Variables” tab and add these:
Required Variables:
Section titled “Required Variables:”# PostgreSQL Connection (use values from Step 1)POSTGRES_HOST=${{Postgres.PGHOST}}POSTGRES_PORT=${{Postgres.PGPORT}}POSTGRES_USER=${{Postgres.PGUSER}}POSTGRES_PASSWORD=${{Postgres.PGPASSWORD}}POSTGRES_DBNAME=${{Postgres.PGDATABASE}}
# Database URL (alternative format)DATABASE_URL=postgresql://${{Postgres.PGUSER}}:${{Postgres.PGPASSWORD}}@${{Postgres.PGHOST}}:${{Postgres.PGPORT}}/${{Postgres.PGDATABASE}}
# R2R ConfigurationR2R_PROJECT_NAME=supenR2R_HOST=0.0.0.0R2R_PORT=7272
# OpenAI API Key (REQUIRED for embeddings)OPENAI_API_KEY=sk-your-openai-key-here
# Optional: Anthropic for better LLMANTHROPIC_API_KEY=your-anthropic-keyRailway-Specific Syntax:
Section titled “Railway-Specific Syntax:”Railway uses ${{ServiceName.VARIABLE}} to reference other services’ variables. This auto-wires your database connection!
Step 4: Expose R2R to Public Internet
Section titled “Step 4: Expose R2R to Public Internet”- In R2R service, go to “Settings”
- Scroll to “Networking”
- Click “Generate Domain”
- Railway will give you a URL like:
r2r-production.up.railway.app - Copy this URL - you’ll need it for Supen
Step 5: Verify Deployment
Section titled “Step 5: Verify Deployment”Check Logs:
Section titled “Check Logs:”- Click on R2R service → “Deployments”
- Click the latest deployment
- Check logs for errors
Good signs:
Starting R2R server...Uvicorn running on 0.0.0.0:7272Database connection successfulBad signs:
Connection refusedMissing environment variableFailed to connect to databaseTest Health Endpoint:
Section titled “Test Health Endpoint:”# Replace with your Railway URLcurl https://r2r-production.up.railway.app/v3/healthShould return:
{ "status": "ok", "version": "3.x.x"}Step 6: Connect Supen to Railway R2R
Section titled “Step 6: Connect Supen to Railway R2R”In Railway - Supen Service:
Section titled “In Railway - Supen Service:”Add these environment variables to your Supen service:
R2R_API_URL=https://r2r-production.up.railway.appR2R_API_KEY= # Leave empty unless you set up authLocally (for development):
Section titled “Locally (for development):”Update app/.env.local:
R2R_API_URL=https://r2r-production.up.railway.appR2R_API_KEY=Step 7: Test the Integration
Section titled “Step 7: Test the Integration”Test from your local machine:
Section titled “Test from your local machine:”# Start Supenpnpm dev
# Test health endpointcurl http://localhost:3333/api/knowledge/health
# Should return:# {# "available": true,# "provider": "R2R",# "baseUrl": "https://r2r-production.up.railway.app"# }Upload a test document:
Section titled “Upload a test document:”curl -X POST http://localhost:3333/api/knowledge/documents \ -F "useHiResMode=false"Troubleshooting
Section titled “Troubleshooting”R2R crashes immediately:
Section titled “R2R crashes immediately:”Check logs for:
Missing OPENAI_API_KEY→ Add it in VariablesCannot connect to database→ Verify PostgreSQL is runningvector extension not found→ Install pgvector (Step 1)
Solution:
# Make sure these are set:POSTGRES_HOST=${{Postgres.PGHOST}}OPENAI_API_KEY=sk-...R2R starts but crashes after 30s:
Section titled “R2R starts but crashes after 30s:”Railway’s health check might be failing.
- Go to R2R service → “Settings”
- Scroll to “Health Check”
- Set:
- Path:
/v3/health - Timeout:
300seconds (5 min) - Interval:
60seconds
- Path:
”Connection refused” from Supen:
Section titled “”Connection refused” from Supen:”Check:
- R2R service has a public domain generated
- R2R is actually running (check Deployments tab)
R2R_API_URLincludeshttps://
Documents upload but queries fail:
Section titled “Documents upload but queries fail:”OpenAI API key issue:
# Make sure this is set in R2R service:OPENAI_API_KEY=sk-...
# Restart R2R after adding it“Database does not exist” error:
Section titled ““Database does not exist” error:”PostgreSQL not initialized:
# Connect to PostgreSQL and run:CREATE DATABASE railway;CREATE EXTENSION IF NOT EXISTS vector;Cost Estimation
Section titled “Cost Estimation”Railway Hobby Plan ($5/month):
- PostgreSQL: ~$5/month
- R2R Service: ~$5-10/month (depends on usage)
- Total: ~$10-15/month
Free Tier:
- $5 credit/month (enough for testing)
- Limited to 500 hours/month total
Configuration Tips
Section titled “Configuration Tips”Use Lighter Models (Save OpenAI Costs):
Section titled “Use Lighter Models (Save OpenAI Costs):”# In R2R service variables, add:R2R_EMBEDDING_MODEL=text-embedding-3-small # Cheaper than ada-002R2R_COMPLETION_MODEL=gpt-4o-mini # Cheaper than gpt-4Enable Debug Logging:
Section titled “Enable Debug Logging:”R2R_LOG_LEVEL=DEBUGIncrease Memory (if crashes):
Section titled “Increase Memory (if crashes):”- R2R service → “Settings”
- Scroll to “Resources”
- Upgrade to higher memory plan if needed
Alternative: Local Development Only
Section titled “Alternative: Local Development Only”If Railway is too complex, use local R2R for now:
# In your supen directory:docker compose up r2r r2r-postgres -d
# Use in .env.local:R2R_API_URL=http://localhost:7272This works great for development, deploy to Railway later when ready for production.
Next Steps
Section titled “Next Steps”Once R2R is running:
- ✅ Test document upload via Supen API
- ✅ Test RAG queries
- ✅ Build the knowledge UI
- ✅ Integrate with Vibex agents
Need Help?
Section titled “Need Help?”Common issues and solutions:
- R2R docs: https://github.com/SciPhi-AI/R2R
- Railway docs: https://docs.railway.app
- Supen issues: Check our docs/knowledge-quickstart.md