# Webhooks

Webhooks tell your server when something finishes, so you don't have to poll.

## Set up an endpoint

In the developer portal open **Webhooks → Add endpoint**, enter an `https://` URL and pick
the events. The signing secret (`whsec_…`) is shown **once**; store it with your other
secrets. You can rotate it at any time (the old secret stops working at once), send a test
event, see the delivery log, disable or re-enable it, and delete the endpoint. Up to 10
endpoints per account.

URLs must be public `https://` addresses; private, local and reserved addresses are
rejected.

## Events

| Event | When | `data.object` |
|---|---|---|
| `job.succeeded` | A job finished. | The Job, with `result`. |
| `job.failed` | A job failed (refunded). | The Job, with `error`. |
| `job.canceled` | A job was canceled (refunded). | The Job. |
| `avatar.ready` | A custom avatar created with an API key is ready to use. | `{ id, object: "avatar", name, status }` |
| `avatar.failed` | A custom avatar created with an API key could not be prepared. | `{ id, object: "avatar", name, status, error }` |
| `meeting.started` | The interpreter was let into the meeting; billing started. | The Meeting. |
| `meeting.ended` | The meeting ended (stopped, call ended, out of funds or failed). | The Meeting, with `billed_seconds` and `cost_usd`. |

## Delivery

Each event is a `POST` with a JSON body:

```json
{
  "id": "evt_job_8c1d2e3f4a5b6c7d8e9f0a1b_succeeded",
  "object": "event",
  "type": "job.succeeded",
  "created": 1795435204,
  "data": { "object": { "id": "job_8c1d2e3f4a5b6c7d8e9f0a1b", "object": "job", "status": "succeeded", "...": "..." } }
}
```

Headers: `Talksign-Signature`, `Talksign-Event-Id`, `Content-Type: application/json`.

- Answer any **2xx** within **10 seconds** to acknowledge. Do slow work after you answer.
- Anything else (or a timeout) is retried after **1 minute, 5 minutes, 30 minutes, 2 hours
  and 6 hours** (6 attempts in total).
- The same event can arrive more than once. Use `id` to ignore repeats.
- Events can arrive out of order; use the object's `status`, or fetch it, if order matters.
- After 15 failed attempts in a row the endpoint is **disabled** and the portal shows a
  notice. Fix it, then re-enable it in the portal.

## Verify the signature

`Talksign-Signature: t=<unix seconds>,v1=<hex>` where `v1` is the HMAC-SHA256 of
`<t>.<raw request body>` keyed with your endpoint secret. Always verify against the **raw**
body (before JSON parsing) and reject old timestamps to stop replays.

```js
import crypto from 'node:crypto';
import express from 'express';

const app = express();
const SECRET = process.env.TALKSIGN_WEBHOOK_SECRET; // whsec_…

function verifyTalksign(header, rawBody, secret, toleranceSec = 300) {
  const parts = Object.fromEntries(String(header || '').split(',').map((kv) => kv.split('=')));
  const t = Number(parts.t);
  if (!t || !parts.v1 || Math.abs(Date.now() / 1000 - t) > toleranceSec) return false;
  const expected = crypto.createHmac('sha256', secret).update(`${t}.${rawBody}`).digest('hex');
  const a = Buffer.from(expected, 'hex');
  const b = Buffer.from(parts.v1, 'hex');
  return a.length === b.length && crypto.timingSafeEqual(a, b);
}

app.post('/talksign/webhooks', express.raw({ type: 'application/json' }), (req, res) => {
  const raw = req.body.toString('utf8');
  if (!verifyTalksign(req.get('Talksign-Signature'), raw, SECRET)) return res.status(400).end();
  const event = JSON.parse(raw);
  res.status(200).end(); // acknowledge first
  if (event.type === 'job.succeeded') {
    // event.data.object.result.download_url (valid 1 hour; video kept 7 days)
  }
});

app.listen(3000);
```
