Claude API on Cloudflare Workers: Deployment Guide
Deploying the Claude API on Cloudflare Workers means calling Anthropic's HTTPS endpoints from a serverless edge function instead of a traditional backend. It works because Workers support the standard fetch API and ReadableStream, which is all you need to send requests and consume streaming responses — but there are a handful of runtime constraints (no Node.js SDK internals, limited CPU time, secrets handling) that trip people up on first deployment.
This guide walks through a working setup: project scaffolding, calling Claude directly from a Worker, streaming responses back to the client, handling secrets correctly, and the specific limitations of the Workers runtime you need to design around.
Why Cloudflare Workers for Claude API calls
Workers run on V8 isolates at Cloudflare's edge, which gives you near-zero cold start and global low latency between your users and your API layer. For a Claude-backed app — a chat widget, a support bot, a content tool — that means the network hop from "user clicks" to "request leaves the edge" is small. The actual latency to Anthropic's servers is unchanged, but you remove the extra hop of a centralized origin server.
The tradeoff: Workers don't run Node.js. There's no filesystem, no native Node http module, and the official Anthropic Node SDK relies on some Node-specific behavior that doesn't always work cleanly in the Workers runtime. The practical answer is to skip the SDK and call the REST API directly with fetch, which Workers fully support.
Project setup with Wrangler
Scaffold a new Worker with Wrangler, Cloudflare's CLI:
npm create cloudflare@latest claude-worker
cd claude-worker
Choose "Hello World Worker" when prompted, then add your API key as a secret rather than an environment variable in wrangler.toml. Secrets are encrypted at rest and not visible in your config file or git history:
npx wrangler secret put ANTHROPIC_API_KEY
You'll be prompted to paste the key, and Wrangler stores it bound to your Worker. In code it's accessible via the env object passed to your handler.
Calling Claude directly from a Worker
Here's a minimal Worker that proxies a chat request to Claude's Messages API:
export default {
async fetch(request, env) {
if (request.method !== "POST") {
return new Response("Use POST", { status: 405 });
}
const { messages } = await request.json();
const response = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"content-type": "application/json",
"x-api-key": env.ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify({
model: "claude-sonnet-4-5",
max_tokens: 1024,
messages,
}),
});
return new Response(response.body, {
status: response.status,
headers: { "content-type": "application/json" },
});
},
};
This works because fetch in Workers returns a standard Response whose .body is a ReadableStream you can pass straight through — no buffering required for non-streaming calls.
Streaming responses back to the browser
Streaming is where Workers shine, since you can pipe Claude's server-sent events straight to the client without holding the whole response in memory. Set "stream": true in the request body and forward the stream unchanged:
const upstream = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"content-type": "application/json",
"x-api-key": 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",
},
});
Because the Worker never buffers the full response, this pattern stays within Workers' CPU time limits even for long completions — the CPU limit applies to active compute, not to time spent waiting on I/O.
Common deployment pitfalls
CPU time limits. The free Workers plan caps CPU time per request at 10ms of active compute, and paid plans extend that but still don't account for idle wait time on streams the same way a traditional server would. Since most of a Claude request is spent waiting on the model, not computing locally, this is rarely the bottleneck — but it matters if you add heavy post-processing (large JSON parsing, regex on big payloads) after the response comes back.
No native SDK support. The Anthropic Node SDK depends on Node's http/https modules in some code paths. Calling the REST API directly via fetch, as shown above, avoids this entirely and is the recommended approach for Workers.
Secrets vs. vars. Don't put API keys in wrangler.toml under [vars] — that file is often committed to git. Use wrangler secret put for anything sensitive.
Request size and timeouts. Workers have their own request/response size limits separate from Anthropic's. For large document uploads or long multi-turn conversations, check both your Workers plan limits and Anthropic's payload constraints before assuming a failure is on the model side.
Managing keys and usage without building your own layer
If you're shipping multiple client apps or internal tools that all need Claude access from the edge, raw API keys get unwieldy fast — every Worker needs its own secret, and there's no shared view of usage or spend across them. SubToAPI turns your existing Claude access into a standard HTTPS API with scoped sub_live_... keys per application, so each Worker can hold its own key while usage, streaming, and tool-use metadata roll up into one dashboard.
Since it's a drop-in REST endpoint, the same fetch-based Worker pattern above works unchanged — just point the URL at https://api.subtoapi.app/v1/messages and swap the header to Authorization: Bearer $SUBTOAPI_KEY. Check the quickstart for the exact request shape, or streaming docs if you're building the SSE pipe-through described earlier.
Wrapping up
Deploying Claude on Cloudflare Workers comes down to three things: call the REST API with fetch instead of the Node SDK, pass streaming responses through without buffering, and keep your API key in a Wrangler secret. From there, the deployment itself is just npx wrangler deploy — Cloudflare handles the edge distribution automatically.
Questions
Do I need the Anthropic Node SDK to use Claude in a Worker? No. The SDK isn't fully compatible with the Workers runtime. Calling https://api.anthropic.com/v1/messages directly with fetch works reliably and keeps your bundle smaller.
Will streaming responses hit Workers' CPU time limits? Generally no, because CPU limits apply to active compute, not time spent waiting on I/O. Piping a stream through without processing it keeps CPU usage minimal even for long completions.
Where should I store my Claude API key in a Worker project? Use wrangler secret put to store it as an encrypted secret, never in wrangler.toml or committed source files. If you manage keys across multiple Workers, a scoped-key service like SubToAPI (see pricing) can simplify rotation and usage tracking.