# Railway Production Deployment

**Status**: ✅ **DEPLOYED AND OPERATIONAL**
**Deployment Date**: 2025-10-08
**Platform**: Railway
**Production URL**: https://slide-system-production-5813.up.railway.app

---

## Current Deployment

### Architecture

```
Client/Browser
    ↓
Railway Production (Docker Container)
    ↓
Motia API Server (:3000)
    ↓
API Step: demo-session-orchestration.step.ts
    ↓
    ├─→ Daily.co API (create video room)
    └─→ Supabase REST API (create session record)
```

### Configuration

**Railway Settings**:
- **Root Directory**: `apps/slide-system`
- **Build Method**: Dockerfile
- **Base Image**: `motiadev/motia:latest`
- **Port**: Auto-assigned by Railway (defaults to 3000)
- **Region**: us-west1
- **Auto-Deploy**: Enabled on `main` branch

**Environment Variables**:
```bash
DAILY_API_KEY=********  # Daily.co API key
SUPABASE_URL=https://dwdytxxqcvlrmyemonxz.supabase.co
SUPABASE_SERVICE_KEY=********  # Supabase service role key
NODE_ENV=production
```

---

## API Endpoints

### POST /demo-sessions

Creates a new demo session with Daily.co room and Supabase record.

**Production URL**:
```
https://slide-system-production-5813.up.railway.app/demo-sessions
```

**Request**:
```bash
curl -X POST https://slide-system-production-5813.up.railway.app/demo-sessions \
  -H "Content-Type: application/json" \
  -d '{
    "session_type": "live_demo",
    "environment": "prod",
    "source": "website",
    "utm_source": "homepage",
    "utm_medium": "cta",
    "utm_campaign": "fall_2025",
    "referrer_url": "https://buzztrail.ai"
  }'
```

**Response** (200 OK):
```json
{
  "session_id": "91e78256-c70d-4777-819d-08c546006b2e",
  "daily_room_url": "https://buzztrail.daily.co/demo-1759918390081-d77k2k0j",
  "daily_room_name": "demo-1759918390081-d77k2k0j",
  "status": "waiting_for_customer",
  "created_at": "2025-10-08T10:13:11.403222+00:00"
}
```

**Request Parameters**:

| Field | Type | Required | Options | Description |
|-------|------|----------|---------|-------------|
| `session_type` | string | ✅ Yes | `live_demo`, `test`, `replay`, `preview` | Type of demo session |
| `environment` | string | ✅ Yes | `dev`, `staging`, `prod` | Deployment environment |
| `source` | string | ❌ No | Any string | Source identifier (e.g., "website", "email") |
| `utm_source` | string | ❌ No | Any string | UTM source parameter |
| `utm_medium` | string | ❌ No | Any string | UTM medium parameter |
| `utm_campaign` | string | ❌ No | Any string | UTM campaign parameter |
| `referrer_url` | string | ❌ No | Valid URL | Referrer URL |

**Error Responses**:

400 Bad Request:
```json
{
  "error": "Missing required fields: session_type and environment"
}
```

500 Internal Server Error:
```json
{
  "error": "Daily.co API error (401): Unauthorized",
  "details": "Stack trace (dev only)"
}
```

---

## Deployment Process

### Automatic Deployment

Every push to `main` branch triggers automatic deployment:

```bash
# 1. Make changes
git add .
git commit -m "Your changes"
git push origin main

# 2. Railway automatically:
#    - Detects push
#    - Builds Docker image
#    - Runs tests (if configured)
#    - Deploys to production
#    - Performs health checks
#    - Routes traffic to new deployment
```

### Manual Deployment

Via Railway CLI (optional):

```bash
# Install Railway CLI
npm install -g @railway/cli

# Login
railway login

# Link to project
railway link

# Deploy manually
railway up
```

---

## Verification

### Health Check

```bash
# Check if server is responding
curl -I https://slide-system-production-5813.up.railway.app/demo-sessions

# Expected: 405 Method Not Allowed (GET not supported, only POST)
```

### Integration Test

```bash
# Full integration test
curl -X POST https://slide-system-production-5813.up.railway.app/demo-sessions \
  -H "Content-Type: application/json" \
  -d '{"session_type":"test","environment":"prod"}' | jq .

# Verify Daily.co room
# Copy daily_room_url from response and open in browser

# Verify Supabase record
# Check Supabase dashboard → conversation_sessions table
```

---

## Monitoring

### Railway Dashboard

**URL**: https://railway.app

**Available Metrics**:
- Real-time logs
- CPU usage
- Memory usage
- Network traffic
- Deployment history
- Build logs

### Daily.co Dashboard

**URL**: https://dashboard.daily.co

Monitor:
- Active rooms
- Room usage
- Recording status
- API usage

### Supabase Dashboard

**URL**: https://supabase.com/dashboard

Monitor:
- Session records in `conversation_sessions` table
- API usage
- Database performance

---

## Troubleshooting

### Application Not Responding

**Symptoms**:
- "Application failed to respond"
- 502 Bad Gateway
- Timeout errors

**Solutions**:
1. Check Railway logs for errors
2. Verify environment variables are set
3. Ensure PORT binding uses `--host 0.0.0.0`
4. Confirm Root Directory is `apps/slide-system`

### Daily.co Integration Errors

**Symptoms**:
- 401 Unauthorized
- 403 Forbidden
- "Failed to create room"

**Solutions**:
1. Verify `DAILY_API_KEY` in Railway Variables
2. Check Daily.co dashboard for account status
3. Verify API key permissions
4. Check billing/quota limits

### Supabase Integration Errors

**Symptoms**:
- "Failed to create session"
- Database connection errors
- 401/403 from Supabase

**Solutions**:
1. Verify `SUPABASE_URL` is correct
2. Check `SUPABASE_SERVICE_KEY` has write permissions
3. Ensure `conversation_sessions` table exists
4. Check Supabase project status

### Build Failures

**Symptoms**:
- "Dockerfile does not exist"
- Build timeout
- npm/pnpm errors

**Solutions**:
1. Verify Root Directory is set to `apps/slide-system`
2. Check Dockerfile syntax
3. Remove conflicting `railway.json` files
4. Check Railway build logs for specific errors

---

## Deployment History

### October 8, 2025 - Initial Production Deployment ✅

**Key Commits**:
- `c4b93d9` - Use Railway's PORT env var with fallback
- `7f73fe0` - Use motia dev with explicit host binding
- `b69016b` - Pre-install motia CLI in Docker image
- `8796fd2` - Remove railway.json
- `f316056` - Fix Railway deployment command

**Resolution Steps**:
1. Set Root Directory to `apps/slide-system`
2. Changed CMD to `npx motia dev --port ${PORT:-3000} --host 0.0.0.0`
3. Pre-installed motia globally in Dockerfile
4. Removed conflicting railway.json
5. Used shell form CMD for PORT interpolation

**Test Results**:
- ✅ Daily.co room created successfully
- ✅ Supabase session record persisted
- ✅ API responding with 200 status
- ✅ No errors in production logs

---

## Files

**Deployment Configuration**:
- `/apps/slide-system/Dockerfile` - Container configuration
- `/apps/slide-system/.env.motia` - Local environment variables (not deployed)
- `/apps/slide-system/.dockerignore` - Build exclusions

**Application Code**:
- `/apps/slide-system/steps/demo-session-orchestration.step.ts` - API endpoint
- `/apps/slide-system/workflows/demo-session-orchestration.ts` - Workflow (legacy)

**Documentation**:
- `/apps/slide-system/README.md` - Project overview
- `/apps/slide-system/DEPLOYMENT-STATUS.md` - Deployment status
- `/apps/slide-system/RAILWAY-QUICKSTART.md` - Quick start guide
- `/apps/slide-system/workflows/DEPLOYMENT.md` - This file

---

## Environment Configuration

### Production (Railway)

```bash
DAILY_API_KEY=<daily-api-key>
SUPABASE_URL=https://dwdytxxqcvlrmyemonxz.supabase.co
SUPABASE_SERVICE_KEY=<supabase-service-role-key>
NODE_ENV=production
```

### Local Development

```bash
# .env.motia
DAILY_API_KEY=<daily-api-key>
SUPABASE_URL=https://dwdytxxqcvlrmyemonxz.supabase.co
SUPABASE_SERVICE_KEY=<supabase-service-role-key>
```

Run locally:
```bash
cd apps/slide-system
pnpx motia dev --port 3000
```

---

## Previous Deployment Attempts

### ❌ Motia Cloud (Abandoned)

**Date**: October 8, 2025
**Status**: Abandoned due to rate limiting
**Issue**: Persistent 429 errors prevented deployment
**Resolution**: Switched to Railway self-hosting

See `MOTIA-CLOUD-RATE-LIMIT-ISSUE.md` for details.

### ✅ Supabase Edge Functions (Optional)

**Status**: Deployed but not primary
**Function**: `create-demo-session`
**URL**: `https://dwdytxxqcvlrmyemonxz.supabase.co/functions/v1/create-demo-session`

Can be used as a proxy layer but Railway endpoint can be called directly.

---

## Support Resources

- **Railway Docs**: https://docs.railway.app
- **Railway Discord**: https://discord.gg/railway
- **Motia Docs**: https://www.motia.dev/docs
- **Daily.co Docs**: https://docs.daily.co
- **Supabase Docs**: https://supabase.com/docs

---

## Next Steps

1. ✅ Production deployment - COMPLETE
2. ✅ Integration testing - VERIFIED
3. 🔄 Client application integration
4. 🔄 Error monitoring setup
5. 🔄 Performance optimization
6. 🔄 Load testing
