Claude API Chatbot Deployment on Vercel: Full Guide
Deploying a Claude-powered chatbot on Vercel means wiring a serverless (or edge) function to the Claude API, streaming the response back to your frontend, and keeping your API key out of the browser. Vercel handles the hosting, scaling and CDN; you handle the request/response logic and the system prompt. This guide walks through the whole setup, from project structure to streaming to the gotchas that trip people up in production.
The short answer: create an API route under /api, call Claude from that route (never from client-side JavaScript), stream the output using the Fetch streaming API or Vercel's AI SDK helpers, and store your key as an environment variable in the Vercel dashboard. Everything below expands on that.
Project structure
A minimal Next.js chatbot on Vercel looks like this:
/app
/api/chat/route.ts <- serverless function, calls Claude
/page.tsx <- chat UI
.env.local <- local dev secrets (never committed)
If you're not using Next.js, any Vercel-supported framework works the same way: your frontend calls your own backend route, and that route is the only place that holds the API key.
Setting up the API route
Here's a basic non-streaming handler first, so the mechanics are clear before adding streaming:
// app/api/chat/route.ts
export async function POST(req) {
const { messages } = await req.json();
const response = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": process.env.CLAUDE_API_KEY,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify({
model: "claude-sonnet-4",
max_tokens: 1024,
messages,
}),
});
const data = await response.json();
return Response.json(data);
}
This works, but every call waits for the full response before the user sees anything — not acceptable for a chat UI where users expect a typing effect.
Adding streaming
Claude supports server-sent events when you set "stream": true. Vercel's edge and serverless functions can both forward a streaming response, but edge runtime is the better fit for low-latency chat because it skips cold-start overhead and runs closer to the user.
// app/api/chat/route.ts
export const runtime = "edge";
export async function POST(req) {
const { messages } = await req.json();
const upstream = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": process.env.CLAUDE_API_KEY,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify({
model: "claude-sonnet-4",
max_tokens: 1024,
stream: true,
messages,
}),
});
return new Response(upstream.body, {
headers: { "Content-Type": "text/event-stream" },
});
}
On the frontend, read the stream with ReadableStream or a helper library, and append each chunk to the message bubble as it arrives. This is what gives you the familiar token-by-token chat experience.
Environment variables and secrets
Never put your Claude key in client code, in a public repo, or in a NEXT_PUBLIC_* variable — that prefix exposes it to the browser bundle. Instead:
- Add the key in the Vercel dashboard under Project Settings → Environment Variables.
- Set it for Production, Preview, and Development separately if you use different keys per environment.
- Reference it only inside server-side files (API routes, server components) — never in files that ship to the client.
If you're building something that will eventually have multiple customers hitting your chatbot through your own app, consider whether you want to proxy every request through one shared key or give each tenant a scoped key. Scoped keys make billing, abuse control, and usage attribution far easier, and it's not something you get for free from the raw Claude API.
Handling rate limits and retries on serverless
Vercel functions have execution time limits (10s on Hobby, longer on paid plans), and cold starts can add latency on low-traffic routes. For a chatbot, keep these in mind:
- Set
max_tokensconservatively so a single response can't run past your function timeout. - Catch 429/529 responses from Claude and retry with backoff, but cap retries at 1–2 attempts inside a serverless function since the user is waiting live.
- Log token usage per request if you plan to bill users or track costs — Claude's response includes usage metadata you should capture, not discard.
Where SubToAPI fits
If you'd rather not manage raw Anthropic keys, rotate secrets manually, or build your own usage dashboard, SubToAPI sits between your Vercel function and Claude. You create application-scoped keys (sub_live_...) per project or per environment, point your fetch calls at https://api.subtoapi.app/v1/messages instead of Anthropic's endpoint, and get streaming, tool use, and usage metadata without changing your chatbot's architecture. The request shape stays the same — you're swapping a base URL and an Authorization: Bearer $SUBTOAPI_KEY header. Check /docs/quickstart for the setup and /docs/streaming for the streaming-specific details.
This matters most once you have more than one app or more than one teammate deploying chatbots: instead of sharing a raw Anthropic key across projects, each Vercel deployment gets its own SubToAPI key, and you can see exactly which app is burning tokens from one dashboard.
Deployment checklist
Before pushing to production:
- [ ] API key stored as a server-only environment variable, not hardcoded
- [ ] Streaming route tested with a slow network throttle to confirm chunks render correctly
- [ ] Error states handled (timeout, rate limit, malformed response) with a visible fallback message in the UI
- [ ] Token usage logged per request if cost tracking matters
- [ ] Preview deployments use a separate key or limited quota so testing doesn't inflate production usage
Once deployed, Vercel's preview URLs make it easy to share a working chatbot with stakeholders before merging to main — just make sure the preview environment variable points to a key you're comfortable exposing to testers.
Questions
Does Vercel's free tier support streaming Claude responses? Yes. Edge functions on the Hobby plan support streaming responses; the main constraints are execution time limits and regional availability, not streaming support itself.
Should I call the Claude API directly from the browser? No. Client-side calls expose your API key to anyone who opens dev tools. Always proxy through a server-side route, which is also where you'd apply rate limiting or usage tracking.
Can I use the same setup for a team or multi-app chatbot? Yes, the Vercel route logic doesn't change. For managing multiple keys and seeing usage across apps, a layer like SubToAPI (see /pricing) adds per-key tracking and team seats without rewriting your chatbot code.