← Blog

Claude API Vercel Edge Function Integration Guide

2026-10-21 · 5 min read · SubToAPI Team

Calling Claude from a Vercel Edge Function works, but it needs a different approach than a typical Node.js API route. Edge Functions run on a lightweight V8 runtime, not Node.js, which means the standard Anthropic SDK behavior around Node-specific modules, long-lived connections, and buffering doesn't carry over cleanly. The reliable pattern is to talk to the API directly with fetch and stream the response back using the Web Streams API.

This matters because Edge Functions exist specifically for low-latency, globally distributed requests — exactly the kind of experience you want for an LLM-backed chat feature. Done right, a Claude-powered endpoint on Vercel's Edge runtime starts streaming tokens to the browser almost immediately, with no cold-start penalty tied to a specific region.

Why run Claude calls on the Edge runtime

Vercel's Edge runtime deploys your function to every region in the network instead of one. For an LLM request, the actual latency bottleneck is almost always the model generating tokens, not network hops — but Edge still helps with:

The tradeoff is that you lose access to Node APIs like fs, native crypto module functions, and some SDKs that assume a Node environment.

Edge runtime constraints you need to know

Before wiring anything up, be aware of these limits:

A basic non-streaming request

Here's a minimal Next.js App Router Edge route that sends a prompt and returns the full response:

// app/api/ask/route.js
export const runtime = 'edge';

export async function POST(req) {
  const { prompt } = 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.ANTHROPIC_API_KEY,
      'anthropic-version': '2023-06-01',
    },
    body: JSON.stringify({
      model: 'claude-sonnet-4-5',
      max_tokens: 1024,
      messages: [{ role: 'user', content: prompt }],
    }),
  });

  const data = await response.json();
  return Response.json(data);
}

This works fine for short responses, but for anything conversational, streaming is the better experience — and the point where Edge Functions really pay off.

Streaming Claude responses from the Edge

To stream, set stream: true on the request and forward the response body directly instead of waiting for .json():

// 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',
      max_tokens: 1024,
      stream: true,
      messages,
    }),
  });

  return new Response(upstream.body, {
    headers: {
      'content-type': 'text/event-stream',
      'cache-control': 'no-cache',
      connection: 'keep-alive',
    },
  });
}

Because upstream.body is already a ReadableStream, you can pass it straight through — no manual chunk parsing needed on the server side. The client reads it as server-sent events and appends text deltas as they arrive.

If you need to transform the stream (e.g., strip SSE framing and forward plain text chunks), use a TransformStream:

const { readable, writable } = new TransformStream({
  transform(chunk, controller) {
    const text = new TextDecoder().decode(chunk);
    controller.enqueue(new TextEncoder().encode(text));
  },
});

upstream.body.pipeTo(writable);
return new Response(readable);

Handling tool use inside an Edge Function

Tool use adds a second round trip: Claude returns a tool_use block, your code executes the tool, then you send the result back in a follow-up message. This is fully compatible with Edge Functions as long as your tool execution itself doesn't depend on Node-only APIs (database drivers using raw TCP sockets, for example, often need the Node runtime instead). If your tool logic needs Node APIs, route that specific call through a regular serverless function and keep only the Claude request/response loop on the Edge.

Using SubToAPI instead of calling Anthropic directly

If you're already managing multiple Claude integrations, routing Edge Function traffic through SubToAPI simplifies key management: you get a single sub_live_... key per application instead of juggling raw Anthropic credentials across environments, plus usage metadata per request and streaming support out of the box.

The Edge code barely changes — swap the endpoint and header:

export const runtime = 'edge';

export async function POST(req) {
  const { messages } = await req.json();

  const upstream = await fetch('https://api.subtoapi.app/v1/messages', {
    method: 'POST',
    headers: {
      'content-type': 'application/json',
      authorization: `Bearer ${process.env.SUBTOAPI_KEY}`,
    },
    body: JSON.stringify({
      model: 'claude-sonnet-4-5',
      max_tokens: 1024,
      stream: true,
      messages,
    }),
  });

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

Check /docs/streaming for the exact event format and /docs/quickstart for setting up your first sub_live_ key. Plans start at €9/month on the Solo tier, with team seats on the Team (€19/seat) and Scale (€49/seat) plans — see /pricing — and a free trial at /signup.

questions

Does the official Anthropic SDK run on Vercel's Edge runtime? Partially. It depends on the SDK version and which Node-specific internals it touches. The safest, most portable approach is calling the REST API directly with fetch, which has zero Node dependencies and works identically on Edge and Node runtimes.

Will streaming responses time out on Vercel's Edge Functions? Streaming keeps the connection actively sending data, which is treated differently from an idle hold. As long as tokens keep arriving, the request stays alive. Check your current plan's execution limits in the Vercel dashboard if you expect very long generations.

Can I use Claude's tool use feature in an Edge Function? Yes, as long as the tool execution code itself is Edge-compatible (fetch-based, no Node-only drivers). If a tool needs a Node API, call that tool through a standard serverless function and keep the Claude request loop on the Edge Function.

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 →