Claude API Webhook Event Handling Tutorial
If you're searching for "Claude API webhook event handling," you're probably trying to figure out how to get notified when a Claude request finishes, fails, or produces a tool call — without polling. The short answer: Claude's API itself doesn't push webhooks to you. It's a request/response API (with optional streaming), not an event-driven one like Stripe or GitHub. So "webhook event handling" in the Claude context means something specific: you build a thin webhook layer around your Claude calls, and that layer is what you need to design and secure correctly.
This tutorial covers the actual pattern developers use in production: queuing Claude requests, running them asynchronously, and firing your own webhook events when something interesting happens — completion, tool use, error, or timeout. It also covers signature verification and retry handling, since that's where most webhook implementations break.
Why Claude API Has No Native Webhooks
Claude's Messages endpoint is synchronous or streamed over SSE — you open a connection, you get tokens back, the connection closes. There's no background job ID you can subscribe to later, and no built-in "notify me when this completes" mechanism. That's fine for short requests, but it becomes a problem when:
- You're summarizing long documents and the call takes 20–40 seconds
- You're running a batch of requests and don't want to hold open connections
- You need to trigger downstream systems (CRM update, Slack message, database write) only after Claude responds
- You're orchestrating tool use and need to react to each tool call as a discrete event
In all of these cases, you need an event layer on top of the API call, not inside it.
The Core Pattern
The standard architecture looks like this:
- Your app receives a user request and enqueues a job
- A worker picks up the job and calls Claude (streaming or non-streaming)
- As the response progresses, the worker emits internal events (
message.started,tool.called,message.completed,message.failed) - Those events are turned into outbound webhooks to whatever system needs to know
This decouples "talking to Claude" from "reacting to what Claude said," which makes the system testable and lets you add new event consumers without touching the Claude call itself.
Step 1: Define Your Event Schema
Keep it small and versioned from day one:
{
"event": "message.completed",
"version": 1,
"request_id": "req_8a21c9",
"timestamp": "2024-05-14T10:03:21Z",
"data": {
"stop_reason": "end_turn",
"usage": { "input_tokens": 512, "output_tokens": 310 }
}
}
Typical event types to support:
message.startedmessage.delta(optional, only if you want chunk-level webhooks instead of streaming)tool.calledtool.resultmessage.completedmessage.failed
Step 2: Build the Webhook Receiver
A minimal Express receiver that verifies a signature and processes idempotently:
import crypto from "crypto";
import express from "express";
const app = express();
app.use(express.json());
app.post("/webhooks/claude-events", (req, res) => {
const signature = req.headers["x-signature"];
const expected = crypto
.createHmac("sha256", process.env.WEBHOOK_SECRET)
.update(JSON.stringify(req.body))
.digest("hex");
if (signature !== expected) {
return res.status(401).send("invalid signature");
}
const { event, request_id, data } = req.body;
// Idempotency check — store processed request_ids
if (hasProcessed(request_id, event)) {
return res.status(200).send("already processed");
}
switch (event) {
case "message.completed":
handleCompletion(request_id, data);
break;
case "tool.called":
handleToolCall(request_id, data);
break;
case "message.failed":
handleFailure(request_id, data);
break;
}
markProcessed(request_id, event);
res.status(200).send("ok");
});
The two non-negotiable pieces here are signature verification and idempotency. Webhooks get retried — by design, since the sender assumes network failures happen — so your receiver has to tolerate duplicate deliveries without double-processing.
Step 3: Emit Events From the Worker
This is the part that replaces the "native webhook" Claude doesn't have. Your worker calls Claude, then dispatches events based on what comes back:
async function processJob(job) {
await emitEvent("message.started", job.id, {});
try {
const response = await callClaude(job.payload);
if (response.stop_reason === "tool_use") {
await emitEvent("tool.called", job.id, { tool: response.tool_name });
}
await emitEvent("message.completed", job.id, {
usage: response.usage,
stop_reason: response.stop_reason,
});
} catch (err) {
await emitEvent("message.failed", job.id, { error: err.message });
}
}
callClaude() here is your actual API call. If you're using SubToAPI, this is a normal call to the Messages endpoint with your sub_live_... key — see /docs/messages for the request format and /docs/tools if you're handling tool-use events specifically, since tool call structure matters for how you shape tool.called payloads.
Step 4: Handle Retries Properly
Outbound webhook delivery should retry on non-2xx responses with exponential backoff, and cap total attempts (5–8 is typical). Log every delivery attempt with status code and latency — this is the first place you'll look when a downstream consumer says "we never got the event."
Streaming vs Webhook Events — Pick the Right One
If your use case is a live UI (chat interface, progress indicator), use Claude's native streaming instead of building webhook chunking — it's simpler and lower latency. SubToAPI exposes the same streaming interface over SSE; see /docs/streaming for details. Reserve webhook events for cases where the consumer of the result isn't a live connection — background jobs, integrations, notifications, multi-step pipelines.
If you're building this on top of SubToAPI, the practical setup is: SubToAPI handles authentication, streaming, tool use, and per-request usage metadata (see /docs/messages); your own worker and webhook dispatcher handle turning those responses into events for the rest of your stack. You can get a key and test this end to end from /signup.
questions
Does Claude's API support native webhooks? No. Claude's API is request/response (or streamed via SSE). If you need event-driven notifications, you build a webhook layer around your own worker that calls Claude and emits events based on the result.
How do I avoid processing the same webhook event twice? Store a unique identifier per event (request ID + event type) and check it before processing. Webhook senders retry on failure, so duplicate deliveries are expected, not a bug.
Should I use webhooks or streaming for a chat UI? Use streaming. Webhooks add latency and complexity for live interfaces. Reserve webhook events for background jobs, integrations, and systems that aren't holding an open connection — see /docs/streaming for the native streaming approach.