Webhook

在您自己的 URL 上接收 HTTP 事件,而无需轮询 API。

创建一个 Webhook

http
POST /v1/webhooks
json
{
  "url": "https://votre-app.exemple.com/webhooks/emini",
  "events": ["job.completed", "job.failed"]
}
json
{
  "webhook": { "id": "...", "url": "https://votre-app.exemple.com/webhooks/emini", "events": ["job.completed", "job.failed"], "status": "active" },
  "secret": "whsec_..."
}
secret 仅在创建时返回一次。请妥善保存,用于验证后续投递的签名。

可用事件

  • job.completed — 一个异步任务(视频)已成功完成
  • job.failed — 一个异步任务已失败
  • budget.alert — 项目预算的警报阈值已被触发

验证签名

每次投递都会附带一个 X-Emini-Signature 请求头:使用您的 secret 对原始请求体计算得到的 HMAC-SHA256 值。

Node.jstypescript
import crypto from "node:crypto";

function isValid(rawBody: string, signature: string, secret: string): boolean {
  const expected = crypto.createHmac("sha256", secret).update(rawBody).digest("hex");
  return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
}

管理 Webhook

bash
GET /v1/webhooks                              # lister
DELETE /v1/webhooks/{webhook_id}              # desactiver
GET /v1/webhooks/{webhook_id}/deliveries      # historique des livraisons

替代方案:轮询

Webhook 为可选功能——GET /v1/jobs/{job_id} 始终可用,可用于主动轮询任务状态(参见视频指南)。