# Webhooks and signatures

> The events Nibify sends, how to verify their Nibify-Signature, and what happens when your server does not answer.

When Kiwi Studio's invoice is answered, Nibify can tell your server instead of waiting to be asked. It sends a signed `POST` to an address of yours. This page covers the events, how to check that one really came from Nibify, and what happens when your server does not answer.

## Two kinds of address

- **A webhook endpoint** belongs to the project. Add it once in the [dashboard](https://app.nibify.app), under **Webhooks**, with the events it wants. It hears about every message of its environment: a `test` endpoint hears `sk_test_` traffic, a `live` one `sk_live_`.
- **A `callbackUrl`** belongs to one request, and you send it with that request. It hears only that request's end. See [Wait for the answer](https://nibify.app/docs/wait-for-the-answer).

Both receive the same signed events. A request with a `callbackUrl` and an endpoint subscribed to the same event gets two deliveries, one to each, with the same event `id`.

## The events

- `message.delivered`: the message reached one of the person's phones.
- `message.read`: the person opened it.
- `message.answered`: the person chose an action.
- `message.expired`: the request's `expiresAt` passed with no answer.
- `message.dismissed`: the person declined the request.
- `message.cancelled`: you withdrew it.

The last four end a request, and they are the only ones a `callbackUrl` receives. `delivered` and `read` go to endpoints only, and they come for notifications too.

Every event has the same envelope: an `id`, a `type`, a `createdAt` and `data`. This is the invoice, answered from the lock screen:

```json
{
  "id": "evt_…",
  "type": "message.answered",
  "createdAt": "2026-10-12T09:00:41Z",
  "data": {
    "requestId": "msg_…",
    "status": "answered",
    "environment": "test",
    "response": {
      "responseId": "res_…",
      "actionName": "file",
      "sourceComponentId": "notification",
      "context": {},
      "clientTimestamp": "2026-10-12T09:00:41Z",
      "answeredAt": "2026-10-12T09:00:41Z"
    }
  }
}
```

On `message.expired`, `message.dismissed` and `message.cancelled`, `response` is `null`. The receipts carry a `messageId` instead of a `requestId`, with `deliveredAt` and `readAt`.

## Verify the signature

Anyone can `POST` to your address. Every delivery carries a `Nibify-Signature` header made with your project's signing secret, which starts with `whsec_` and is on the **Webhooks** page of the [dashboard](https://app.nibify.app). One secret signs every delivery of the project, to endpoints and to `callbackUrl`s alike.

With the SDK, `webhooks.verify()` checks the signature and returns the event. It needs no API key:

```ts
import { webhooks } from '@nibify/sdk';

export async function POST(request: Request): Promise<Response> {
  let event;
  try {
    event = await webhooks.verify({
      body: await request.text(),
      signature: request.headers.get('nibify-signature'),
      secret: process.env.NIBIFY_WEBHOOK_SECRET!,
    });
  } catch {
    return new Response(null, { status: 401 });
  }

  if (event.type === 'message.answered') {
    event.data.response.actionName; // 'file'
  }
  return new Response(null, { status: 204 });
}
```

`body` is the request body as text, exactly as it arrived. Parse it and serialise it again, and the bytes change and the signature never matches. In Express, read it with `express.raw({ type: 'application/json' })` rather than `express.json()`.

When the signature does not hold, `verify()` throws a `NibifyError` whose `code` says why:

- `webhook_signature_mismatch`: the body is not the bytes that were sent, or the secret is not this project's.
- `webhook_timestamp_out_of_tolerance`: the signature is genuine but older than five minutes, or your clock is off. Pass `tolerance` in seconds to change the window.
- `webhook_signature_missing`, `webhook_signature_malformed`, `webhook_secret_missing`, `webhook_body_not_raw`, `webhook_payload_invalid`.

### Without the SDK

The header looks like this:

```http
Nibify-Signature: t=1760259641,v1=5f2b0c9e8d…
```

`t` is when the delivery was signed, in Unix seconds. `v1` is the hex HMAC-SHA256 of `t`, a dot and the raw body, keyed with the whole secret, `whsec_` included. The same check in Node, with no dependencies:

```ts
import { createHmac, timingSafeEqual } from 'node:crypto';

const TOLERANCE_SECONDS = 300;

export function verifySignature(
  body: string,
  header: string | null,
  secret: string,
): boolean {
  let t = Number.NaN;
  const digests: string[] = [];
  for (const part of (header ?? '').split(',')) {
    const [key, value = ''] = part.trim().split('=');
    if (key === 't') t = Number(value);
    if (key === 'v1') digests.push(value);
  }
  const age = Math.abs(Date.now() / 1000 - t);
  if (!Number.isInteger(t) || age > TOLERANCE_SECONDS) return false;

  const expected = createHmac('sha256', secret).update(`${t}.${body}`).digest();
  return digests.some((digest) => {
    const received = Buffer.from(digest, 'hex');
    return (
      received.length === expected.length &&
      timingSafeEqual(received, expected)
    );
  });
}
```

Three things matter in any language: sign the raw body, compare in constant time, and refuse a `t` older than five minutes, so that a recorded delivery cannot be replayed. The header may carry more than one `v1`; accept the delivery when any of them matches.

## Answer quickly, and expect repeats

Answer with any `2xx` within ten seconds; the body of your answer is ignored. Anything else counts as a failure, `4xx` included, and so does a redirect: Nibify does not follow it.

Delivery is at least once. A delivery whose answer was lost is sent again, and a request with both a `callbackUrl` and an endpoint is delivered to each. Make your handler safe to run twice: keep the event `id`, which is the same on every attempt, or the `responseId` of an answer, and skip what you have seen.

## Retries and abandoned deliveries

A failed delivery is retried with longer and longer waits, starting at ten seconds and never more than ten minutes apart, for about half an hour. Each attempt is signed afresh, so its `t` is recent.

When the last attempt fails, Nibify stops and tells you:

- the project's owner gets an email naming the event and the address;
- the dashboard lists it under **Webhooks**, as an abandoned delivery, with the number of attempts and the last error.

Nothing is lost with it. The answer is stored on the request, and `GET /v1/requests/{id}` still returns it: fix the endpoint, then read the requests the abandoned deliveries name. Deliveries are not replayed.

## Next

- [Idempotency and retries](https://nibify.app/docs/idempotency-and-retries): Send a request again without asking twice, what the SDK retries on its own, and what to do with a 429.
- [SDK](https://nibify.app/docs/sdk): Every method and type @nibify/sdk exports: the client, ask(), notify(), the Request handle, the lists, webhooks.verify() and the component constructors.
