取得 Zonos 整合的即時事件通知。
Webhook 讓 Zonos 能夠在特定事件發生時,主動通知您的外部系統。當訂閱的事件發生時,Zonos 會向您指定的 Webhook URL 傳送 HTTP POST 請求。請求主體將包含事件詳細資訊,讓您的系統以程式設計方式處理該事件。
Webhook 適用於將 Zonos 與其他平台整合、觸發自動化工作流程,以及在即時系統中保持資料同步。例如,您可以使用 Webhook 來:
所有可用的 Webhook 類型都包含在 WebhookType 列舉中。每個類型的範例內容可以在我們的 Event Types 指南中找到。
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列舉中。每個類型的範例內容可以在我們的 Event Types 指南中找到。建立 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 的密鑰作為金鑰計算後,以 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
這個頁面有幫助嗎?