# Webhooks

Webhooks tell your server about every finished verification, including ones started from the dashboard or a bulk CSV upload. To turn them on, an organization admin sets your HTTPS endpoint in the dashboard's webhook settings. The dashboard then shows a signing secret (`whsec_…`) once; store it securely.

## Events

| Type | Sent when |
| --- | --- |
| `verification.completed` | A verification completed, including "not found" answers |
| `verification.failed` | A verification failed at the source (and was refunded) |

## Payload

```json
{
  "id": "cmhd2k9a20004s60hb7c1d2e3",
  "type": "verification.completed",
  "createdAt": "2026-10-30T09:15:02.530Z",
  "data": {
    "id": "cmhd2k7q80001s60h3x9f2a1b",
    "api": "identity.national-id",
    "status": "COMPLETED",
    "environment": "LIVE",
    "result": { "found": true, "...": "..." },
    "error": null,
    "cost": "50",
    "latencyMs": 412,
    "createdAt": "2026-10-30T09:15:02.118Z",
    "completedAt": "2026-10-30T09:15:02.530Z"
  }
}
```

`data` is the same object that `GET /v1/verifications/{id}` returns.

## Delivery and retries

- Your endpoint has 10 seconds to reply with any `2xx` status. Do slow work after you reply.
- Failed deliveries are retried with exponential back-off (from 2 seconds up to an hour between tries), up to 10 attempts.
- Redirects are not followed.
- An event can arrive more than once, and events can arrive out of order. **Deduplicate on the event `id`.**

## Verifying the signature

Every delivery is signed with your secret. The signature is in this header:

```
X-Nilinde-Signature: t=1793351702,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
```

To check it:

1. Take `t` (Unix seconds) and `v1` from the header.
2. Compute HMAC-SHA256 of `"<t>.<raw request body>"` using your secret, as lowercase hex.
3. Compare it with `v1` in constant time.
4. Reject the request if they differ, or if `t` is more than 5 minutes old, which stops replays.

Use the raw body bytes exactly as received, not re-serialized JSON.

Node.js (Express):

```js
const crypto = require("crypto");

app.post("/webhooks/nilinde", express.raw({ type: "application/json" }), (req, res) => {
  const header = req.get("X-Nilinde-Signature") ?? "";
  const { t, v1 } = Object.fromEntries(header.split(",").map((part) => part.split("=")));
  const expected = crypto.createHmac("sha256", process.env.NILINDE_WEBHOOK_SECRET).update(`${t}.${req.body}`).digest("hex");
  const valid = v1?.length === expected.length && crypto.timingSafeEqual(Buffer.from(v1), Buffer.from(expected));
  if (!valid || Math.abs(Date.now() / 1000 - Number(t)) > 300) return res.sendStatus(400);

  const event = JSON.parse(req.body);
  // dedupe on event.id, then handle event.type
  res.sendStatus(200);
});
```

Python (Flask):

```python
import hashlib, hmac, os, time
from flask import request, abort

@app.post("/webhooks/nilinde")
def nilinde_webhook():
    parts = dict(p.split("=", 1) for p in request.headers.get("X-Nilinde-Signature", "").split(","))
    body = request.get_data()
    expected = hmac.new(os.environ["NILINDE_WEBHOOK_SECRET"].encode(), f"{parts.get('t')}.".encode() + body, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, parts.get("v1", "")) or abs(time.time() - int(parts.get("t", 0))) > 300:
        abort(400)
    event = request.get_json()
    # dedupe on event["id"], then handle event["type"]
    return "", 200
```
