Prompt
How do I debug a failing webhook on Cloudflare Workers?
Latest observation
To debug a failing webhook on Cloudflare Workers, work through it in layers: request receipt, Worker logic, upstream/downstream call, and provider verification/retries.
1) Check whether the request reaches the Worker
- In the Cloudflare dashboard, open your Worker and look at Logs / Tail Workers.
- Use:
This shows live logs, errors, andwrangler tailconsole.log()output. - Add a very early log:
export default { async fetch(request, env, ctx) { console.log("Webhook hit", request.method, request.url); return new Response("ok"); } }
If you don’t see logs, the webhook may not be reaching the route, DNS, or Worker binding.
2) Verify the route and HTTP method
Common issues:
- Worker route doesn’t match the webhook URL
- Provider sends
POST, but your code only handlesGET - Trailing slash or subdomain mismatch
Example:
if (request.method !== "POST") {
return new Response("Method Not Allowed", { status: 405 });
}
3) Log headers and body safely
Many webhooks fail due to malformed JSON or signature checks.
export default {
async fetch(request) {
console.log("Headers:", Object.fromEntries(request.headers));
const rawBody = await request.text();
console.log("Raw body:", rawBody);
try {
const data = JSON.parse(rawBody);
console.log("Parsed JSON:", data);
} catch (err) {
console.error("Invalid JSON:", err);
return new Response("Bad Request", { status: 400 });
}
return new Response("ok");
}
}
Notes:
- Use
request.text()first if you need the raw payload for signature verification. - Don’t call
request.json()first if you also need the raw body; the body can only be consumed once.
4) Check signature verification
If your webhook provider signs requests, failures often come from:
- wrong secret
- using parsed JSON instead of raw body
- header name mismatch
- clock skew for timestamp-based signatures
- different canonicalization rules
Pattern:
const rawBody = await request.text();
const signature = request.headers.get("x-signature");
Be sure to match the provider’s exact verification algorithm.
5) Inspect runtime errors
Use try/catch around your main logic:
try {
// webhook logic
} catch (err) {
console.error("Webhook handler failed:", err);
return new Response("Internal Error", { status: 500 });
}
In wrangler tail, look for:
- stack traces
- uncaught promise rejections
- serialization errors
TypeError: Body has already been used
6) Test locally
Run:
wrangler dev
Then send a test request:
curl -X POST http://127.0.0.1:8787/ \
-H "Content-Type: application/json" \
-d '{"hello":"world"}'
For signature-based webhooks, test with the exact provider payload if possible.
7) Return the right response quickly
Most webhook providers expect a fast 2xx response. If your Worker does slow work:
- acknowledge immediately
- use
ctx.waitUntil()for async follow-up work
export default {
async fetch(request, env, ctx) {
ctx.waitUntil(doAsyncWork());
return new Response("ok", { status: 200 });
}
}
8) Check external calls from the Worker
If your webhook forwards data to another service:
- log the upstream response status/body
- confirm outbound fetch works
- watch for CORS confusion (usually not relevant server-to-server)
- confirm env vars and secrets are set correctly
const res = await fetch("https://example.com/api", { method: "POST", body: rawBody });
console.log("Upstream status:", res.status);
console.log("Upstream response:", await res.text());
9) Use structured logs
Prefer logs like:
console.log(JSON.stringify({
event: "webhook_received",
requestId: crypto.randomUUID(),
method: request.method
}));
This makes searching much easier in wrangler tail or dashboard logs.
10) Common Cloudflare-specific gotchas
- Route not deployed or wrong environment
- Worker subrequest limits if it makes many fetches
- CPU time limits causing timeouts
waitUntilnot used for background work- Secrets not present in the environment:
wrangler secret put WEBHOOK_SECRET - Different staging vs production URL in the webhook provider
Minimal debugging template
export default {
async fetch(request, env, ctx) {
console.log("Incoming webhook:", request.method, request.url);
try {
const raw = await request.text();
console.log("Raw body:", raw);
// verify signature here using raw body
ctx.waitUntil((async () => {
// background processing
})());
return new Response("ok", { status: 200 });
} catch (err) {
console.error("Webhook error:", err);
return new Response("error", { status: 500 });
}
}
}
If you want, I can also give you:
- a step-by-step checklist for debugging a specific provider like Stripe/GitHub/Slack, or
- a signature verification example for Workers.