← Blog

Claude API Cloudflare Workers Integration Guide

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

Integrating the Claude API with Cloudflare Workers means calling an LLM from the edge instead of a traditional server: no cold-start containers, no Node.js runtime, and a request lifecycle that's built around fetch and Response objects. The integration itself is straightforward — Workers can make outbound HTTPS requests like any JavaScript environment — but streaming, secrets handling, and CPU time limits behave differently than on Lambda or a VPS.

This guide covers the practical setup: project scaffolding with Wrangler, making a request to Claude from a Worker, streaming tokens back to the client, storing API keys securely, and the platform limits you need to design around.

Why run Claude API calls on Cloudflare Workers

Workers run on V8 isolates deployed to Cloudflare's global network. For an LLM integration, the main benefits are:

The tradeoff is a constrained runtime: no native Node.js APIs by default, a CPU time budget per request (not wall-clock time, so waiting on a network response doesn't count against it), and a limit on concurrent outbound subrequests. For streaming LLM responses, this is actually fine, since the Worker spends most of its time waiting on I/O, which is free.

Project setup with Wrangler

Scaffold a new Worker:

npm create cloudflare@latest -- claude-worker
cd claude-worker

Pick the "Hello World" Worker template with TypeScript if asked. Your wrangler.toml should look roughly like this:

name = "claude-worker"
main = "src/index.ts"
compatibility_date = "2024-09-01"

Store your API key as a secret rather than a plain environment variable:

npx wrangler secret put CLAUDE_API_KEY

This keeps the key out of your source code and wrangler.toml, and out of version control entirely.

Making a basic request

Inside the Worker, calling Claude is a normal fetch call. Here's a minimal non-streaming example hitting the Messages API directly:

export default {
  async fetch(request, env) {
    const { message } = await request.json();

    const res = await fetch("https://api.anthropic.com/v1/messages", {
      method: "POST",
      headers: {
        "content-type": "application/json",
        "x-api-key": env.CLAUDE_API_KEY,
        "anthropic-version": "2023-06-01",
      },
      body: JSON.stringify({
        model: "claude-sonnet-4-5",
        max_tokens: 1024,
        messages: [{ role: "user", content: message }],
      }),
    });

    return new Response(await res.text(), {
      headers: { "content-type": "application/json" },
    });
  },
};

This works, but every request to Anthropic's native API also needs the provisioned-access approval that comes with direct Claude API accounts. If you're already paying for Claude as an individual or team and just want a key you can drop into a Worker, SubToAPI issues sub_live_... keys backed by your existing Claude access, so the request shape above barely changes — you swap the endpoint and headers, as covered in the quickstart.

const res = await fetch("https://api.subtoapi.app/v1/messages", {
  method: "POST",
  headers: {
    "content-type": "application/json",
    "authorization": `Bearer ${env.SUBTOAPI_KEY}`,
  },
  body: JSON.stringify({
    model: "claude-sonnet-4-5",
    max_tokens: 1024,
    messages: [{ role: "user", content: message }],
  }),
});

Full request and response shapes are documented at /docs/messages.

Streaming responses from a Worker

Chat interfaces need tokens as they're generated, not after the full response completes. Workers support this well because fetch responses expose a readable stream you can pipe straight through:

export default {
  async fetch(request, env) {
    const { message } = await request.json();

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

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

Because you're just forwarding upstream.body, the Worker never buffers the full response in memory, which keeps CPU time low and avoids hitting the body size limits that apply to buffered responses. See /docs/streaming for event formats if you need to parse server-sent events on the client instead of forwarding them raw.

Handling tool use at the edge

If your Worker needs Claude to call tools — database lookups, internal APIs, calculations — define the tool schemas in your request body as usual and handle the tool_use content block in the response before sending a follow-up request with the tool result. Workers are a natural fit for this pattern since the tool execution (e.g., a KV read or a call to another Worker) is also fast at the edge. Schema details are in /docs/tools.

CORS and client-side calls

If a browser is calling your Worker directly, add CORS headers to every response, including the streaming one:

function withCors(response) {
  response.headers.set("access-control-allow-origin", "*");
  response.headers.set("access-control-allow-headers", "content-type");
  return response;
}

Never expose your Claude or SubToAPI key to the browser — the Worker should be the only thing holding the secret, with the client calling your Worker's own route.

Limits to design around

Can I use the Anthropic SDK inside a Cloudflare Worker?

The official Node SDK relies on Node-specific APIs that aren't available by default in the Workers runtime. Plain fetch calls, as shown above, are the reliable approach and work identically for both Anthropic's native API and SubToAPI's endpoints.

Does streaming work the same way in Workers as in Node?

Yes, functionally. Workers expose ReadableStream natively, and you can forward response.body from your upstream fetch call straight into your Worker's Response without buffering, which is actually simpler than some Node streaming setups.

How do I keep my API key out of the client bundle?

Store it with wrangler secret put and reference it via the env parameter passed to your Worker's fetch handler. It's injected at runtime and never bundled into your deployed script or exposed to the browser. If you're using SubToAPI, generate a key at /signup and store it the same way — see /pricing for plan details.

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 →