Deploy to Vercel
Complete guide to deploying Babylon to Vercel with Upstash Redis and Neon PostgreSQL.
Prerequisites
Before deploying, ensure you have:
- Vercel account (vercel.com )
- GitHub repository with Babylon code
- Neon account for PostgreSQL (neon.tech )
- Upstash account for Redis (upstash.com )
- Privy account (privy.io )
Step 1: Database Setup (Neon)
Create PostgreSQL Database
- Sign up at neon.tech
- Create a new project
- Name:
babylon-production - Region: Choose closest to your users
- Copy connection strings:
- Pooled connection (for API routes)
- Direct connection (for migrations)
Example URLs:
# Pooled connection (use for DATABASE_URL)
postgresql://user:pass@ep-xxx.us-east-2.aws.neon.tech/babylon?sslmode=require
# Direct connection (use for migrations)
postgresql://user:pass@ep-xxx.us-east-2.aws.neon.tech/babylon?sslmode=require&direct=trueRun Migrations
From your local machine:
# Set DATABASE_URL to Neon direct connection
export DATABASE_URL="postgresql://..."
# Run migrations
bun run db:migrate deploy
# Seed initial data
bun run db:seedStep 2: Redis Setup (Upstash)
Create Redis Database
- Sign up at upstash.com
- Create new database
- Name:
babylon-production - Region: Same as your Vercel deployment
- Type: Regional (or Global for multi-region)
- Copy credentials:
- REST URL
- REST Token
Example:
UPSTASH_REDIS_REST_URL=https://xxx.upstash.io
UPSTASH_REDIS_REST_TOKEN=AabBcc...Step 3: Vercel Deployment
Connect Repository
- Go to vercel.com/new
- Import your GitHub repository
- Configure project:
- Framework Preset: Next.js
- Root Directory:
./(or leave empty) - Build Command:
bun run build - Install Command:
bun install
Environment Variables
Add these in Vercel dashboard under Settings → Environment Variables:
Required Variables
# Database (Neon)
DATABASE_URL="postgresql://user:pass@host/babylon?sslmode=require&pgbouncer=true"
DIRECT_DATABASE_URL="postgresql://user:pass@host/babylon?sslmode=require"
# Redis (Upstash)
UPSTASH_REDIS_REST_URL="https://xxx.upstash.io"
UPSTASH_REDIS_REST_TOKEN="your_token"
# Privy Authentication
NEXT_PUBLIC_PRIVY_APP_ID="clp..."
PRIVY_APP_SECRET="your_secret"
# AI Provider
OPENAI_API_KEY="sk-..."
# OR
GROQ_API_KEY="gsk_..."
# Cron Secret (generate with: openssl rand -hex 32)
CRON_SECRET="your_random_secret"
# App URL
NEXT_PUBLIC_API_URL="https://babylon.market"Optional but Recommended
# Storage (Vercel Blob)
BLOB_READ_WRITE_TOKEN="vercel_blob_token"
# Blockchain
BASE_SEPOLIA_RPC_URL="https://sepolia.base.org"
BASE_RPC_URL="https://mainnet.base.org"
# Social Auth
TWITTER_CLIENT_ID="your_client_id"
TWITTER_CLIENT_SECRET="your_client_secret"
FARCASTER_CLIENT_ID="your_client_id"
FARCASTER_CLIENT_SECRET="your_client_secret"Deploy
Click Deploy and wait for the build to complete (usually 2-3 minutes).
Step 4: Vercel Integrations
Add Neon Integration
- Go to Integrations in Vercel dashboard
- Search for “Neon”
- Click Add Integration
- Follow prompts to connect your Neon database
- This will automatically set
DATABASE_URLfor all environments
Add Upstash Integration
- Go to Integrations in Vercel dashboard
- Search for “Upstash”
- Click Add Integration
- Follow prompts to connect your Upstash database
- This will automatically set Redis environment variables
Benefits of Integrations
- Automatic environment variable management
- Zero-downtime database migrations
- Connection pooling optimization
- Monitoring and alerts
Step 5: Configure Domains
Custom Domain
- Go to Settings → Domains
- Add your custom domain (e.g.,
babylon.market) - Add DNS records as instructed by Vercel
- Wait for DNS propagation (5-60 minutes)
Update Privy
After domain is configured:
- Go to dashboard.privy.io
- Add your production domain to allowed domains
- Update callback URLs
Step 6: Cron Jobs
Vercel automatically configures cron jobs from vercel.json:
{
"crons": [
{
"path": "/api/cron/game-tick",
"schedule": "* * * * *"
},
{
"path": "/api/reputation/sync",
"schedule": "0 */3 * * *"
}
]
}Verify Cron Setup
Check cron logs in Vercel dashboard:
- Go to Deployment
- Click Functions
- View cron execution logs
Step 7: Monitoring
Enable Vercel Analytics
- Go to Analytics tab
- Enable Web Analytics
- Enable Speed Insights
Setup Error Tracking
Add Sentry or similar:
# Install Sentry
bun add @sentry/nextjs
# Configure in next.config.mjsDatabase Monitoring
Neon provides built-in monitoring:
- Go to your Neon project
- View Monitoring tab
- Check query performance and connection pools
Performance Optimization
Edge Caching
Configure in next.config.mjs:
export default {
// Enable edge caching for static assets
images: {
domains: ['babylon.market'],
},
// Configure headers for caching
async headers() {
return [
{
source: '/api/markets/:path*',
headers: [
{
key: 'Cache-Control',
value: 's-maxage=60, stale-while-revalidate',
},
],
},
];
},
};Connection Pooling
Ensure Prisma uses connection pooling:
DATABASE_URL="postgresql://...?pgbouncer=true&connection_limit=10"Redis Caching
Cache frequently accessed data:
import { redis } from '@/lib/redis';
// Cache for 5 minutes
await redis.set('markets:active', JSON.stringify(markets), 'EX', 300);Troubleshooting
Build Failures
Issue: Module not found errors
Solution: Ensure all dependencies are in package.json:
bun install --productionIssue: Prisma Client not generated
Solution: Add to package.json:
{
"scripts": {
"postinstall": "prisma generate"
}
}Database Connection Errors
Issue: P1001: Can't reach database server
Solution: Check these:
- Verify
DATABASE_URLis correct - Ensure database is not suspended (Neon free tier)
- Check IP allowlist in Neon dashboard
Redis Connection Errors
Issue: ECONNREFUSED or timeout
Solution:
- Verify
UPSTASH_REDIS_REST_URLand token - Check Upstash dashboard for database status
- Ensure using REST API (not Redis protocol)
Cron Jobs Not Running
Issue: Cron endpoint returns 401
Solution: Verify CRON_SECRET matches in:
- Vercel environment variables
- Your cron endpoint authorization check
Multi-Region Deployment
For global low-latency:
1. Enable Edge Functions
// app/api/route.ts
export const runtime = 'edge';2. Use Upstash Global
Upgrade Upstash to Global plan for multi-region replication.
3. Read Replicas
Configure Neon read replicas:
DATABASE_URL="postgresql://primary..."
DIRECT_DATABASE_URL="postgresql://direct..."
REPLICA_URL="postgresql://replica..."Cost Optimization
Neon
- Free Tier: 0.5 GB storage, 10k rows
- Scale: Start at $19/month
- Tip: Use autoscaling to reduce costs
Upstash
- Free Tier: 10k commands/day
- Scale: Pay per request
- Tip: Set TTL on cached data
Vercel
- Hobby: Free for personal projects
- Pro: $20/month for production
- Tip: Optimize function execution time
Next Steps
Support
Having issues deploying?
- Check Vercel Status
- Review Build Logs
- Join our Discord
- Open a GitHub Issue