Webhook
1tokens 会在平台内发生事件时,向你在控制台登记的 HTTPS 地址发送带签名的 POST 请求。在控制台「Webhook」页新建端点、勾选要订阅的事件即可,新端点最迟 30 秒内生效。
事件目录
模型
| 类型 | 名称 | data 字段 |
|---|---|---|
model.listed | 模型上架 | model tenant_id previous_status status actor |
model.delisted | 模型下架 | model tenant_id previous_status status actor |
model.deprecated | 模型弃用 | model previous_status status actor |
model.price.changed | 售价变动 | model tenant_id pricing_status sale_price_mode multiplier price changes actor |
model.user_price.changed | 专属价变动 | model user_id multiplier changes actor |
model.updated | 模型更新 | model changes actor |
API Key
| 类型 | 名称 | data 字段 |
|---|---|---|
api_key.created | Key 创建 | api_key actor |
api_key.updated | Key 修改 | api_key changes actor |
api_key.revoked | Key 吊销 | api_key previous_status status actor |
api_key.expired | Key 过期 | api_key actor |
api_key.quota.exhausted | 额度用尽 | api_key quota_limit_usd consumed_usd actor |
api_key.rate_window.exceeded | 时间窗超限 | api_key window limit_usd consumed_usd actor |
账户
| 类型 | 名称 | data 字段 |
|---|---|---|
account.balance.low | 余额不足 | account threshold_usd request_id actor |
account.balance.exhausted | 余额耗尽 | account reason request_id actor |
account.credit.topup | 到账 | account amount_usd amount_credits source order_no actor |
account.credit.adjusted | 调额 | account delta_usd delta_credits kind reason actor |
account.trial.granted | 试用发放 | account amount_credits actor |
account.disabled | 账号停用 | account previous_status status actor |
account.enabled | 账号恢复 | account previous_status status actor |
account.password.reset | 密码重置 | account actor |
订单
| 类型 | 名称 | data 字段 |
|---|---|---|
order.created | 订单创建 | order actor |
order.paid | 支付成功 | order paid_at previous_status status actor |
order.failed | 支付失败 | order reason previous_status status actor |
order.redeemed | 兑换成功 | order code_masked actor |
订阅
| 类型 | 名称 | data 字段 |
|---|---|---|
subscription.started | 订阅开通 | subscription previous_status status actor |
subscription.renewed | 订阅续费 | subscription actor |
subscription.upgraded | 订阅升级 | subscription changes actor |
subscription.cancelled | 订阅取消 | subscription previous_status status actor |
subscription.payment_failed | 续费失败 | subscription reason actor |
subscription.quota.exhausted | 配额用尽 | subscription request_id actor |
任务
| 类型 | 名称 | data 字段 |
|---|---|---|
task.video.succeeded | 视频完成 | task video_count actor |
task.video.failed | 视频失败 | task actor |
task.generation.succeeded | 生成完成 | task media output_count actor |
task.generation.failed | 生成失败 | task media actor |
公告
| 类型 | 名称 | data 字段 |
|---|---|---|
content.changelog.published | 更新日志发布 | entry actor |
content.article.published | 文章发布 | article tenant_id previous_status status actor |
content.article.unpublished | 文章下架 | article tenant_id previous_status status actor |
Webhook
| 类型 | 名称 | data 字段 |
|---|---|---|
webhook.endpoint.auto_disabled | 端点被停用 | endpoint reason consecutive_failures actor |
推送格式
所有事件共用同一个信封:id 事件唯一 id、type 事件类型、domain 功能域、created_at 发生时间(ISO 8601 UTC)、api_version 版本、data 事件内容。data 里键名一律 snake_case,时间以 _at 结尾,金额是十进制字符串(_usd / _credits),没有值的字段显式为 null。同一事件可能因重试重复送达,请按 id 做幂等。
{ "id": "evt_01JBXK6Q3N7Y8R2V4W5X6Z7A8B", "type": "model.price.changed", "domain": "model", "created_at": "2026-11-04T08:12:33Z", "api_version": "2026-11-04", "data": { }}请求头
POST https://your-server.example/webhooksContent-Type: application/jsonUser-Agent: Webhooks/1.0X-Event-Id: evt_01JBXK6Q3N7Y8R2V4W5X6Z7A8BX-Event-Type: model.price.changedX-Delivery-Id: 123456X-Timestamp: 1762243953X-Signature: v1=5f1c…验证签名
用创建端点时保存的密钥,对「X-Timestamp 的值 + 英文句点 + 原始请求体字节」计算 HMAC-SHA256,与 X-Signature 里 v1= 后面的十六进制做常量时间比较;时间戳与当前时间相差超过 5 分钟应拒收。先回 2xx 再处理业务,整个响应需在 10 秒内完成。
import { createHmac, timingSafeEqual } from "node:crypto";export function verify(secret, headers, rawBody) { const ts = headers["x-timestamp"]; if (Math.abs(Date.now() / 1000 - Number(ts)) > 300) return false; const expected = createHmac("sha256", secret).update(ts + "." + rawBody).digest("hex"); const given = String(headers["x-signature"]).replace(/^v1=/, ""); return given.length === expected.length && timingSafeEqual(Buffer.from(given, "hex"), Buffer.from(expected, "hex"));}import hmac, hashlib, timedef verify(secret: str, headers: dict, raw_body: bytes) -> bool: ts = headers["X-Timestamp"] if abs(time.time() - int(ts)) > 300: return False expected = hmac.new(secret.encode(), f"{ts}.".encode() + raw_body, hashlib.sha256).hexdigest() given = headers["X-Signature"].removeprefix("v1=") return hmac.compare_digest(given, expected)func verify(secret string, ts, sig string, body []byte) bool { t, _ := strconv.ParseInt(ts, 10, 64) if math.Abs(float64(time.Now().Unix()-t)) > 300 { return false } mac := hmac.New(sha256.New, []byte(secret)) mac.Write([]byte(ts + ".")) mac.Write(body) expected := hex.EncodeToString(mac.Sum(nil)) return hmac.Equal([]byte(strings.TrimPrefix(sig, "v1=")), []byte(expected))}重试与自动停用
非 2xx 响应或 10 秒内未响应都算失败,最多尝试 7 次,间隔如下表。全部失败后该条投递标记为耗尽;同一端点连续 10 条投递耗尽会被自动停用并邮件通知,修复后可在控制台重新启用。任一条投递都可在控制台手动重发。
| 第 N 次尝试 | 距上一次 |
|---|---|
| 1 | 0 |
| 2 | 1 min |
| 3 | 5 min |
| 4 | 30 min |
| 5 | 2 h |
| 6 | 6 h |
| 7 | 24 h |
接收地址要求
必须是公网可达的 HTTPS 地址;解析到内网、回环、链路本地等地址的域名会被拒绝。不跟随重定向,响应正文最多读取 64 KB。

