# Webhooks

A job create can carry `webhook_url`. When the job ends, its final object is sent there as a `POST` with a JSON body. The create response returns `webhook_secret` once, so store it to verify deliveries. Jobs and their webhooks are planned.

## Headers

| Header | Meaning |
| --- | --- |
| `X-Web-Webhook-Id` | ID of the delivery. Repeated deliveries of one event keep it, so you can skip duplicates. |
| `X-Web-Webhook-Timestamp` | Unix seconds when the delivery was signed. |
| `X-Web-Webhook-Signature` | `v1=` followed by the hex HMAC-SHA256 of `timestamp + "." + body`, keyed with the `webhook_secret`. |

## Verifying a delivery

Compute the HMAC over the raw body as received, before parsing it, and compare in constant time. Reject deliveries whose timestamp is more than five minutes old.

Python:

```python
import hashlib
import hmac
import time


def verify(secret: str, headers: dict, body: bytes) -> bool:
    timestamp = headers["X-Web-Webhook-Timestamp"]
    if abs(time.time() - int(timestamp)) > 300:
        return False
    expected = hmac.new(secret.encode(), timestamp.encode() + b"." + body, hashlib.sha256).hexdigest()
    return hmac.compare_digest("v1=" + expected, headers["X-Web-Webhook-Signature"])
```

TypeScript:

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

export function verify(secret: string, headers: Headers, body: string): boolean {
  const timestamp = headers.get("X-Web-Webhook-Timestamp") ?? "";
  if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false;
  const expected = "v1=" + createHmac("sha256", secret).update(timestamp + "." + body).digest("hex");
  const given = headers.get("X-Web-Webhook-Signature") ?? "";
  return given.length === expected.length && timingSafeEqual(Buffer.from(given), Buffer.from(expected));
}
```

## Delivery

Answer with any `2xx` within 10 seconds. Other answers and timeouts are retried with backoff for up to 24 hours. Delivery is best effort. The job object can always be read with its `GET` path, so do not depend on the webhook alone.
