コンテンツにスキップ

Replifierのwebhookと署名検証

送信webhookは、Replifierで何かが起きたときにシステムへ通知します。APIを定期的に問い合わせる必要はありません。すべての配信は、Replifierとお客様だけが知るシークレットで署名されています。URLを知っていれば誰でも偽のデータを送れるため、内容を信用する前に必ず署名を検証してください。

webhooks.manage 権限を持つAPIキー、またはアプリのセッションを使います。

ターミナルウィンドウ
curl -X POST "https://api.replifier.com/v1/brands/$BRAND_ID/webhooks" \
-H "Authorization: Bearer $REPLIFIER_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "name": "My system", "url": "https://mydomain.com/hooks/replifier", "events": ["message.sent", "message.failed"] }'

利用できるイベントは message.sent、message.failed、contact.created、contact.updated、contact.unsubscribed、campaign.sent、campaign.failed、campaign.started、flow.email_sent、whatsapp.message.received、whatsapp.message.status、whatsapp.template.status、message.received、message.delivered、message.read、message.replied、link.clicked、conversion.recorded です。レスポンスには secret フィールドが含まれますが、表示されるのは一度だけです。環境変数に保管してください。紛失した場合や漏えいが疑われる場合は、POST /v1/brands/{brandId}/webhooks/{whkId}/rotate-secret でローテーションすると、新しいシークレットが返されます。

URLに localhost やプライベートネットワークのアドレスは指定できません。また、Replifierはリダイレクトを追跡しません。

すべての配信は、JSON本文と次のヘッダーを持つ POST です。

ヘッダー 内容
X-Replifier-Signature 署名。sha256=<16進数> の形式です。
X-Replifier-Event イベントの種類(例: message.sent)。
X-Replifier-Delivery 配信の一意の識別子。重複を無視するために使います。

5秒以内に2xxのコードで応答してください。それ以外は失敗として扱われます。Replifierは最大6回再試行し、前回の試行から1分後、5分後、30分後、2時間後、8時間後、24時間後に行います。再試行によって配信が重複することがあるため、各 X-Replifier-Delivery は一度だけ処理してください。配信は GET /v1/brands/{brandId}/webhooks/{whkId}/deliveries で確認でき、POST …/deliveries/{wdlId}/redeliver で再送できます。

署名は、リクエストの生の本文に対して、シークレットをキーにしたHMAC-SHA256を16進数で表したもので、先頭に sha256= が付きます。よくある間違いを避けるための3つのルールがあります。

  1. 生の本文に署名します。 届いたままの状態で使ってください。オブジェクトに解析してから再びシリアライズすると、空白や順序が変わり、署名が一致しなくなります。
  2. 一定時間で比較します。 == ではなく、お使いの言語が提供する関数を使ってください。処理時間の計測から署名を推測されるのを防げます。
  3. 署名が一致しない場合は401を返し、何も処理しません。
import { createHmac, timingSafeEqual } from 'node:crypto'
import express from 'express'
const app = express()
// express.raw keeps the body exactly as it arrived.
app.post('/hooks/replifier', express.raw({ type: 'application/json' }), (req, res) => {
const expected = 'sha256=' + createHmac('sha256', process.env.REPLIFIER_WEBHOOK_SECRET).update(req.body).digest('hex')
const received = req.get('X-Replifier-Signature') ?? ''
const ok = received.length === expected.length && timingSafeEqual(Buffer.from(received), Buffer.from(expected))
if (!ok) return res.sendStatus(401)
const event = JSON.parse(req.body.toString('utf8'))
console.log(req.get('X-Replifier-Event'), req.get('X-Replifier-Delivery'), event)
res.sendStatus(200)
})
app.listen(3000)
<?php
$raw = file_get_contents('php://input'); // the raw body
$expected = 'sha256=' . hash_hmac('sha256', $raw, getenv('REPLIFIER_WEBHOOK_SECRET'));
$received = $_SERVER['HTTP_X_REPLIFIER_SIGNATURE'] ?? '';
if (!hash_equals($expected, $received)) {
http_response_code(401);
exit;
}
$event = json_decode($raw, true);
error_log($_SERVER['HTTP_X_REPLIFIER_EVENT'] . ' ' . $_SERVER['HTTP_X_REPLIFIER_DELIVERY']);
http_response_code(200);
import hashlib
import hmac
import os
from flask import Flask, abort, request
app = Flask(__name__)
@app.post("/hooks/replifier")
def replifier_hook():
raw = request.get_data() # the raw body
expected = "sha256=" + hmac.new(os.environ["REPLIFIER_WEBHOOK_SECRET"].encode(), raw, hashlib.sha256).hexdigest()
received = request.headers.get("X-Replifier-Signature", "")
if not hmac.compare_digest(expected, received):
abort(401)
event = request.get_json()
print(request.headers["X-Replifier-Event"], request.headers["X-Replifier-Delivery"], event)
return "", 200

実際の配信を待たずにコードを確認するには、openssl でテスト用の本文に署名し、サーバーが計算した値と比較します。

ターミナルウィンドウ
BODY='{"hello":"world"}'
printf 'sha256=%s\n' "$(printf '%s' "$BODY" | openssl dgst -sha256 -hmac "$REPLIFIER_WEBHOOK_SECRET" -hex | sed 's/^.* //')"
curl -X POST http://localhost:3000/hooks/replifier \
-H "Content-Type: application/json" \
-H "X-Replifier-Signature: sha256=<the signature above>" \
-d "$BODY"

各配信の本文はイベントによって異なります。以下にグループごとのフィールドを示します。将来のバージョンでフィールドが増えることがあるため、知らないフィールドは無視してください。任意のイベントの正確なサンプルは GET /v1/hooks/samples/{event} で取得できます。

メール、コンタクト、キャンペーン、フロー

Section titled “メール、コンタクト、キャンペーン、フロー”

これらのイベントは、event フィールドと表のフィールドを持つフラットな JSON です。id は、イベントに応じて送信、コンタクト、キャンペーンのいずれかの識別子です。

イベント フィールド
message.sent, message.failed id, brandId
contact.created, contact.updated id, brandId
contact.unsubscribed contactId, listId, brandId
campaign.started, campaign.sent id
campaign.failed id, reason
flow.email_sent flowId, nodeId, contactId, sendId, campaignId

whatsapp.message.received、whatsapp.message.status、whatsapp.template.status は、受信メッセージ、配信ステータス、テンプレートの審査を通知します。フィールドは WhatsApp API リファレンス で説明しています。

メッセージ、クリック、コンバージョン

Section titled “メッセージ、クリック、コンバージョン”

message.received、message.delivered、message.read、message.replied、link.clicked、conversion.recorded は、id、type(イベント名と同じ)、created_at、data を持つ共通のエンベロープを使います。たとえばクリックは次のとおりです。

{
"id": "evt_…",
"type": "link.clicked",
"created_at": "2026-01-01T00:00:00.000Z",
"data": {
"message_id": "ccm_…",
"contact_id": "ctc_…",
"brand_id": "brd_…",
"campaign_id": "cmp_…",
"channel": "email",
"url": "https://example.com"
}
}

data には識別子だけが入り、メッセージの内容は含まれません。message_id、contact_id、brand_id に加え、該当する場合は campaign_id と channel(たとえば whatsapp や email)が入ります。値のないキーは省略されます。link.clicked は url を、conversion.recorded は amount、currency、order_id を追加します。message.delivered と message.read は WhatsApp と SMS のメッセージで送られます。

エンドポイントが、シークレットで署名された配信には200で応答し、本文または署名を1文字でも変えると401で応答し、各 X-Replifier-Delivery を一度だけ処理している状態です。