产品文档
回调与签名
更新于 2026-12-03
异步任务结束、人工复核出结论、申诉处理完成时,穹盾向你方的回调地址发送一条 POST 请求。
配置
在控制台「讯蒙穹盾 → 设置」填写回调地址(须为 https)、勾选要接收的事件、生成签名密钥。创建异步任务时也可以用 callback_url 为这个任务单独指定地址。
事件
| 事件 | 触发时机 |
|---|---|
task.completed | 异步任务完成 |
task.failed | 异步任务失败(预扣已退回) |
review.completed | 一条送审记录的人工复核出结论 |
feedback.resolved | 一条申诉处理完成 |
回调请求头
| 请求头 | 类型 | 说明 |
|---|---|---|
X-Guard-Event | string | 事件类型 |
X-Guard-Delivery | string | 本次投递唯一 ID(如 gwd_...),用于幂等去重 |
X-Guard-Timestamp | integer | 投递发送时的 Unix 秒级时间戳 |
X-Guard-Signature | string | 仅配置签名密钥时附带,格式为 t=<时间戳>,v1=<HMAC-SHA256 十六进制> |
请求格式
HTTP
POST /hooks/guard HTTP/1.1
Content-Type: application/json
X-Guard-Event: task.completed
X-Guard-Delivery: gwd_5d1c0b7e9a3f4c2d8e6f1a0b
X-Guard-Timestamp: 1795852800
X-Guard-Signature: t=1795852800,v1=5f2b…c9a1JSON
{
"id": "gwd_5d1c0b7e9a3f4c2d8e6f1a0b",
"event": "task.completed",
"created_at": "2026-11-28T08:00:41.000Z",
"data": {
"id": "gt_3b7f5d2e9c1a4f60a1b2c3d4",
"object": "guard.task",
"type": "document",
"status": "succeeded",
"data_id": "report_2026Q4",
"result": { "decision": "review", "counts": { "pass": 2, "review": 1, "block": 0 } }
}
}任务回调只带汇总,逐条明细用 GET /v1/guard/tasks/{id} 获取。
校验签名
请先在控制台「讯蒙穹盾 → 设置 → 回调设置」中生成回调签名密钥(未配置密钥时回调仅投递事件正文,不附带 X-Guard-Signature)。启用密钥后,签名 = HMAC-SHA256(签名密钥, "<时间戳>.<请求体原文>") 的十六进制。校验时用收到的请求体原始字节(不要先解析再序列化),先校验请求头完整性与 5 分钟时间窗:
Python
import hashlib, hmac, time
def verify(secret: str, header: str | None, body: bytes) -> bool:
if not secret or not header:
return False
parts = dict(kv.split("=", 1) for kv in header.split(",") if "=" in kv)
ts_str, sig = parts.get("t"), parts.get("v1")
if not ts_str or not sig or not ts_str.isdigit():
return False
if abs(time.time() - int(ts_str)) > 300:
return False
expected = hmac.new(secret.encode(), f"{ts_str}.".encode() + body, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, sig)JavaScript
import { createHmac, timingSafeEqual } from "node:crypto";
export function verify(secret, header, rawBody) {
if (!secret || typeof header !== "string" || !header) return false;
const parts = Object.fromEntries(
header.split(",").filter((kv) => kv.includes("=")).map((kv) => kv.split("=", 2)),
);
const ts = Number(parts.t);
if (!Number.isInteger(ts) || !parts.v1 || Math.abs(Date.now() / 1000 - ts) > 300) {
return false;
}
const expected = Buffer.from(
createHmac("sha256", secret).update(`${parts.t}.`).update(rawBody).digest("hex"),
);
const actual = Buffer.from(parts.v1);
return expected.length === actual.length && timingSafeEqual(expected, actual);
}应答与重试
- 你方在 10 秒内返回任意 2xx 状态码即视为送达。
- 没送达时按 1 分钟、5 分钟、30 分钟、2 小时、6 小时的间隔重试 5 次。
- 同一事件可能重复送达,请按
X-Guard-Delivery去重。 - 投递记录与每次的应答状态码在控制台「设置 → 回调记录」里查看,可手动重发。