跳到主要内容

Webhook 事件

Webhook 把简数侧发生的事件推送到你的 HTTPS 端点,避免轮询。

支持的事件:

事件触发时机payload 关键字段
task.run.completed一轮采集完成(0 条也会发)taskId、runId、recordCount、startedAt、finishedAt
task.run.failed采集失败taskId、crawlType、occurredAt
export.completed导出完成exportId、taskId、format、rowCount、expiresAt
webhook.test调用测试接口endpointId、test=true

webhook.test 只能由测试接口触发,不可订阅,也不受订阅过滤。

创建端点​

POST /api/v2/webhooks
Authorization: Bearer <API Key>
Content-Type: application/json
{
"url": "https://your-domain.com/keydatas/events",
"events": ["task.run.completed", "task.run.failed", "export.completed"],
"taskId": 0
}
字段必填说明
url是仅 HTTPS;≤1024;端口只允许 80/443/8080/8443;不允许 fragment/userinfo
events是必须是上面三类之一;空数组或未接线类型 → 422
taskId否0 或省略 = 订阅该用户全部任务;>0 必须属于当前用户

响应:

{
"data": {
"id": "12",
"url": "https://your-domain.com/keydatas/events",
"events": ["task.run.completed"],
"taskId": 0,
"status": "active",
"createdAt": 1760000000000,
"updatedAt": 1760000000000,
"secret": "64位小写hex",
"secretMasked": "****abcd"
},
"requestId": "...",
"timestamp": 1760000000123
}

secret 只在创建响应里返回一次。丢失后只能删除端点重建。列表接口只返回 secretMasked。

限制:

  • 需要 webhook scope;
  • 当前不支持 Idempotency-Key,重试会创建两个端点;
  • 端点数按套餐:Free/Standard 0、试用/专业 1、旗舰 10、企业默认 50;
  • URL 会做 DNS 解析与 SSRF 校验,私网/回环/云元数据地址返回 422 invalid_url。

列出与删除​

GET    /api/v2/webhooks
DELETE /api/v2/webhooks/{id}

删除是软删(status=disabled)。重复删除返回 404;已删除端点仍可查询历史事件,但不能回放。

发送测试事件​

POST /api/v2/webhooks/{id}/test

返回:

{
"data": {"eventId": "32位hex", "status": "queued"},
"requestId": "...",
"timestamp": 1760000000123
}

queued 只表示事件已入队,不代表已投递成功。结果看你的端点是否收到请求。

投递契约​

POST <你的 url>
Content-Type: application/json; charset=utf-8
User-Agent: keydatas-event-relay/1.0
X-Keydatas-Event-Id: <32 位 hex>
X-Keydatas-Event-Type: task.run.completed
X-Keydatas-Timestamp: <Unix 秒>
X-Keydatas-Signature: sha256=<hex>
X-Keydatas-Delivery-Attempt: <1..6>

签名:

signature = "sha256=" + hex(HMAC-SHA256(secret, timestamp + "." + rawBody))
  • timestamp 是 X-Keydatas-Timestamp 的原始字符串(Unix 秒);
  • rawBody 是请求体的原始字节,不要先解析再序列化;
  • 比较时使用常量时间比较;
  • 建议只接受时间戳在 ±5 分钟内的请求。

Node.js 校验​

import {createHmac, timingSafeEqual} from 'node:crypto';

function verify(req, secret) {
const timestamp = req.headers['x-keydatas-timestamp'];
const expected = 'sha256=' + createHmac('sha256', secret)
.update(`${timestamp}.${req.rawBody}`)
.digest('hex');
const given = req.headers['x-keydatas-signature'];
return Math.abs(Date.now() / 1000 - Number(timestamp)) <= 300
&& given.length === expected.length
&& timingSafeEqual(Buffer.from(given), Buffer.from(expected));
}

Python 校验​

import hashlib
import hmac
import time

def verify(secret: str, timestamp: str, raw_body: bytes, signature: str) -> bool:
expected = "sha256=" + hmac.new(
secret.encode("utf-8"),
timestamp.encode("utf-8") + b"." + raw_body,
hashlib.sha256,
).hexdigest()
return (
abs(time.time() - int(timestamp)) <= 300
and hmac.compare_digest(expected, signature)
)

重试与死信​

第 1 次失败 → 1 分钟后重试
第 2 次失败 → 5 分钟后
第 3 次失败 → 30 分钟后
第 4 次失败 → 2 小时后
第 5 次失败 → 6 小时后
第 6 次失败 → DEAD(死信)
  • 首投 + 5 次重试 = 最多 6 次 HTTP 尝试;
  • 2xx 视为成功;3xx 不跟随、按失败处理;超时 10 秒;
  • 投递是 at-least-once:极端情况下可能重复,请按 X-Keydatas-Event-Id 幂等去重;
  • 端点被软删或不存在时,事件直接置为 DONE,不进死信(用户主动删除的端点回放没有意义)。

查询事件与死信​

GET /api/v2/webhooks/{id}/events?status=DEAD&eventType=export.completed&limit=20
参数默认说明
statusallPENDING / SENDING / DONE / DEAD
eventTypeall三类事件 + webhook.test
createdFrom / createdTo0Unix 毫秒;0=不限
limit201~100
beforeId0上一页最后一条 id;0=第一页

响应:

{
"data": {
"items": [
{
"id": 9001,
"eventId": "32位hex",
"eventType": "task.run.completed",
"status": "DEAD",
"attempts": 6,
"taskId": 738590,
"lastError": "connection refused",
"nextRetryAt": 0,
"createdAt": 1760000000000,
"updatedAt": 1760003600000,
"payloadJson": "{\"eventType\":\"task.run.completed\",...}"
}
],
"hasMore": false,
"nextBeforeId": 0
}
}
  • 按发件箱自增 id 倒序;
  • status=DEAD 即死信;
  • payloadJson 是投递体原文,已保证不含 secret 与下载 token。

回放死信​

单条​

POST /api/v2/webhooks/{id}/events/{eventId}/replay

批量​

POST /api/v2/webhooks/{id}/events/replay
Content-Type: application/json

{"eventIds": ["32位hex", "另一个32位hex"]}

规则:

  • 只有 DEAD 可以回放;DONE 回放会让消费方收到重复事件;
  • 批量最多 50 条,重复项自动去重,允许部分成功;
  • 回放后 eventId 不变,attempts=0,X-Keydatas-Delivery-Attempt 从 1 重新计数;
  • 端点已软删 → 409;事件不存在/非 DEAD/状态已变化 → 409;
  • 总开关关闭时,创建/测试/回放返回 503。

事件 payload 示例​

task.run.completed​

{
"eventType": "task.run.completed",
"taskId": 738590,
"runId": "20261113-001",
"status": "completed",
"crawlType": 1,
"recordCount": 128,
"startedAt": 1760000000000,
"finishedAt": 1760003600000,
"occurredAt": 1760003600100
}

task.run.failed​

{
"eventType": "task.run.failed",
"taskId": 738590,
"status": "failed",
"crawlType": 1,
"occurredAt": 1760003600100
}

v1 不带失败原因与 runId;具体原因看任务运行明细或日志。

export.completed​

{
"eventType": "export.completed",
"exportId": "8f3a2c1d4e5b6a7c8d9e0f1a2b3c4d5e",
"taskId": 738590,
"format": "xlsx",
"rowCount": 18342,
"expiresAt": 1760604800000,
"occurredAt": 1760003600100
}

payload 不含 downloadUrl / token。收到事件后自行调用 GET /exports/{id} 换取下载地址。

保留期​

类型保留
DONE7 天
DEAD30 天
投递流水30 天
PENDING / SENDING不清理

最佳实践​

  1. 先校验签名与时间窗,再解析业务。
  2. 按 X-Keydatas-Event-Id 做幂等(数据库唯一键或去重表)。
  3. 端点快速返回 2xx;重活放队列,不要在 HTTP 请求里做长任务。
  4. 收到 export.completed 后立即换下载地址,不要等快过期。
  5. 记录 X-Keydatas-Delivery-Attempt,便于排查是首投还是重试。
  6. 不要依赖事件顺序;用 occurredAt 与业务 ID 自己排序。

相关​