获取 Zonos 集成的实时事件通知。
Webhook 为 Zonos 提供了一种方式,在发生某些事件时主动通知您的外部系统。当订阅的事件发生时,Zonos 将向您指定的 webhook URL 发送 HTTP POST 请求。请求体将包含事件详情,允许您的系统以编程方式处理该事件。
Webhook 对于将 Zonos 与其他平台集成、触发自动化工作流以及实时跨系统保持数据同步很有用。例如,您可以使用 webhook 来:
所有可用的 webhook 类型都包含在 WebhookType 枚举中。每个类型的示例载荷可在我们的事件类型指南中找到。
WebhookType
要通过 API 创建 webhook:
mutation WebhookCreate($input: WebhookCreateInput!) {
webhookCreate(input: $input) {
id
url
type
status
secret
headers {
key
}
重要: 创建 webhook 时请保存 secret 值 — 该值仅在此处返回一次,您需要用它来验证 webhook 签名。请参阅下方的验证 webhook 签名。
要通过 API 编辑现有 webhook:
mutation WebhookUpdate($input: WebhookUpdateInput!) {
webhookUpdate(input: $input) {
Zonos 发送的每个 webhook 请求都包含一个 zonos-signature 标头,以便您确认该请求确实来自 Zonos,且负载在传输过程中未被篡改。
zonos-signature
该标头的值格式为:
timestamp=<unix-timestamp-ms>,hmac=<base64-encoded-signature>
timestamp
hmac
验证请求的步骤:
const crypto = require('crypto');
function verifyZonosWebhook(rawBody, signatureHeader, secret) {
const [timestampPart, hmacPart] = signatureHeader.split(',');
const receivedHmac = hmacPart.split('=')[1];
const expectedHmac = crypto
.createHmac('sha256', secret)
.update(rawBody)
.digest('base64');
const receivedBuffer = Buffer.from(receivedHmac);
const expectedBuffer = Buffer.from(expectedHmac);
if (receivedBuffer.length !== expectedBuffer.length) {
return false;
return crypto.timingSafeEqual(receivedBuffer, expectedBuffer);
注意: 如果您在 webhook 上配置了自定义标头,这些标头也会随 zonos-signature 一起逐字包含在每个请求中。
要通过 API 查看 webhook 日志:
query WebhookLogs(
$first: Int
$after: String
$filter: WebhookLogsFilterInput
) {
webhookLogs(first: $first, after: $after, filter: $filter) {
edges {
node {
createdAt
responseStatus
WebhookCreateInput WebhookLogsFilterInput WebhookUpdateInput
webhookCreate webhookUpdate
Webhook
使用 webhook 监听事件
获取 Zonos 集成的实时事件通知。
Webhook 为 Zonos 提供了一种方式,在发生某些事件时主动通知您的外部系统。当订阅的事件发生时,Zonos 将向您指定的 webhook URL 发送 HTTP POST 请求。请求体将包含事件详情,允许您的系统以编程方式处理该事件。
Webhook 对于将 Zonos 与其他平台集成、触发自动化工作流以及实时跨系统保持数据同步很有用。例如,您可以使用 webhook 来:
Webhook 类型
所有可用的 webhook 类型都包含在
WebhookType枚举中。每个类型的示例载荷可在我们的事件类型指南中找到。创建 webhook
要通过 API 创建 webhook:
mutation WebhookCreate($input: WebhookCreateInput!) {webhookCreate(input: $input) {idurltypestatussecretheaders {key}}}编辑 webhook 详情
要通过 API 编辑现有 webhook:
mutation WebhookUpdate($input: WebhookUpdateInput!) {webhookUpdate(input: $input) {idurltypestatus}}验证 webhook 签名
Zonos 发送的每个 webhook 请求都包含一个
zonos-signature标头,以便您确认该请求确实来自 Zonos,且负载在传输过程中未被篡改。该标头的值格式为:
timestamp— 请求签名时的时间,以 Unix 纪元毫秒为单位。hmac— 原始 JSON 请求体的 HMAC-SHA256 签名,使用您 webhook 的 secret 作为密钥计算并进行 Base64 编码。验证请求的步骤:
zonos-signature标头中解析出timestamp和hmac的值。hmac值进行比较,如果不匹配则拒绝该请求。timestamp早于几分钟前的请求,以防止被截获的请求遭到重放。Zonos 本身不强制执行送达时间窗口,因此是否进行此项检查由您决定。const crypto = require('crypto');function verifyZonosWebhook(rawBody, signatureHeader, secret) {const [timestampPart, hmacPart] = signatureHeader.split(',');const receivedHmac = hmacPart.split('=')[1];const expectedHmac = crypto.createHmac('sha256', secret).update(rawBody).digest('base64');const receivedBuffer = Buffer.from(receivedHmac);const expectedBuffer = Buffer.from(expectedHmac);if (receivedBuffer.length !== expectedBuffer.length) {return false;}return crypto.timingSafeEqual(receivedBuffer, expectedBuffer);}查看 webhook 日志
要通过 API 查看 webhook 日志:
query WebhookLogs($first: Int$after: String$filter: WebhookLogsFilterInput) {webhookLogs(first: $first, after: $after, filter: $filter) {edges {node {idtypeurlcreatedAtresponseStatus}}}}WebhookCreateInput WebhookLogsFilterInput WebhookUpdateInput
webhookCreate webhookUpdate
这个页面有帮助吗?