# Webhooks

A webhook tells your server when something happens to a post or a channel, so you do not have to keep asking. Add one in
the Postaki app (Settings, "For developers", "API keys and webhooks", "Webhooks"), or through the API.

Webhooks belong to the workspace owner, as in the app. Listing, creating and deleting them through the API needs a key
or an app connected by the owner (anyone else gets `403 forbidden`), because an endpoint receives every event of the
workspace and its address can carry a secret.

## Register

```bash
curl https://app.postaki.io/api/v1/webhooks \
  -H "Authorization: Bearer $POSTAKI_KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{"url": "https://example.com/postaki-hook", "events": ["post.awaiting_approval", "variant.sent"]}'
```

`events` is optional: leave it out for all events. The answer holds `id`, `url`, `events` and a **`secret`** that signs
every delivery. It is shown only in this answer. Store it now.

If you lost the secret, delete the webhook (`DELETE /webhooks/{id}`) and recreate it. A replay of the same
`POST /webhooks` with the same `Idempotency-Key` returns `secret: null`, because the secret is never kept for replays.
There is no way to read it again.

The address must be https on port 443 and a public host. It is checked when you register it and again at every
delivery. Redirects are not followed. A workspace can have 10 active webhooks.

## Events

| Event | When |
|---|---|
| `post.awaiting_approval` | A post's copies were sent for approval (one event per post, naming its copies) |
| `post.approved` | A person approved it and it is scheduled |
| `variant.sent` | A copy is published on its channel |
| `variant.failed` | A copy could not be published |
| `variant.needs_reconnect` | A copy could not go out because the channel's access ended |
| `variant.deleted` | A copy was taken down from its channel |
| `channel.needs_reconnect` | A channel's access ended: a person must reconnect it |

You can also send a test event (`ping`) from the webhook's page in the app.

## The delivery

Each event is a `POST` with a JSON body and these headers:

| Header | Value |
|---|---|
| `Postaki-Signature` | `t=<unix seconds>,v1=<hex>`, see below |
| `Postaki-Event` | The event name |
| `Postaki-Delivery-Id` | One id for this event to this endpoint. The same on every retry: use it to de-duplicate |
| `Postaki-Event-Id` | The id of the event itself (also in the body); the same for every endpoint |

```json
{
  "id": "5f0e4a1e-0a52-4b57-8b0e-3c0d9a3a8f10",
  "type": "post.awaiting_approval",
  "created_at": "2026-11-23T08:15:00Z",
  "delivery_id": "0d3b3b4e-3a6f-4a0f-9a43-1c2c6b9d7f21",
  "workspace_id": 7,
  "data": {
    "post_id": 981, "brand_id": 12, "status": "awaiting_approval",
    "scheduled_at": "2026-12-01T07:00:00Z",
    "post_url": "https://app.postaki.io/app/...",
    "variant_ids": [4411], "channel_ids": [345]
  }
}
```

Answer with any 2xx within 10 seconds. Anything else, a timeout or a failure to connect counts as a failure.

## Verify the signature

`v1` is the HMAC-SHA256, keyed with your secret, of the text `<t>.<raw request body>`, as lowercase hex. Compute it over
the raw bytes you received, compare in constant time, and reject a `t` that is too old (for example more than five
minutes) so a captured request cannot be replayed.

```python
import hashlib, hmac, time

def verify(secret: str, header: str, body: bytes, tolerance=300) -> bool:
    parts = dict(p.split("=", 1) for p in header.split(","))
    t, v1 = parts["t"], parts["v1"]
    if abs(time.time() - int(t)) > tolerance:
        return False
    expected = hmac.new(secret.encode(), f"{t}.".encode() + body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, v1)
```

## Retries and switching off

A failed delivery is retried after 1 minute, 5 minutes, 30 minutes, 2 hours and 12 hours: six tries in all. Because a
delivery can arrive more than once, make your handler idempotent and de-duplicate on `Postaki-Delivery-Id`.

An endpoint that keeps failing for 3 days is switched off and the workspace owners are told. Turn it back on from the
webhook's page in the app once it works again. Delivery records are kept for 30 days.
