Replifier webhook 与签名验证
出站 webhook 会在 Replifier 中发生事件时通知你的系统,你无需轮询 API。每次推送都使用只有 Replifier 和你知道的密钥进行签名:在信任内容之前先验证签名,因为任何知道你的 URL 的人都可能向你发送伪造的数据。
注册 webhook
Section titled “注册 webhook”使用具有 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 不会跟随重定向。
你的服务器会收到什么
Section titled “你的服务器会收到什么”每次推送都是一个带有 JSON 请求体的 POST,并包含以下请求头:
| 请求头 | 内容 |
|---|---|
X-Replifier-Signature |
签名,格式为 sha256=<hexadecimal>。 |
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 重新发送其中一条。
签名如何计算
Section titled “签名如何计算”签名是对请求的原始请求体计算的 HMAC-SHA256(十六进制),以你的密钥作为密钥,并在前面加上 sha256=。三条规则可以避免最常见的错误:
- 对原始请求体签名,保持其到达时的原样。如果你把它解析成对象再重新序列化,空格和顺序都会改变,签名就不再匹配。
- 以恒定时间比较,使用你所用语言提供的函数,而不是
==,这样就没有人能通过测量耗时来推断出签名。 - 如果签名不匹配,返回 401 且不处理任何内容。
Node.js
Section titled “Node.js”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);Python
Section titled “Python”import hashlibimport hmacimport 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手动测试签名
Section titled “手动测试签名”如果想在不等待真实推送的情况下检查你的代码,可以用 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"事件及其内容
Section titled “事件及其内容”每次推送的正文取决于事件。下面列出各组的字段;后续版本可能增加字段,因此请忽略你不认识的字段。要查看任一事件的确切示例,请调用 GET /v1/hooks/samples/{event}。
邮件、联系人、活动和流程
Section titled “邮件、联系人、活动和流程”这些事件是扁平的 JSON,包含 event 字段和表中的字段。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 和短信消息。
你的端点对使用你的密钥签名的推送返回 200;只要你改动请求体或签名中的哪怕一个字符,就返回 401;并且对每个 X-Replifier-Delivery 只处理一次。