# Environment Variables Guide

**Last Updated**: 2025-10-15
**Version**: 1.0

## Overview

This document catalogs all environment variables across the BuzzTrail monorepo, organized by application/service.

## Table of Contents

1. [voice-demo-ui (Frontend)](#voice-demo-ui-frontend)
2. [slide-system (Backend API)](#slide-system-backend-api)
3. [buzztrail-voice-agent (Python)](#buzztrail-voice-agent-python)
4. [Environment Variable Security](#environment-variable-security)

---

## voice-demo-ui (Frontend)

**Application**: React frontend for LiveKit voice demos
**Location**: `voice-demo-ui/.env.local`
**Port**: 3006 (configured in vite.config.ts)

### Required Variables

```bash
# Backend API endpoint (slide-system)
VITE_API_BASE_URL=http://localhost:3000

# LiveKit Cloud WebSocket URL (for client connection)
VITE_LIVEKIT_URL=wss://buzztrail-xxx.livekit.cloud
```

### Important Notes

- **NO sensitive credentials** are stored in voice-demo-ui
- LiveKit API keys/secrets remain on the backend
- All authentication happens server-side
- Frontend only receives temporary LiveKit access tokens from API

### How to Configure

1. Copy `.env.example` to `.env.local`:
   ```bash
   cd voice-demo-ui
   cp .env.example .env.local
   ```

2. Get your LiveKit URL:
   - Visit https://cloud.livekit.io/projects/<your-project>/settings
   - Copy the WebSocket URL (format: `wss://buzztrail-xxx.livekit.cloud`)
   - Paste into `VITE_LIVEKIT_URL`

3. Ensure slide-system is running on port 3000

4. Start the app:
   ```bash
   pnpm nx dev voice-demo-ui
   # Access at: http://localhost:3006
   ```

---

## slide-system (Backend API)

**Application**: Motia API backend (Node.js)
**Location**: Railway production environment variables
**Port**: 3000 (local), Railway-assigned (production)

### Required Variables (Railway)

```bash
# LiveKit Configuration
LIVEKIT_URL=wss://buzztrail-xxx.livekit.cloud
LIVEKIT_API_KEY=<from-cloud.livekit.io>
LIVEKIT_API_SECRET=<from-cloud.livekit.io>

# Supabase Configuration
SUPABASE_URL=https://dwdytxxqcvlrmyemonxz.supabase.co
SUPABASE_SERVICE_KEY=<supabase-service-role-key>

# Optional: AI Services (if slide-system calls them directly)
# Note: These are primarily used by voice-agent, not slide-system
# DEEPGRAM_API_KEY=<deepgram-api-key>
# ELEVENLABS_API_KEY=<elevenlabs-api-key>
# OPENAI_API_KEY=<openai-api-key>
```

### How to Configure (Railway)

1. Go to Railway dashboard: https://railway.app/dashboard
2. Select the `slide-system` service
3. Navigate to **Variables** tab
4. Add/update environment variables:
   - `LIVEKIT_URL`: From LiveKit Cloud project settings
   - `LIVEKIT_API_KEY`: From LiveKit Cloud → Settings → API Keys
   - `LIVEKIT_API_SECRET`: From LiveKit Cloud → Settings → API Keys
   - `SUPABASE_URL`: From Supabase project settings
   - `SUPABASE_SERVICE_KEY`: From Supabase → Settings → API → service_role key

### What slide-system Uses These For

- **LIVEKIT_URL**: Generate LiveKit room connections
- **LIVEKIT_API_KEY/SECRET**: Create LiveKit access tokens for clients
- **SUPABASE_URL/KEY**: Database operations (session storage, bot tracking)

---

## buzztrail-voice-agent (Python)

**Application**: LiveKit Agents Python app
**Location**: Railway production environment variables OR Railway project environment variables
**Technology**: Python, LiveKit Agents SDK, uv package manager

### Required Variables

```bash
# LiveKit Configuration (REQUIRED)
LIVEKIT_URL=wss://buzztrail-xxx.livekit.cloud
LIVEKIT_API_KEY=<from-cloud.livekit.io>
LIVEKIT_API_SECRET=<from-cloud.livekit.io>

# AI Services (REQUIRED)
DEEPGRAM_API_KEY=<deepgram-api-key>        # Speech-to-text
OPENAI_API_KEY=<openai-api-key>            # GPT-4 for conversation
# Note: ElevenLabs may be optional depending on TTS implementation

# Database (REQUIRED for slide control)
SUPABASE_URL=https://dwdytxxqcvlrmyemonxz.supabase.co
SUPABASE_SERVICE_KEY=<supabase-service-role-key>

# Optional: Feature flags
# SLIDE_SYSTEM_URL=https://slide-system-production-5813.up.railway.app
```

### How to Configure (Railway)

**Option A: Service-level variables** (recommended for production)
1. Railway dashboard → buzztrail-voice-agent service → Variables tab
2. Add variables specific to this service

**Option B: Project-level variables** (for shared config)
1. Railway dashboard → Project settings → Shared variables
2. Add variables that multiple services use (LIVEKIT_*, SUPABASE_*)

### What voice-agent Uses These For

- **LIVEKIT_URL/KEY/SECRET**: Join LiveKit rooms as an AI participant
- **DEEPGRAM_API_KEY**: Convert customer speech to text (STT)
- **OPENAI_API_KEY**: GPT-4 for natural language understanding and responses
- **SUPABASE_URL/KEY**: Send slide control commands to screenshare bot

---

## Environment Variable Security

### Best Practices

1. **Never commit `.env.local` or `.env` files**
   - Already in `.gitignore`
   - Use `.env.example` as template only

2. **Rotate API keys regularly**
   - LiveKit: Regenerate keys quarterly
   - AI services: Follow provider recommendations
   - Supabase: Use service role key, not anon key

3. **Use separate keys for dev/staging/prod**
   - LiveKit: Create separate projects per environment
   - Supabase: Use different projects or databases

4. **Principle of least privilege**
   - Frontend: Only gets WebSocket URLs (no keys)
   - Backend: Gets service role keys
   - Voice agent: Gets AI service keys

### Where Credentials Are Stored

| Credential Type | Stored In | Why |
|-----------------|-----------|-----|
| LiveKit API keys | slide-system (Railway) | Generate access tokens |
| LiveKit WSS URL | voice-demo-ui (.env.local) | Client connection endpoint |
| Supabase service key | slide-system, voice-agent | Database admin access |
| AI service keys | voice-agent (Railway) | Speech, conversation, TTS |
| Customer tokens | Never stored | Generated on-demand, short-lived |

### Checking Current Configuration

```bash
# Check Railway environment variables (from CLI)
railway variables

# Check local .env.local files
cat voice-demo-ui/.env.local

# Verify voice-demo-ui can connect to API
curl http://localhost:3000/health  # Assuming health endpoint exists
```

---

## Migration Checklist

### Adding LiveKit Support to Existing System

- [x] Create Supabase migrations (Task #146)
  - File: `apps/slide-system/supabase/migrations/20251015_add_livekit_support.sql`
  - Status: Applied (user confirmed)

- [ ] Configure LiveKit environment variables
  - Railway: slide-system service
  - Railway: voice-agent service (if deployed)

- [ ] Configure voice-demo-ui
  - Copy `.env.example` to `.env.local`
  - Add LiveKit WSS URL

- [ ] Test end-to-end
  - Start slide-system: `pnpm nx serve slide-system`
  - Start voice-demo-ui: `pnpm nx dev voice-demo-ui`
  - Click "See a demo now" button
  - Verify session creation and LiveKit connection

---

## Troubleshooting

### Error: "Cannot connect to API"
**Solution**: Check `VITE_API_BASE_URL` in voice-demo-ui/.env.local
- Ensure slide-system is running on port 3000
- Verify URL format: `http://localhost:3000` (no trailing slash)

### Error: "LiveKit connection failed"
**Solution**: Check LiveKit configuration
- Verify `VITE_LIVEKIT_URL` in voice-demo-ui/.env.local
- Ensure LiveKit API keys are set in slide-system (Railway)
- Check LiveKit Cloud dashboard for room creation errors

### Error: "Invalid access token"
**Solution**: Check slide-system LiveKit credentials
- Verify `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET` in Railway
- Regenerate keys if necessary (LiveKit Cloud → Settings → API Keys)

### Error: "Supabase connection failed"
**Solution**: Check Supabase credentials
- Verify `SUPABASE_URL` and `SUPABASE_SERVICE_KEY`
- Test connection: `curl <SUPABASE_URL>/rest/v1/conversation_sessions?limit=1 -H "apikey: <KEY>"`

---

## Quick Reference

### Environment Variable Prefixes

- `VITE_*`: Exposed to frontend (React, Vite)
- `LIVEKIT_*`: LiveKit SDK configuration
- `SUPABASE_*`: Supabase database
- `DEEPGRAM_*`: Deepgram STT
- `OPENAI_*`: OpenAI GPT
- `ELEVENLABS_*`: ElevenLabs TTS (if used)

### Port Summary

| Application | Port | URL |
|-------------|------|-----|
| slide-system (local) | 3000 | http://localhost:3000 |
| voice-demo-ui | 3006 | http://localhost:3006 |
| demos | 8080 | http://localhost:8080 |
| chatbot | 4200 | http://localhost:4200 |
| backup-manager | 4200 | http://localhost:4200 ⚠️ |
| demo-experience-ui | 4500 | http://localhost:4500 |

### Related Documentation

- [Port Assignments](.claude/docs/port-assignments.md)
- [LiveKit Voice Avatar System PRD](.claude/prds/voice-slide-show-livekit.md)
- [Task #146: Extend slide-system API](.claude/epics/voice-slide-show-livekit/146.md)
- [Task #147: Build Voice Demo UI](.claude/epics/voice-slide-show-livekit/147.md)
