---
title: Deploy to Vercel
description: 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.

> **Note**
>
> If you haven't set up your project yet, start with the
> [Quickstart](/quickstart) to scaffold and run locally first.

## 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](/gateways/vercel) to access 120+ models from OpenAI, Anthropic, Google, and more.

1. Go to [Vercel AI Gateway](https://vercel.com/ai-gateway)
2. Create an API key
3. Add to environment: `AI_GATEWAY_API_KEY`

You can switch to [OpenRouter](/gateways/openrouter) or any other [supported gateway](/gateways/overview) in `chat.config.ts`. See [Multi-Model Support](/core/multi-model) 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](../storage) for the provider-agnostic architecture and migration behavior.

1. Go to **Storage** → **Create** → **Blob**
2. Connect to your project
3. Environment variable `BLOB_READ_WRITE_TOKEN` is auto-added

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

```typescript
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 **Storage** → **Create** → **KV**
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`:

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

Runs daily at 2 AM UTC. Adjust the schedule using [cron syntax](https://crontab.guru/).

### Security

The cron endpoint requires a `CRON_SECRET` environment variable:

```bash
# 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:

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

## Code Execution Sandbox

The code execution tool uses [Vercel Sandbox](https://vercel.com/docs/functions/og-image-generation) for secure Python execution.

### Authentication

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

```bash
VERCEL_TEAM_ID=team_xxx
VERCEL_PROJECT_ID=prj_xxx
VERCEL_TOKEN=xxx
```

### Runtime Configuration

Set the Python version via environment variable:

```bash
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:

```bash
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](https://vercel.com/docs/security/vercel-firewall) in your project settings. Create a rate limiting rule for the chat endpoint:

1. Go to **Settings** → **Firewall**
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](https://vercel.com/docs/security/vercel-waf) 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](https://vercel.com/docs/projects/environment-variables) to set secrets per environment (production, preview, development)
- Rotate `AUTH_SECRET` and API keys periodically

## Production Checklist

Before going live:

<Accordion>
  <AccordionItem title="Enable production domain">
    1. Go to **Settings** → **Domains** 2. Add your custom domain 3. Update
    OAuth callback URLs to use the new domain
  </AccordionItem>
  <AccordionItem title="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 ```
  </AccordionItem>
  <AccordionItem title="Enable rate limiting">
    Set up [Vercel Firewall](#vercel-firewall) rules for the `/api/chat`
    endpoint to prevent abuse. See the section above for details.
  </AccordionItem>
  <AccordionItem title="Set up monitoring">
    - Enable [Vercel Analytics](https://vercel.com/analytics) for web vitals -
    Configure [Langfuse](https://langfuse.com) for LLM observability (optional)
  </AccordionItem>
  <AccordionItem title="Review storage limits">
    Check your plan limits for: - Blob storage (file count and size) - KV
    operations (for resumable streams) - AI Gateway usage
  </AccordionItem>
</Accordion>

## Troubleshooting

<Accordion>
  <AccordionItem title="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)
  </AccordionItem>
  <AccordionItem title="Blob upload fails">
    - Ensure `BLOB_READ_WRITE_TOKEN` is set - Check blob storage isn't at
    capacity - Verify file size is under 500MB limit
  </AccordionItem>
  <AccordionItem title="Code execution times out">
    - Default timeout is 5 minutes - Check sandbox logs in Vercel dashboard -
    Ensure OIDC is working (automatic on Vercel)
  </AccordionItem>
</Accordion>
