This guide will walk you through deploying your Therapeutic AI Assistant to production.
- Node.js 18+ installed
- npm or yarn package manager
- Docker Desktop (for TTS service)
- Firebase account
- Stripe account
- Upstash Redis account (for rate limiting)
- Domain name purchased
-
Run the automated setup:
chmod +x scripts/setup-production.sh ./scripts/setup-production.sh
-
Validate your setup:
chmod +x scripts/deploy-check.sh ./scripts/deploy-check.sh
-
If all checks pass, proceed to deployment section below.
- Chat API: 20 requests/minute per user
- Voice API: 10 requests/minute per user
- Auth endpoints: 5 requests/5 minutes per IP
- Payment API: 3 requests/minute per user
- Content Security Policy (CSP)
- X-Frame-Options: DENY
- X-Content-Type-Options: nosniff
- Strict-Transport-Security
- X-XSS-Protection
- DOMPurify integration
- Zod validation with sanitization
- XSS prevention
- SQL injection protection
- Configurable allowed origins
- Proper preflight handling
- Credential support
-
Copy environment template:
cp .env.production.template .env.local
-
Fill in your API keys in
.env.local:Firebase (Required):
NEXT_PUBLIC_FIREBASE_API_KEY=your_api_key NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your_project.firebaseapp.com NEXT_PUBLIC_FIREBASE_PROJECT_ID=your_project_id # ... other Firebase config
Stripe (Required):
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_xxxxx # Use LIVE keys for production STRIPE_SECRET_KEY=sk_live_xxxxx STRIPE_WEBHOOK_SECRET=whsec_xxxxx
Upstash Redis (Required for rate limiting):
UPSTASH_REDIS_REST_URL=https://xxx.upstash.io UPSTASH_REDIS_REST_TOKEN=xxxxx
Domain Configuration:
NEXT_PUBLIC_APP_URL=https://yourdomain.com ALLOWED_ORIGINS=https://yourdomain.com,https://www.yourdomain.com
-
Update Firebase authorized domains:
- Go to Firebase Console β Authentication β Settings
- Add your domain to "Authorized domains"
-
Configure Firestore security rules:
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /users/{userId} { allow read, write: if request.auth != null && request.auth.uid == userId; } } }
- Create products and prices in Stripe Dashboard
- Configure webhook endpoint:
- URL:
https://yourdomain.com/api/payment/webhook - Events:
checkout.session.completed,customer.subscription.*
- URL:
Option A: Co-located with app (simplest)
export COQUI_TOS_AGREED=1
docker-compose -f docker-compose.tts.yml up -dOption B: Separate server
- Deploy TTS service to dedicated server
- Update
XTTS_API_URLin environment variables
npm install
npm run build
npm run test # Optional: run tests-
Connect repository:
- Go to Vercel Dashboard
- Import your GitHub repository
-
Configure environment variables:
- Copy all variables from
.env.localto Vercel dashboard - Go to Project Settings β Environment Variables
- Copy all variables from
-
Configure custom domain:
- Go to Project Settings β Domains
- Add your custom domain
- Update DNS records as instructed
-
Deploy:
vercel --prod
-
Build for production:
npm run build npm start
-
Use PM2 for process management:
npm install -g pm2 pm2 start npm -- start pm2 startup pm2 save
-
Configure reverse proxy (Nginx example):
server { listen 80; server_name yourdomain.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name yourdomain.com; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
-
Authentication Flow:
- Sign up with email/password
- Login with Google
- Password reset
-
Payment Flow:
- Subscribe to premium plan
- Payment success/failure handling
- Webhook delivery
-
Chat Functionality:
- Send messages
- Receive AI responses
- Rate limiting kicks in after limits
-
Voice Synthesis:
- Generate voice from text
- Different voice options work
- Audio playback in browser
-
Security Headers:
- Check with Security Headers
- Verify CSP is not blocking resources
# Test rate limiting
curl -X POST https://yourdomain.com/api/chat \
-H "Content-Type: application/json" \
-d '{"message":"test"}' \
--rate 30/m # Should trigger rate limit
# Test security headers
curl -I https://yourdomain.com/
# Test TTS service
curl -X POST https://yourdomain.com/api/voice \
-H "Content-Type: application/json" \
-d '{"text":"Hello world","voiceId":"female-1"}'-
Sentry:
SENTRY_DSN=https://xxxxx@sentry.io/xxxxx
-
Vercel Analytics:
- Automatically enabled on Vercel Pro plan
- UptimeRobot (free): https://uptimerobot.com/
- Pingdom: https://pingdom.com/
- Vercel Speed Insights
- Google PageSpeed Insights
- Web.dev Measure
1. Build Fails:
# Clear Next.js cache
rm -rf .next
npm run build2. CORS Errors:
- Check
ALLOWED_ORIGINSin.env.local - Verify middleware configuration
- Check browser developer tools
3. Rate Limiting Not Working:
- Verify Upstash Redis credentials
- Check Redis connection in logs
- Ensure middleware is running
4. TTS Service Not Responding:
# Check TTS service status
docker ps | grep xtts
# Check TTS logs
docker-compose -f docker-compose.tts.yml logs -f
# Restart TTS service
docker-compose -f docker-compose.tts.yml restart5. Firebase Auth Issues:
- Check authorized domains in Firebase Console
- Verify API keys are correct
- Check browser network tab for errors
Large Bundle Size:
# Analyze bundle
npm install -g @next/bundle-analyzer
ANALYZE=true npm run buildSlow API Responses:
- Check database query performance
- Implement caching for frequent requests
- Optimize AI model inference
- Vercel: Automatically scales serverless functions
- Self-hosted: Use load balancer + multiple instances
- Database: Consider migrating from Firebase to PostgreSQL
- TTS: Deploy multiple TTS instances with load balancer
- Monitor Vercel/Firebase usage
- Implement response caching
- Optimize bundle size
- Use CDN for static assets
- Update dependencies monthly:
npm audit fix - Rotate API keys quarterly
- Review security headers annually
- Monitor error logs weekly
- Check uptime monitoring alerts
- All dependencies up to date
- No critical security vulnerabilities
- Rate limiting functioning
- HTTPS enforced
- Security headers present
- Input sanitization working
- Error messages don't leak info
If you encounter issues:
- Check logs: Vercel dashboard or server logs
- Run deployment check:
./scripts/deploy-check.sh - Review environment variables
- Test individual components
Your deployment is successful when:
- β All API endpoints respond correctly
- β Authentication flow works end-to-end
- β Payment processing completes successfully
- β Voice synthesis generates audio
- β Rate limiting prevents abuse
- β Security headers are present
- β No critical errors in logs
- β Performance metrics are acceptable
Congratulations! Your Therapeutic AI Assistant is now live! π