# Webhooks (/docs/webhooks)

When a call ends we send a `POST` with the result to the `webhook_url` of the call:

```json
{
  "id": "call_x9f2k",
  "status": "answered",
  "option_path": [1],
  "option_label": "Confirm",
  "metadata": { "booking_id": "bk_1042" }
}
```

The body is exactly what `GET /v1/calls/{id}` returns.

## Fields [#fields]

| Field | Type | Description |
|---|---|---|
| `id` | string | Call ID returned when you created the call. |
| `status` | string | `answered`, `not_answered` or `failed` (see below). |
| `option_path` | integer[] | Keys leading to the chosen option. `[2]` = option 2 of the main menu; `[3, 1]` = option 1 of the submenu under option 3. Empty if nothing was chosen. |
| `option_label` | string \| null | `label` of the chosen option, or `null`. |
| `metadata` | object | The `metadata` you sent, unchanged. Use it to find your own record. |

## Statuses [#statuses]

| `status` | Meaning | Counts towards your plan |
|---|---|---|
| `queued` | Waiting, or between retries | — |
| `in_progress` | Ringing or on the call | — |
| `answered` | The person picked up (with or without choosing an option) | Yes |
| `not_answered` | No answer, busy or voicemail, after all retries | No |
| `failed` | Invalid number or technical error | No |

The webhook is sent once the call reaches a final status: `answered`, `not_answered` or `failed`.

## Verify the signature [#verify-the-signature]

Every webhook is a `POST` with `Content-Type: application/json` and a `CallHuman-Signature` header: the **HMAC-SHA256 of the raw request body, as lowercase hex with no prefix**, using your project's **webhook secret** (dashboard → **Settings**). Compute it over the raw bytes you received — before parsing the JSON — and compare in constant time.

```js tab="Node.js (Express)"
import crypto from "node:crypto";
import express from "express";

const app = express();
const secret = process.env.CALLHUMAN_WEBHOOK_SECRET;

// Use the raw body: the signature is computed over the exact bytes we sent.
app.post("/hooks/callhuman", express.raw({ type: "application/json" }), (req, res) => {
  const expected = crypto.createHmac("sha256", secret).update(req.body).digest("hex");
  const received = req.get("CallHuman-Signature") ?? "";

  const valid =
    received.length === expected.length &&
    crypto.timingSafeEqual(Buffer.from(received), Buffer.from(expected));
  if (!valid) return res.sendStatus(401);

  const call = JSON.parse(req.body.toString("utf8"));
  if (call.status === "answered" && call.option_label === "Confirm") {
    // markBookingConfirmed(call.metadata.booking_id);
  }
  res.sendStatus(200);
});

app.listen(3000);
```

```python tab="Python (Flask)"
import hashlib
import hmac
import json
import os

from flask import Flask, abort, request

app = Flask(__name__)
SECRET = os.environ["CALLHUMAN_WEBHOOK_SECRET"].encode()

@app.post("/hooks/callhuman")
def callhuman_webhook():
    raw = request.get_data()  # raw bytes, before parsing
    expected = hmac.new(SECRET, raw, hashlib.sha256).hexdigest()
    received = request.headers.get("CallHuman-Signature", "")
    if not hmac.compare_digest(expected, received):
        abort(401)

    call = json.loads(raw)
    if call["status"] == "answered" and call["option_label"] == "Confirm":
        pass  # mark_booking_confirmed(call["metadata"]["booking_id"])
    return "", 200
```

## Retries [#retries]

Respond with any `2xx` status **within 10 seconds** (do slow work after responding). If your server returns an error or times out, we retry **up to 5 times: after 1, 5, 15, 30 and 60 minutes**. The same result can therefore arrive more than once: make your handler **idempotent** and ignore duplicates by `id`.

## No webhook? [#no-webhook]

`webhook_url` is optional. You can poll `GET /v1/calls/{id}` instead — useful for scripts and tests:

```bash
curl https://api.callhuman.es/v1/calls/call_x9f2k \
  -H "Authorization: Bearer $CALLHUMAN_API_KEY"
```