# Ngā Webhook

Kaua e pātengi auau; tukuna he `webhook_url` ki tētahi oma. Ina tae te tono ki tētahi tūnga whakamutunga (`succeeded`, `failed` rānei
`canceled`), ka tukuna e mātou he `POST` kua waitohua ki taua URL.

```bash
curl -X POST "https://api.doitong.com/v1/run/{model_id}?webhook_url=https://example.com/hooks/doitong" \
  -H "Authorization: Bearer $DOITONG_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "..."}'
```

> [!NOTE]
> Me tūmatanui te URL, ā, me whakamahi i te `https://`. Ka whakakāhoretia ngā wāhitau tūmataiti, ā-rohe me ngā wāhitau ehara i te HTTPS mā te
> `422 invalid_webhook_url` i mua i te nama o tētahi mea.

## Te takahanga

```http Ngā pane tono
POST /hooks/doitong HTTP/1.1
Content-Type: application/json
User-Agent: Doitong-Webhooks/1
Doitong-Request-Id: req_8k2m4q7x1z9c3v5b6n0p2r4t
Doitong-Signature: t=1760961609,v1=5f1c0d6e2b…
```

```json Te tinana tono
{
  "id": "evt_8k2m4q7x1z9c3v5b6n0p2r4t_succeeded",
  "object": "event",
  "type": "request.succeeded",
  "created_at": "2026-10-20T12:00:09Z",
  "data": {
    "id": "req_8k2m4q7x1z9c3v5b6n0p2r4t",
    "object": "request",
    "status": "succeeded",
    "outputs": [ { "type": "video", "url": "https://content.doitong.com/dev-api/…/0-output.mp4" } ]
  }
}
```

- Ko `type` ko `request.succeeded`, `request.failed` rānei `request.canceled`.
- Ko `data` te ahanoa tono katoa, he rite tonu ki tā `GET /requests/{id}` e whakahoki ana.
- He pūmau a `id` mō tētahi tono me tōna tūnga whakamutunga. Whakamahia hei waiho atu i tētahi tuku kua tukatukatia kētia e koe.

## Ngā tuku me ngā ngana anō

Whakautu mai ki tētahi tūnga `2xx` i roto i te 10 hēkona. Ko ērā atu mea (ko te pau rānei o te wā) ka ngana anōhia: kia 6 ngā ngana katoa,
i muri i te 30 hēkona, 2 meneti, 10 meneti, 1 hāora me te 6 hāora. Mahia ngā mahi pōturi i muri i tō whakautu, kaua i mua.

## Manatoko i te waitohu

Kei ia kī tōna ake muna waitohu. Pānuitia kotahi mai i `GET https://api.doitong.com/v1/account` (`webhook_signing_secret`,
ā, ka tīmata ki te `whsec_`) ka penapenahia ki te taha o te kī.

Ko te pane `Doitong-Signature` ko `t=<unix time>,v1=<signature>`, ā, ko te waitohu te
HMAC-SHA256 hautekauono o `"<t>.<raw request body>"` me tō muna hei kī. Hei manatoko:

1. Tangohia ngā paita tinana mata kia rite tonu ki te rironga mai (i mua i te tātari JSON).
2. Tātaihia te HMAC-SHA256 o `t + "." + body` me tō muna, hei hautekauono pū iti.
3. Whakatauritea ki `v1` i roto i te wā pūmau.
4. Whakakāhoretia te takahanga ina neke atu a `t` i te 5 meneti te tawhiti atu i tō karaka (tiaki purei anō).

He kaiwhiwhi oti i Node.js (Express) me Python (Flask):

```javascript Node.js · Express
import crypto from "node:crypto";
import express from "express";

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

app.post("/hooks/doitong", express.raw({ type: "application/json" }), (req, res) => {
  const header = req.get("Doitong-Signature") || "";
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=", 2)));
  const t = Number(parts.t);
  const expected = crypto.createHmac("sha256", SECRET).update(t + "." + req.body).digest("hex");
  const given = Buffer.from(parts.v1 || "", "utf8");
  const valid =
    given.length === expected.length &&
    crypto.timingSafeEqual(given, Buffer.from(expected, "utf8")) &&
    Math.abs(Date.now() / 1000 - t) < 300;
  if (!valid) return res.status(400).send("bad signature");

  const event = JSON.parse(req.body);
  if (event.type === "request.succeeded") {
    console.log(event.data.id, event.data.outputs.map((o) => o.url));
  }
  res.sendStatus(204);
});
```
```python Python · Flask
import hashlib
import hmac
import json
import os
import time

from flask import Flask, abort, request

SECRET = os.environ["DOITONG_WEBHOOK_SECRET"].encode()  # whsec_...
app = Flask(__name__)


@app.post("/hooks/doitong")
def doitong_hook():
    body = request.get_data()  # raw bytes
    parts = dict(p.split("=", 1) for p in request.headers.get("Doitong-Signature", "").split(",") if "=" in p)
    t = parts.get("t", "0")
    expected = hmac.new(SECRET, t.encode() + b"." + body, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, parts.get("v1", "")) or abs(time.time() - int(t)) > 300:
        abort(400)

    event = json.loads(body)
    if event["type"] == "request.succeeded":
        print(event["data"]["id"], [o["url"] for o in event["data"]["outputs"]])
    return "", 204
```

## Ngā tohutohu

> [!TIP]
> He whakangāwari noa ngā Webhook, ehara i te pūtake pono: ki te mahue i a koe tētahi, kei `GET /requests/{id}` tonu te
> tūnga whakamutunga.

- Kia tere te whakautu, ka whakarārangi ai i āu ake tukanga.
- Nō te kī API nāna i tīmata te oma te muna. He rerekē ngā muna o ngā kī i ngā ratonga rerekē.