cloudflare-to-claude-fix
v1.0.8
Published
<p align="center"> <img width="400px" src="https://i.imgur.com/RQP3lma.png" /> </p>
Downloads
1,087
Readme
A Cloudflare Workers Queue consumer that fires a Claude Code routine whenever a Workers build fails. Required Workers Paid and Claude Pro.
Setup
1. Clone / copy this project
git clone <your-repo>
cd cloudflare-to-claude-fix
npm install2. Create the Cloudflare Queue
# Main queue
wrangler queues create workers-build-events
# Dead-letter queue (catches messages that fail all 3 retries)
wrangler queues create workers-build-events-dlq3. Enable Event Subscriptions on your target Worker
In the Cloudflare dashboard:
- Open Workers & Pages → select the Worker you want to monitor
- Go to Settings → Event Subscriptions
- Set the queue to
workers-build-events - Enable: Build started, Build succeeded, Build failed, Build cancelled
This Worker only acts on status === "failed" events; the rest are acknowledged and discarded.
4. Create a Claude Code routine
- Go to claude.ai/code/routines
- Click New routine
- Configure:
- Prompt: e.g. "A Cloudflare Workers build has failed. The build ID, branch, commit, author and error log are below. Investigate the error, identify the root cause, and push a fix to the branch. Summarise what you changed."
- Repository: the repo that this Worker is deployed from
- Under Select a trigger → Add another trigger → choose API
- Click Generate token — copy the token (shown once)
- Copy the full fire URL shown in the modal (format:
https://api.anthropic.com/v1/claude_code/routines/trig_<id>/fire)
5. Store secrets in Wrangler
# Required
wrangler secret put ROUTINE_FIRE_URL
# paste: https://api.anthropic.com/v1/claude_code/routines/trig_<id>/fire
wrangler secret put ROUTINE_FIRE_TOKEN
# paste: sk-ant-oat01-...
# Optional — post session link to Slack or Discord
wrangler secret put NOTIFY_WEBHOOK_URL
# paste: https://hooks.slack.com/services/... (Slack)
# or: https://discord.com/api/webhooks/... (Discord)Never commit secrets to source control or paste them in plaintext anywhere.
6. Deploy
wrangler deployHow it works
- Cloudflare Builds publishes a
BuildEventmessage toworkers-build-eventswhen a build status changes. - This consumer Worker receives the batch. Non-failure messages are
ack()-ed immediately. - For
status === "failed"messages the Worker:- Formats the
build_id,worker_name,branch,commit_hash,author,timestamp, anderror_messagesinto a single plaintext block (≤ 65,536 chars). - POSTs that block to the Claude Code routine
/fireendpoint. - If
NOTIFY_WEBHOOK_URLis set, posts the resultingclaude_code_session_urlto Slack/Discord so your team can watch the live debugging session.
- Formats the
- If the fire request fails, the message is
retry()-ed up to 3 times (configured inwrangler.toml). After 3 failures the message lands inworkers-build-events-dlq.
Claude Code routine /fire API quick-reference
POST https://api.anthropic.com/v1/claude_code/routines/{routine_id}/fire
Authorization: Bearer sk-ant-oat01-...
anthropic-version: 2023-06-01
anthropic-beta: experimental-cc-routine-2026-04-01
Content-Type: application/json
{ "text": "<up to 65,536 chars of context>" }Response:
{
"type": "routine_fire",
"claude_code_session_id": "session_01...",
"claude_code_session_url": "https://claude.ai/code/session_01..."
}| Status | Cause |
| ------- | -------------------------------------------------------------- |
| 400 | Missing beta header, text > 65 536 chars, or routine is paused |
| 401 | Wrong or missing bearer token |
| 403 | Account doesn't have Claude Code on the web |
| 404 | Routine ID not found |
| 429 | Daily run allowance exhausted |
Revoking / rotating the routine token
- Open claude.ai/code/routines → edit the routine
- Click the API trigger → Generate token (this immediately revokes the old one)
- Update the secret:
wrangler secret put ROUTINE_FIRE_TOKEN
Local testing
Send a synthetic failed-build message to the queue:
wrangler queues publish workers-build-events \
--message '{
"build_id": "build_test001",
"status": "failed",
"worker_name": "my-api",
"branch": "feat/new-endpoint",
"commit_hash": "abc1234",
"author": "[email protected]",
"error_messages": ["Error: Cannot find module '\''./utils'\''", " at Object.<anonymous> (src/index.ts:3:1)"],
"timestamp": "2026-05-01T19:00:00Z"
}'Then tail logs to verify:
wrangler tail cloudflare-to-claude-fix