Webhook
註冊 webhook
註冊 Sofia 以帶簽章 JSON 事件呼叫的 https URL,失敗最多重試五次。金鑰僅顯示一次,最多 10 個。
註冊 Webhook
註冊一個公開的 https 地址和你想要的事件。簽名金鑰(whsec_…)只會出現在這次回應中。一個應用程式最多可以有 10 個 Webhook。
curl -X POST https://api.sofia-post.com/api/v1/webhooks \
-H "Authorization: Bearer $SOFIA_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"url":"https://app.example.com/hooks/sofia","events":["post.published","post.failed"]}'事件
| 事件 | data |
|---|---|
post.published | postId、establishmentId、platform、publishedAt、permalink |
post.failed | postId、establishmentId、platform、error |
webhook.test | webhookId |
Sofia 以 POST 方式傳送每個事件,格式為 JSON { id, type, createdAt, data },並帶有 Sofia-Event-Id、Sofia-Event-Type 和 Sofia-Signature 請求標頭。只有由你的應用程式建立的貼文才會被通知。
驗證簽名
Sofia-Signature 的值為 t=<unix time>,v1=<hex>,其中 v1 是用你的金鑰對 <t>.<raw body> 計算的 HMAC-SHA256。請對原始請求本文驗證,並拒絕過舊的時間戳:
import { createHmac, timingSafeEqual } from 'node:crypto';
const isSignedBySofia = (header, rawBody, secret) => {
const parts = Object.fromEntries(
(header ?? '').split(',').map((part) => part.trim().split('=', 2)),
);
const timestamp = Number(parts.t);
if (!Number.isInteger(timestamp) || typeof parts.v1 !== 'string') return false;
if (Math.abs(Date.now() / 1000 - timestamp) > 300) return false;
const expected = createHmac('sha256', secret).update(`${timestamp}.${rawBody}`).digest();
const received = Buffer.from(parts.v1, 'hex');
return received.length === expected.length && timingSafeEqual(received, expected);
};投遞與重試
- 返回
2xx即表示事件已送達。任何其他回應、重新導向(從不跟隨)或 10 秒無回應,都會在 1 分鐘、5 分鐘、30 分鐘和 2 小時之後觸發重試,最多重試 5 次。 - 同一個事件可能到達兩次:請根據
Sofia-Event-Id去重。 - 請盡快回應,之後再處理事件。
授權
Authorizationstringheader必填你的個人金鑰或 OAuth 存取權杖,以
Bearer <token>傳送。
請求本文
urlstring必填Sofia 呼叫的 https 位址。解析到私有、迴路或連結本機位址的主機會被拒絕
eventsstring[]必填要接收的事件
回應
webhook 及其簽章金鑰,僅顯示一次。
idstring必填webhook 的識別碼
urlstring必填Sofia 呼叫的位址
eventsstring[]必填已訂閱的事件
createdAtstring<date-time>必填webhook 的註冊時間
secretstring必填為每次遞送簽章的 HMAC-SHA256 金鑰(
Sofia-Signature)。僅此一次顯示
這個頁面對你有幫助嗎?