Gwe-gysylltiadau
Yn lle holi dro ar ôl tro, rhowch webhook_url i rediad. Pan fydd y cais yn cyrraedd statws terfynol (succeeded, failed neu
canceled), byddwn yn anfon POST wedi'i lofnodi i'r URL hwnnw.
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": "..."}'
Y digwyddiad
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…
{
"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" } ]
}
}
typeywrequest.succeeded,request.failedneurequest.canceled.datayw gwrthrych y cais llawn, yn union yr hyn y maeGET /requests/{id}yn ei ddychwelyd.- Mae
idyn sefydlog ar gyfer cais a'i statws terfynol. Defnyddiwch ef i anwybyddu cyflenwad rydych eisoes wedi'i brosesu.
Cyflenwadau ac ailgeisiadau
Atebwch ag unrhyw statws 2xx o fewn 10 eiliad. Caiff unrhyw beth arall (neu derfyn amser) ei ailgeisio: hyd at 6 ymgais i gyd,
ar ôl 30 eiliad, 2 funud, 10 munud, 1 awr a 6 awr. Gwnewch waith araf ar ôl ateb, nid cyn hynny.
Gwirio'r llofnod
Mae gan bob allwedd ei chyfrinach lofnodi ei hun. Darllenwch hi unwaith o GET https://api.doitong.com/v1/account (webhook_signing_secret,
mae'n dechrau gyda whsec_) a'i storio wrth ymyl yr allwedd.
Mae pennyn Doitong-Signature yn t=<unix time>,v1=<signature>, lle mae'r llofnod yn HMAC-SHA256 hecs o
"<t>.<raw request body>" gan ddefnyddio eich cyfrinach fel yr allwedd. I wirio:
- Cymerwch y beitiau corff crai yn union fel y'u derbyniwyd (cyn dosrannu unrhyw JSON).
- Cyfrifwch HMAC-SHA256 o
t + "." + bodygyda'ch cyfrinach, fel hecs llythrennau bach. - Cymharwch ef â
v1mewn amser cyson. - Gwrthodwch y digwyddiad pan fo
tfwy na 5 munud oddi wrth eich cloc (amddiffyniad rhag ailchwarae).
Derbynnydd cyflawn yn Node.js (Express) a Python (Flask):
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);
});
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
Awgrymiadau
- Ymatebwch yn gyflym a rhoi eich prosesu eich hun mewn ciw.
- Mae'r gyfrinach yn perthyn i'r allwedd API a ddechreuodd y rhediad. Mae gan allweddi mewn gwasanaethau gwahanol gyfrinachau gwahanol.