# Webhooks

Receive signed message and contact events in your application.

Qwetty sends an HTTP POST to your endpoint when a subscribed event occurs. Use webhooks to sync your application without polling.

## Set up an endpoint

1. Open **Settings → Webhooks** and choose **Create endpoint**.
2. Enter a name, optional description, and public HTTPS URL.
3. Select events and choose all channels or specific channels.
4. Save the endpoint and copy the signing secret when it is shown.
5. Send a test delivery and check its result in delivery history.

Contact events require all-channel access. Private or reserved destinations, URL credentials, and redirects are not accepted. Keep your signing secret on your server.

## Events

| Event | Meaning |
| --- | --- |
| `message.incoming` | A message was received from a contact |
| `message.outgoing` | An outgoing message was recorded; this is not a recipient delivery confirmation |
| `message.updated` | A message's delivery status, text, reactions or deletion changed. Payload version `2026-10-20` only |
| `chat.updated` | A conversation's status, assignee, handler (bot or agent), read state, name or picture changed. Payload version `2026-10-20` only |
| `contact.created` | A contact was created |
| `contact.updated` | A contact was updated |
| `contact.tagged` | A contact's tags changed |
| `webhook.test` | A test delivery was requested |

## Payload and headers

New endpoints use payload version `2026-10-20`. `data.object` is the same object the API answers with for that resource, as it stands when the event is sent — so one parser handles both. Message events also carry the conversation as `data.chat`, and update events say what changed in `data.changes`:

```json
{
  "id": "event-id",
  "type": "message.updated",
  "occurredAt": "2026-10-20T10:30:00.000Z",
  "version": "2026-10-20",
  "data": {
    "object": {
      "object": "message",
      "id": "message-id",
      "chatId": "chat-id",
      "direction": "outgoing",
      "type": "text",
      "text": "Your order is confirmed.",
      "status": "read",
      "clientReference": "order-123-confirmation",
      "updatedAt": "2026-10-20T10:30:00.000Z"
    },
    "chat": {"object": "chat", "id": "chat-id", "status": "open", "unreadCount": 0},
    "changes": ["status"]
  }
}
```

The example shows a subset of fields; the [API reference](/api) lists them all, per event. Events can arrive out of order: compare `updatedAt` before overwriting your copy. Media and picture URLs are signed and expire at `expiresAt`; retrieve the message through the API for fresh ones.

Endpoints on payload version `2026-09-23` keep receiving the snapshot recorded with each event (`{"chat": …, "message": …}` or the contact's fields) and cannot subscribe to `message.updated` or `chat.updated`.

| Header | Meaning |
| --- | --- |
| `X-Qwetty-Event` | Event type |
| `X-Qwetty-Event-Id` | Stable event ID; use this to deduplicate processing |
| `X-Qwetty-Delivery` | Delivery ID |
| `X-Qwetty-Signature` | Timestamp and HMAC signatures |

## Verify the signature

Keep the raw request bytes before parsing JSON. The signature header has the form `t=<unix-seconds>,v1=<hex-digest>`. Verify HMAC-SHA256 of `timestamp + "." + rawBody`, compare in constant time, and reject timestamps more than five minutes from your server's clock.

The following Node.js example accepts a `Buffer` containing the raw body:

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

export function verifyWebhook(rawBody, header, secret, now = Date.now()) {
  if (!Buffer.isBuffer(rawBody) || typeof header !== 'string') return false;
  const parts = header.split(',');
  const timestamps = parts.filter(part => part.startsWith('t='));
  if (timestamps.length !== 1) return false;
  const timestamp = timestamps[0].slice(2);
  if (!/^\d+$/.test(timestamp)) return false;
  const seconds = Number(timestamp);
  if (!Number.isSafeInteger(seconds) || Math.abs(now / 1000 - seconds) > 300) return false;
  const expected = createHmac('sha256', secret)
    .update(`${timestamp}.`)
    .update(rawBody)
    .digest();
  return parts.filter(part => part.startsWith('v1=')).some(part => {
    const hex = part.slice(3);
    return /^[a-f0-9]{64}$/i.test(hex) &&
      timingSafeEqual(Buffer.from(hex, 'hex'), expected);
  });
}
```

Reject a request if verification fails. After verification, persist the event or enqueue it durably before responding with `2xx`. Deduplicate by event ID so repeated deliveries cannot repeat your business actions.

## Retries and history

Delivery is at least once; events can arrive out of order. Qwetty permits one concurrent request per endpoint, paced at up to five requests/second, with a 10-second timeout.

Any `2xx` acknowledges delivery. Network errors, `408`, `425`, `429`, and `5xx` are retried. Other responses become terminal failures. There are eight total attempts: immediately, then approximately 1 minute, 5 minutes, 30 minutes, 2 hours, 6 hours, 12 hours, and 24 hours after initial scheduling. Timing includes jitter; valid longer `Retry-After` delays are honored within the retention window.

Completed and terminal payloads and attempts are retained for 30 days. Inspect failures and request redelivery from delivery history or the API. Redelivery preserves the event ID and payload, so retain deduplication records for your replay horizon. A failed delivery does not automatically disable the endpoint.

## Configuration and rotation

Disabling an endpoint suppresses pending deliveries and stops new deliveries. Re-enabling does not automatically release suppressed work. Changing its URL, subscriptions, channel filter, or payload version suppresses pending deliveries for the previous configuration.

Rotation reveals a new secret once. For 24 hours, Qwetty includes signatures for both the previous and new secrets. Update your receiver during this window; accept a valid matching signature from the header.

Existing endpoints marked `legacy` retain `{ event, timestamp, data }` and, when configured with a secret, a plain hexadecimal HMAC-SHA256 signature over the raw body. Their signature has no timestamp prefix. Update your receiver before upgrading the endpoint's payload version; the verifier above is for versions `2026-09-23` and `2026-10-20`, which are signed the same way.
