Webhooks

任务完成或失败时,实时接收通知

概述

Webhooks allow you to receive HTTP callbacks when your image or video generation tasks complete. Instead of polling for results, you can set up a webhook endpoint to receive automatic notifications.

可用事件
  • job.completedTriggered when a task completes successfully
  • job.failedTriggered when a task fails
API 端点
GET/api/v1/webhooks

List all your webhook configurations

响应示例:

{
  "success": true,
  "data": [
    {
      "id": 1,
      "url": "https://your-server.com/webhook",
      "events": ["job.completed", "job.failed"],
      "status": "active"
    }
  ]
}
POST/api/v1/webhooks

Create a new webhook endpoint

请求示例:

{
  "url": "https://your-server.com/webhook",
  "events": ["job.completed", "job.failed"],
  "secret_key": "your-secret-key"
}

参数:

参数类型必需描述
urlstring必需Your webhook endpoint URL
eventsstring[]必需Events to subscribe to
secret_keystring可选Secret for signature verification

响应示例:

{
  "success": true,
  "data": {
    "id": 1,
    "url": "https://your-server.com/webhook",
    "events": ["job.completed", "job.failed"],
    "status": "active"
  }
}
DELETE/api/v1/webhooks/{id}

Delete a webhook endpoint

响应示例:

{
  "success": true,
  "data": { "id": 1 }
}
Webhook 负载
发送到您端点的 Webhook 通知格式
页首描述
X-Nano-TimestampUnix timestamp of the request
X-Nano-SignatureHMAC-SHA256 signature for verification

示例载荷:

{
  "event": "job.completed",
  "task_id": "task_xxx",
  "task_type": "image",
  "status": "completed",
  "data": {
    "url": "https://cdn.example.com/image.png",
    "credits_charged": 6
  },
  "timestamp": "2024-12-23T10:00:00Z"
}
签名验证
const crypto = require('crypto');

function verifySignature(payload, signature, secret, timestamp) {
  const message = `${timestamp}.${JSON.stringify(payload)}`;
  const expectedSig = crypto
    .createHmac('sha256', secret)
    .update(message)
    .digest('hex');
  return `v1=${expectedSig}` === signature;
}
Nano Banana Pro API