取得您 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 的密鑰作為金鑰計算並經過 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
這個頁面有幫助嗎?