# Webhooks (https://docs.capy.ai/api-reference/webhooks)

> Get an HTTP POST at your own URL when a thread needs you, when a thread finishes, or when a Captain replies, signed with a secret you verify

A webhook endpoint is a URL your organization registers once. Capy posts a signed JSON request to it whenever a thread in the organization needs a person, finishes with a final reply, or a Captain replies to its person. Use it instead of holding a [stream](https://docs.capy.ai/api-reference/streaming) open or polling thread status.

```bash
curl -X POST "https://api.capy.ai/api/v1/webhooks" \
  -H "Authorization: Bearer $CAPY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/capy/webhook", "description": "Ops alerts"}'
```

The response carries the endpoint and its `secret`, shown once. Store it: every later read omits it, and `POST /webhooks/{id}/rotate` is the only way to get a new one.

Creating, changing, rotating, and deleting endpoints needs an API key with organization admin access. Reads need read access. The URL must be `https` and resolve to a public address; a URL that does not answers `400 capy/InvalidRequest`.

## Events

Every endpoint receives all three events for every thread in the organization. Subagents never fire an event, and neither does an archived thread.

| Event | When | Extra data |
| --- | --- | --- |
| `thread.needs_you` | A thread opens an ask for a person: a reply marked needs-you, a clarifying question, a request for environment variables or an MCP server, a credits ask, a desktop or terminal request. Fires once per ask. | `request: { kind, ask }`, the kind of request and its one-line ask (`null` when the request carries none). |
| `thread.finished` | A thread comes to rest after a final reply. A thread that rests on a needs-you fires `thread.needs_you` instead, and a thread waiting on a subagent, a background command, or a timer fires nothing until that wait ends. | `message`, the text of the final reply, cut to 4,000 characters. |
| `captain.replied` | A Captain delivers a message to its person, whether it is an interim update, a final answer, or a needs-you. | `state` (`working`, `final`, or `needs_you`), `message` (the reply, cut to 4,000 characters), and `ask` (`null` unless the state is `needs_you`). |

## The request

```http
POST /capy/webhook HTTP/1.1
Content-Type: application/json
User-Agent: Capy-Webhooks/1
Capy-Webhook-Id: whd_01JB4X9Q2M7N8P3R5T6V7W8X9Y
Capy-Event: thread.finished
Capy-Timestamp: 1761883929
Capy-Signature: v1=5f1c7f9f2b4a...

{
  "id": "whd_01JB4X9Q2M7N8P3R5T6V7W8X9Y",
  "type": "thread.finished",
  "createdAt": "2026-10-31T04:12:09.318Z",
  "data": {
    "thread": {
      "id": "jam_01JB4W...",
      "code": "CAPY-19",
      "title": "Outbound webhooks: spec and build",
      "projectId": "proj_...",
      "status": "idle",
      "url": "https://capy.ai/thread/jam_01JB4W..."
    },
    "message": "PR is up: https://github.com/..."
  }
}
```

- `id` is the delivery id. A retry carries the same id, so dedupe on it.
- `createdAt` is when the event was recorded; `Capy-Timestamp` is when this attempt was sent.
- `data.thread` is the thread's id, code (`null` for a thread without a project), title (`null` while untitled), project id, status at the time, and web URL.

Answer with any `2xx` status within 10 seconds. Read the body, then do the work after you respond; Capy only needs the status.

## Verifying the signature

`Capy-Signature` is `v1=` followed by the hex HMAC-SHA256 of `${Capy-Timestamp}.${raw body}` under your endpoint's secret. Compute the same value over the exact body bytes you received, compare in constant time, and refuse a timestamp more than five minutes from your clock.

```js
import { createHmac, timingSafeEqual } from "node:crypto";

export const verify = (secret, headers, rawBody) => {
  const timestamp = headers["capy-timestamp"];
  const signature = headers["capy-signature"];
  if (timestamp === undefined || signature === undefined) return false;
  if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false;
  const expected = `v1=${createHmac("sha256", secret).update(`${timestamp}.${rawBody}`).digest("hex")}`;
  return expected.length === signature.length && timingSafeEqual(Buffer.from(expected), Buffer.from(signature));
};
```

Verify before you parse the JSON, and sign over the raw bytes, not a re-serialized object: frameworks that parse the body for you often change key order or whitespace.

To rotate, call `POST /webhooks/{id}/rotate`, update your receiver with the new `secret`, and redeploy. Deliveries that fail in between are retried with the new secret on the schedule below.

## Retries

Delivery is at least once. A `2xx` response marks the delivery done. Any other response, a timeout, or a connection failure schedules a retry: after 1 minute, then 5 minutes, 30 minutes, 2 hours, and 8 hours. After six attempts the delivery is marked `failed` and stays in the delivery list for you to inspect. Order is not promised: two events of one thread can arrive in either order, and a retry arrives after newer events.

`GET /webhooks/{id}/deliveries` lists the newest deliveries with each one's event, thread, state, attempt count, last HTTP status, last error, and next attempt time. It never returns the body or the secret.

## Pausing and removing

`PATCH /webhooks/{id}` with `{"enabled": false}` pauses an endpoint: events during the pause are not queued. `{"enabled": true}` resumes it. `DELETE /webhooks/{id}` removes the endpoint and its delivery history.