← Blog

Claude API Vercel Serverless Deployment Guide

2026-11-22 · 5 min read · SubToAPI Team

Deploying Claude API calls inside Vercel serverless functions works well once you account for three things Vercel handles differently than a traditional server: function duration limits, runtime choice (Node vs Edge), and how streaming responses pass through the platform. Get those right and a Claude-powered endpoint on Vercel is just a few lines of code.

The short answer: use a Node.js serverless function for most use cases, switch to the Edge runtime only if you need lower latency at the edge and don't need Node-specific APIs, set maxDuration explicitly for long completions, and stream responses with ReadableStream so Vercel doesn't buffer the whole reply before sending it to the client. Below is how to set each of these up.

Choosing a Runtime: Node vs Edge

Vercel functions run in one of two runtimes:

If you're doing anything beyond a single request/response — file handling, complex middleware, database writes with a Node-only driver — stick with the Node runtime. For a thin passthrough endpoint that streams tokens to the browser, Edge is a good fit.

Node.js Serverless Function Example

A basic api/chat.js route under the Pages Router (or app/api/chat/route.js for App Router) looks like this:

// api/chat.js
export const config = {
  maxDuration: 60,
};

export default async function handler(req, res) {
  const { messages } = req.body;

  const response = await fetch('https://api.anthropic.com/v1/messages', {
    method: 'POST',
    headers: {
      'content-type': 'application/json',
      'x-api-key': process.env.ANTHROPIC_API_KEY,
      'anthropic-version': '2023-06-01',
    },
    body: JSON.stringify({
      model: 'claude-sonnet-4-5-20250929',
      max_tokens: 1024,
      messages,
    }),
  });

  const data = await response.json();
  res.status(response.status).json(data);
}

The maxDuration config is the part people miss most often. Claude completions, especially with larger max_tokens or extended thinking, can run past Vercel's default function timeout. Set it explicitly and match it to your plan's ceiling.

Edge Function Example with Streaming

For streaming token-by-token responses, the App Router's Edge runtime pairs naturally with Claude's SSE stream:

// app/api/chat/route.js
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.ANTHROPIC_API_KEY,
      'anthropic-version': '2023-06-01',
    },
    body: JSON.stringify({
      model: 'claude-sonnet-4-5-20250929',
      max_tokens: 1024,
      stream: true,
      messages,
    }),
  });

  return new Response(upstream.body, {
    headers: { 'Content-Type': 'text/event-stream' },
  });
}

Passing upstream.body straight through as the Response body avoids buffering — Vercel's Edge network forwards the stream as it arrives instead of waiting for the full completion. This matters a lot for perceived latency: a chat UI that shows the first token in 300ms feels completely different from one that waits 4 seconds for a full JSON blob.

Environment Variables and Secrets

Claude API keys and any proxy credentials should live in Vercel's environment variable settings, never hardcoded or committed:

vercel env add ANTHROPIC_API_KEY

Set separate values per environment (Development, Preview, Production) so preview deployments don't silently burn production quota. If multiple team members or services need scoped access without sharing one raw key, that's a good argument for an API key layer in front of the raw credential — more on that below.

Avoiding Common Deployment Pitfalls

{
  "functions": {
    "api/chat.js": {
      "regions": ["iad1"]
    }
  }
}

Where an API Layer Helps

Once you have more than one serverless function calling Claude — a chat route, a summarization route, a background job — managing raw Anthropic keys across environments gets messy fast: rotating a key means touching every deployment, and you lose visibility into which function is burning tokens.

SubToAPI sits between your Vercel functions and Claude as a clean HTTPS layer: you generate scoped sub_live_... keys per project or per function instead of distributing one shared key, and get usage metadata per key so you can see which serverless route is driving cost. The request/response shape matches what you'd already send to /v1/messages, so swapping the endpoint and auth header in the code above is the only change needed — see the quickstart and messages docs. Streaming works the same way through /docs/streaming, and if your functions use tool calling, /docs/tools covers that. Plans start at €9/month on the pricing page, with a free trial at signup.

Questions

Do I need the Edge runtime to stream Claude responses on Vercel? No. Both Node and Edge runtimes support streaming by returning a ReadableStream-backed Response (App Router) or piping chunks (Pages Router). Edge gives lower cold-start latency and global distribution; Node gives full API compatibility. Pick based on what else your function needs to do.

Why does my Claude API call time out on Vercel but work locally? Vercel enforces a function execution limit that's much shorter than your local dev server's unlimited runtime. Set maxDuration in your function config (or vercel.json) to the maximum your plan allows, and reduce max_tokens or use streaming so the client starts receiving data before the full completion finishes.

Can I call Claude directly from the browser instead of a serverless function? Not safely — it would expose your API key client-side. Always proxy the request through a Vercel serverless or edge function (or a service like SubToAPI) that holds the credential server-side and forwards only the response to the browser.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →