Webhooks API
配置 webhooks 以接收事件通知。
列出 Webhooks
GET /v1/webhooks
响应:
{
"success": true,
"webhooks": [
{
"id": "wh_abc123",
"url": "https://your-server.com/webhook",
"events": ["index.complete", "security.alert"],
"active": true,
"createdAt": "2024-01-10T08:00:00Z"
}
]
}
创建 Webhook
POST /v1/webhooks
请求体:
{
"url": "https://your-server.com/webhook",
"events": ["index.complete", "security.alert"],
"secret": "您的-webhook-密钥"
}
响应:
{
"success": true,
"webhook": {
"id": "wh_xyz789",
"url": "https://your-server.com/webhook",
"events": ["index.complete", "security.alert"],
"active": true
}
}
删除 Webhook
DELETE /v1/webhooks/:id
可用事件
| 事件 | 描述 |
|---|---|
index.started | 索引已开始 |
index.complete | 索引成功完成 |
index.failed | 索引失 败 |
security.alert | 发现新的安全漏洞 |
analysis.complete | 分析完成 |
Webhook 负载
所有 webhooks 接收 JSON 负载:
{
"event": "index.complete",
"timestamp": "2024-01-15T10:30:00Z",
"project": {
"id": "proj_abc123",
"name": "my-app"
},
"data": {
"filesCount": 234,
"symbolsCount": 1847,
"duration": 45
}
}
验证签名
Webhooks 包含用于验证的签名头:
X-ArchiCore-Signature: sha256=abc123...
验证 (Node.js):
const crypto = require('crypto');
function verifyWebhook(payload, signature, secret) {
const expected = crypto
.createHmac('sha256', secret)
.update(payload)
.digest('hex');
return crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(`sha256=${expected}`)
);
}
重试策略
失败的 webhook 投递会重试:
- 尝试 1: 立即
- 尝试 2: 1 分钟后
- 尝试 3: 5 分钟后
- 尝试 4: 30 分钟后
- 尝试 5: 2 小时后
5 次失败尝试后,webhook 将被禁用。