# Railway Environment Variables Setup

> **Critical:** This document explains how to configure Railway environment variables for the RateRight Growth Engine deployment.

Last Updated: 2026-01-30

---

## The Problem

**Symptom:** Login page shows error: "Auth not configured - VITE_SUPABASE_URL and VITE_SUPABASE_ANON_KEY missing on Railway"

**Root Cause:** Vite embeds environment variables into JavaScript bundles at BUILD time, not runtime. Railway needs these variables during the build phase.

---

## Required Environment Variables

Railway needs **TWO sets** of Supabase credentials:

### Backend Variables (Runtime)
Used by Node.js Express server at runtime:
- `SUPABASE_URL` - Your Supabase project URL
- `SUPABASE_ANON_KEY` - Your Supabase anonymous key
- `SUPABASE_SERVICE_ROLE_KEY` - Your Supabase service role key (admin access)

### Frontend Variables (Build Time)
Used by Vite during `npm run build` to embed into JavaScript bundles:
- `VITE_SUPABASE_URL` - Same as `SUPABASE_URL`
- `VITE_SUPABASE_ANON_KEY` - Same as `SUPABASE_ANON_KEY`
- `VITE_API_URL` - Your Railway app URL (e.g., `https://rateright-growth-production.up.railway.app`)

---

## How to Add Variables to Railway

### Option 1: Railway Dashboard (Recommended)

1. Go to https://railway.app/dashboard
2. Select your `rateright-growth` project
3. Click on the service
4. Go to **Variables** tab
5. Add each variable:
   - Click **+ New Variable**
   - Enter variable name (e.g., `VITE_SUPABASE_URL`)
   - Enter value
   - Click **Add**

### Option 2: Railway CLI

```bash
# Install Railway CLI if needed
npm i -g @railway/cli

# Login
railway login

# Link to project
railway link

# Add variables
railway variables set VITE_SUPABASE_URL=https://your-project.supabase.co
railway variables set VITE_SUPABASE_ANON_KEY=your-anon-key
railway variables set VITE_API_URL=https://rateright-growth-production.up.railway.app
```

---

## Complete Variable Checklist

Copy these values from your existing backend env vars:

```bash
# Backend (already configured)
✓ SUPABASE_URL
✓ SUPABASE_ANON_KEY
✓ SUPABASE_SERVICE_ROLE_KEY

# Frontend (ADD THESE)
□ VITE_SUPABASE_URL         (same value as SUPABASE_URL)
□ VITE_SUPABASE_ANON_KEY    (same value as SUPABASE_ANON_KEY)
□ VITE_API_URL              (your Railway app URL)

# Other required env vars
✓ TWILIO_ACCOUNT_SID
✓ TWILIO_AUTH_TOKEN
✓ TWILIO_PHONE_NUMBER
✓ TWILIO_API_KEY_SID
✓ TWILIO_API_KEY_SECRET
✓ TWILIO_TWIML_APP_SID
✓ OPENAI_API_KEY
✓ PERPLEXITY_API_KEY
✓ DEEPGRAM_API_KEY
✓ SLACK_WEBHOOK_URL
✓ CLAWDBOT_API_KEY
✓ INTERNAL_JOB_KEY
✓ NOTION_API_KEY
✓ WORK_TRACKER_DB
```

---

## Build Process on Railway

When you add/change variables and redeploy, Railway runs:

```bash
# 1. Nixpacks detects Node.js project
# 2. Runs build command from package.json
npm run build
  └─> cd admin && npm install && npm run build && cp -r dist/* ../public/

# 3. During 'vite build':
#    - Reads VITE_* variables from Railway environment
#    - Embeds them into JavaScript bundles
#    - Outputs to admin/dist/
#    - Copies to public/

# 4. Runs start command
npm start
  └─> node src/index.js
      └─> Serves static files from public/ (with embedded credentials)
```

---

## Verification Steps

After adding the variables and redeploying:

### 1. Check Railway Logs
```
Build logs should show:
✓ npm run build
✓ vite build
✓ Build completed
✓ Files copied to ../public/
```

### 2. Check Deployed App
1. Visit your Railway app URL
2. Open browser DevTools → Console
3. Look for: `[Supabase] Client initialized with URL: https://...`
4. Should NOT see: `Supabase credentials not configured`

### 3. Test Login
1. Go to login page
2. Enter phone number
3. Should successfully send OTP
4. No "Auth not configured" error

---

## Troubleshooting

### Still seeing "Auth not configured" after adding variables?

**Check:**
1. ✅ Variables are set in Railway dashboard
2. ✅ Spelling is exact: `VITE_SUPABASE_URL` (not `VITE_SUPABASE_URI`)
3. ✅ Railway redeployed after adding variables
4. ✅ Build completed successfully (check logs)

**Force rebuild:**
```bash
# Trigger redeploy in Railway dashboard
# OR via CLI:
railway up --detach
```

### Build succeeds but login still fails?

**Check the bundle:**
1. Visit: `https://your-app.railway.app/assets/index-*.js`
2. Search for: "Auth not configured"
3. If found → env vars weren't embedded, rebuild needed
4. If not found but login fails → check Supabase credentials are correct

---

## Why This Matters

**Vite Environment Variables Work Differently:**

| Traditional Node.js | Vite Frontend |
|---------------------|---------------|
| Reads `process.env` at runtime | Reads `import.meta.env` at BUILD time |
| Env vars from Railway at runtime | Env vars embedded into JS bundle during build |
| Change var → restart app | Change var → rebuild app |

**This is why:**
- Backend vars work immediately (runtime)
- Frontend vars need rebuild (build-time)
- Can't change frontend config without rebuild
- Frontend bundles are static files served by backend

---

## Related Documentation

- **LESSONS.md** (lines 46-49): Frontend Supabase Auth - Build-Time Env Vars
- **admin/src/lib/supabase.js** (lines 3-4): Where VITE_* vars are used
- **package.json** (line 9): Build script that runs on Railway

---

## Quick Fix Checklist

```bash
# 1. Add to Railway Variables
VITE_SUPABASE_URL         = <your-supabase-url>
VITE_SUPABASE_ANON_KEY    = <your-anon-key>
VITE_API_URL              = <your-railway-url>

# 2. Redeploy on Railway
# (Automatic if variables added via dashboard)

# 3. Verify in browser console
# Should see: "[Supabase] Client initialized with URL: ..."

# 4. Test login
# Should work without "Auth not configured" error
```

---

**Status:** Ready to deploy once variables are configured in Railway dashboard.
