Skip to content
ChatJS
Esc
navigateopen⌘Jpreview
On this page

Deploy to Vercel

Production deployment with Vercel's integrated platform

Vercel is the recommended deployment platform. It provides integrated storage, AI Gateway, and edge infrastructure with zero configuration.

Vercel Integrations

ChatJS uses several Vercel platform features. Enable them in your Vercel dashboard under Storage and AI.

Integration Purpose Required
Vercel Postgres or Neon Primary database Yes
Vercel Blob File attachments, generated images If using attachments/image gen
Vercel KV Resumable streams (Redis) Optional
AI Gateway Unified access to 120+ AI models Yes

AI Gateway

By default, ChatJS uses Vercel AI Gateway to access 120+ models from OpenAI, Anthropic, Google, and more.

  1. Go to Vercel AI Gateway
  2. Create an API key
  3. Add to environment: AI_GATEWAY_API_KEY

You can switch to OpenRouter or any other supported gateway in chat.config.ts. See Multi-Model Support for configuring available models.

Blob Storage

Vercel Blob is the recommended Files SDK provider for file attachments and generated media on Vercel. See File storage for the provider-agnostic architecture and migration behavior.

  1. Go to StorageCreateBlob
  2. Connect to your project
  3. Environment variable BLOB_READ_WRITE_TOKEN is auto-added

To disable blob features, set in chat.config.ts:

features: {
  attachments: false,
},
ai: {
  tools: {
    image: { enabled: false },
    video: { enabled: false },
  },
}

KV (Redis)

Enables resumable streams - users can refresh the page mid-generation and continue where they left off.

  1. Go to StorageCreateKV
  2. Connect to your project
  3. Environment variable REDIS_URL is auto-added

Without Redis, streams work normally but can’t be resumed after disconnection.

Cron Jobs

ChatJS includes a daily cleanup job that removes orphaned managed files uploaded but never saved to a message.

Configuration

Defined in vercel.json:

{
  "crons": [
    {
      "path": "/api/cron/cleanup",
      "schedule": "0 2 * * *"
    }
  ]
}

Runs daily at 2 AM UTC. Adjust the schedule using cron syntax.

Security

The cron endpoint requires a CRON_SECRET environment variable:

# Generate a secret
openssl rand -base64 32

Add to Vercel environment variables. Vercel automatically sends this as a Bearer token.

Customizing Cleanup

Edit app/api/cron/cleanup/route.ts to add cleanup tasks:

const results = {
  orphanedAttachments: await cleanupOrphanedAttachments(),
  // Add other cleanup tasks here
  expiredSessions: await cleanupExpiredSessions(),
};

Code Execution Sandbox

The code execution tool uses Vercel Sandbox for secure Python execution.

Authentication

On Vercel, sandbox uses OIDC automatically. For local development or self-hosted:

VERCEL_TEAM_ID=team_xxx
VERCEL_PROJECT_ID=prj_xxx
VERCEL_TOKEN=xxx

Runtime Configuration

Set the Python version via environment variable:

VERCEL_SANDBOX_RUNTIME=python3.13  # default

Resource Limits

Sandboxes run with:

  • 2 vCPUs
  • 5 minute timeout
  • Pre-installed: matplotlib, pandas, numpy, sympy, yfinance

Environment Variables

Required

Variable Description
DATABASE_URL PostgreSQL connection string
AUTH_SECRET Session encryption key
AI_GATEWAY_API_KEY Vercel AI Gateway key (or OPENROUTER_API_KEY if using OpenRouter)

Optional (Vercel Features)

Variable Feature
BLOB_READ_WRITE_TOKEN Blob storage (auto-set by integration)
REDIS_URL KV/Redis for resumable streams
CRON_SECRET Secure cron endpoint

Pull from Vercel

After linking your project, pull all environment variables:

vercel link
vercel env pull .env.local

Rate Limiting and Security

The /api/chat endpoint is expensive (it calls AI providers on every request). Protect it from abuse in production.

Vercel Firewall

Enable Vercel Firewall in your project settings. Create a rate limiting rule for the chat endpoint:

  1. Go to SettingsFirewall
  2. Add a rule targeting POST /api/chat
  3. Set a rate limit (for example, 20 requests per minute per IP)
  4. Choose Challenge or Block as the action

Vercel WAF

For additional protection, enable the Web Application Firewall to block common attack patterns (SQL injection, XSS) at the edge before requests reach your application.

Environment Variable Security

  • Never commit .env.local to version control
  • Use Vercel’s environment variable management to set secrets per environment (production, preview, development)
  • Rotate AUTH_SECRET and API keys periodically

Production Checklist

Before going live:

Enable production domain
  1. Go to SettingsDomains
  2. Add your custom domain
  3. Update OAuth callback URLs to use the new domain
Configure OAuth for production

Update your OAuth apps (GitHub, Google) with production callback URLs:

https://yourdomain.com/api/auth/callback/github
https://yourdomain.com/api/auth/callback/google
Enable rate limiting

Set up Vercel Firewall rules for the /api/chat endpoint to prevent abuse. See the section above for details.

Set up monitoring
Review storage limits

Check your plan limits for:

  • Blob storage (file count and size)
  • KV operations (for resumable streams)
  • AI Gateway usage

Troubleshooting

Cron job not running
  • Verify CRON_SECRET is set in environment variables
  • Check Vercel dashboard → Logs → filter by /api/cron
  • Crons only run in production (not preview deployments)
Blob upload fails
  • Ensure BLOB_READ_WRITE_TOKEN is set
  • Check blob storage isn’t at capacity
  • Verify file size is under 500MB limit
Code execution times out
  • Default timeout is 5 minutes
  • Check sandbox logs in Vercel dashboard
  • Ensure OIDC is working (automatic on Vercel)

Was this page helpful?