标准 Webhook 的工作方式
对于没有 event_name 的工作流,ModelRiver 会在处理完成后立即把完整 AI 响应发送到你的 webhook 端点。这是一种简单的 fire-and-forget 模式:你的后端收到结果后,自行按需处理。不需要回调,返回任意 2xx 状态码即可。
Webhook 只会在异步请求(POST /v1/ai/async)时发送,并会发送到项目中所有已启用的 webhook 端点。同步请求和 OpenAI 兼容接口会直接返回结果,不会发送 webhook。
Webhook 载荷结构
每次投递都是一层外壳(envelope),事件位于 data 中:
JSON
1{2 "callback_url": "https://api.modelriver.com/v1/callback/550e8400-e29b-41d4-a716-446655440000",3 "channel_id": "550e8400-e29b-41d4-a716-446655440000",4 "data": {5 "channel_id": "550e8400-e29b-41d4-a716-446655440000",6 "customer_data": {7 "metadata.ticket_id": "T-123"8 },9 "data": {10 "summary": "Customer requested refund for order #12345...",11 "sentiment": "negative",12 "category": "billing"13 },14 "message": "success",15 "meta": {16 "status": "success",17 "http_status": 200,18 "workflow": "customer_support_summary",19 "requested_provider": "openai",20 "requested_model": "gpt-5.6-terra",21 "used_provider": "openai",22 "used_model": "gpt-5.6-terra",23 "duration_ms": 2341,24 "usage": {25 "prompt_tokens": 245,26 "completion_tokens": 89,27 "total_tokens": 334,28 "cache_read_tokens": 0,29 "cache_write_tokens": 0,30 "cache_write_1h_tokens": 0,31 "reasoning_tokens": 032 },33 "structured_output": true,34 "attempts": [35 { "provider": "openai", "model": "gpt-5.6-terra", "status": "success" }36 ],37 "customer_fields": ["metadata.ticket_id"]38 },39 "status": "success"40 },41 "timestamp": 176761649642}| 字段 | 说明 |
|---|---|
channel_id | 本次请求的唯一标识,可用于投递去重 |
timestamp | 本次投递签名时的 Unix 时间(秒) |
callback_url | 始终存在,但只用于后端管道工作流;对标准工作流调用它会返回 404 channel_not_found |
data.status | "success" 或 "error" |
data.data | AI 结果。使用结构化输出时,这里是已通过校验的对象 |
data.customer_data | 请求的缓存字段,以字段路径为键 |
data.meta | 实际使用的供应商与模型、token 用量、耗时,以及每一次尝试(包括故障转移) |
失败时的投递
如果请求失败,例如工作流中的所有模型都失败,或触发了支出上限,webhook 仍然会发送。此时 data.status 为 "error",data.error 说明失败原因:
JSON
1{2 "channel_id": "550e8400-e29b-41d4-a716-446655440000",3 "status": "error",4 "message": "Model is no longer available",5 "error": {6 "type": "model_unavailable",7 "message": "Model is no longer available",8 "workflow": "customer_support_summary",9 "attempts": [ ... ]10 },11 "customer_data": { "metadata.ticket_id": "T-123" },12 "meta": {13 "status": "error",14 "http_status": 422,15 "workflow": "customer_support_summary",16 "duration_ms": 512,17 "usage": { "prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0, "cache_read_tokens": 0, "cache_write_tokens": 0, "cache_write_1h_tokens": 0, "reasoning_tokens": 0 },18 "attempts": [ ... ]19 }20}Webhook 请求头
| Header | 说明 |
|---|---|
x-modelriver-signature | HMAC-SHA256 签名,见签名验证 |
x-modelriver-timestamp | 本次投递签名时的 Unix 时间(秒) |
x-modelriver-webhook-id | 本次投递所属 webhook 端点的 ID |
content-type | 恒为 application/json |
集成示例
先用原始请求体验证签名,再从 data 中读取事件。
Node.js(Express)
JAVASCRIPT
1// verifyModelRiverSignature: see /docs/webhooks/signature-verification2app.post('/webhooks/ai', express.raw({ type: 'application/json' }), (req, res) => {3 const rawBody = req.body.toString('utf8');4 const ok = verifyModelRiverSignature(5 rawBody,6 req.get('x-modelriver-timestamp'),7 req.get('x-modelriver-signature'),8 process.env.MODELRIVER_WEBHOOK_SECRET9 );10 if (!ok) return res.status(401).json({ error: 'Invalid signature' });11 12 const event = JSON.parse(rawBody).data;13 14 if (event.status === 'success') {15 // 在这里编写你的业务逻辑16 console.log('AI completed:', event.data, event.customer_data);17 } else {18 console.error('AI request failed:', event.error);19 }20 21 res.status(200).json({ received: true });22});Python(Flask)
PYTHON
1# verify_modelriver_signature: see /docs/webhooks/signature-verification2@app.route('/webhooks/ai', methods=['POST'])3def handle_webhook():4 raw_body = request.get_data()5 if not verify_modelriver_signature(6 raw_body,7 request.headers.get('x-modelriver-timestamp', ''),8 request.headers.get('x-modelriver-signature', ''),9 os.environ['MODELRIVER_WEBHOOK_SECRET'],10 ):11 return jsonify({'error': 'Invalid signature'}), 40112 13 event = json.loads(raw_body)['data']14 15 if event['status'] == 'success':16 # 在这里编写你的业务逻辑17 print(f"AI completed: {event['data']}")18 else:19 print(f"AI request failed: {event['error']}")20 21 return jsonify({'received': True}), 200