如何为 SMSPool 设置 Webhook
Webhook 会在每条短信到达的那一刻将其推送到您的服务器,因此您无需反复轮询 API 来查看验证码。本指南介绍如何设置 Webhook、我们会发送哪些内容、如何验证请求确实来自 SMSPool,以及当您的服务器没有响应时会发生什么。
Webhook 可以发送到哪里
- 您自己的服务器:每个事件都会发送一个带签名的 JSON
POST请求,详见下文。 - Discord(
https://discord.com/api/webhooks/...):每条短信发送一条聊天消息,显示验证码和完整短信内容。 - Slack(
https://hooks.slack.com/...):每条短信发送一条聊天消息。
使用 Discord 和 Slack 时,您无需编写任何代码。本指南其余部分针对您自己的服务器。
设置 Webhook
- 前往设置,打开开发者选项卡。
- 在 Webhook 下输入您的 URL 和账户密码,然后点击更新 Webhook。
- 将签名密钥(以
whsec_开头)复制到服务器配置中。验证请求时需要用到它。 - 点击发送测试事件。结果会显示在同一页面的 Webhook 投递记录下。
您的 URL 必须:
- 以
https://开头,且长度不超过 255 个字符; - 可从互联网访问。私有地址、本地地址及其他保留地址在保存 URL 时以及每次发送请求前都会被拒绝;
- 由该地址直接响应。我们不会跟随重定向,因此
301或302会被视为失败。
之后更改 URL 时,签名密钥保持不变。
我们发送的内容
每个请求都是带有 JSON 正文的 POST 请求,并包含以下请求头:
| 请求头 | 值 |
|---|---|
Content-Type | application/json |
User-Agent | SMSPool-Webhook/2.0 |
X-SMSPool-Event | 事件类型,例如 order.code |
X-SMSPool-Event-Id | 每个事件唯一。同一事件重试时使用相同的 ID |
X-SMSPool-Delivery-Attempt | 首次尝试为 1,最多到 4 |
X-SMSPool-Signature | t=<unix time>,v1=<signature>,参见下文“验证请求是否来自 SMSPool” |
X-SMSPool-Secret | 您的签名密钥,仅在开启简易验证时发送(见下文) |
order.code:订单收到短信
{
"event": "order.code",
"event_id": "evt_6f1c0e9a2b4d8f7e1a3c5b90",
"orderid": "AB12CD34",
"sms": "123456",
"full_sms": "Your verification code is 123456",
"type": "short_term",
"order_code": "AB12CD34",
"status": "completed",
"code": "123456",
"full_code": "Your verification code is 123456",
"phonenumber": "14155550123",
"timestamp": "2026-10-18 02:15:09pm",
"unix_timestamp": 1792332909
}新的集成应读取 order_code、code、full_code 和 unix_timestamp。为兼容旧版集成,orderid、sms、full_sms、type 和 timestamp 字段仍会原样发送。timestamp 采用服务器时间且不含时区,因此建议使用 unix_timestamp。
如果一个订单收到多条短信(例如重新发送后),每条短信都会发送一个 order.code 事件。
order.status:订单未收到验证码即结束(可选)
在开发者选项卡中开启同时发送 order.status 事件即可接收此类事件。
{
"event": "order.status",
"event_id": "evt_0b7d2c4e6a8f1e3d5c7b9a01",
"order_code": "AB12CD34",
"orderid": "AB12CD34",
"status": "refunded",
"phonenumber": "14155550123",
"timestamp": "2026-10-18 02:35:12pm",
"unix_timestamp": 1792334112
}status 的值为 expired、refunded 或 cancelled。订单先过期后退款时会发送两个事件。已完成的订单不会发送 order.status 事件,因为 order.code 事件已经告知了您。
rental.sms:租用号码收到短信
{
"event": "rental.sms",
"event_id": "evt_3a5c7e9b1d2f4a6c8e0b2d4f",
"phonenumber": "14155550123",
"rental_code": "R1A2B3C4",
"full_sms": "Your code is 998877",
"timestamp": "2026-10-18 02:15:09pm",
"unix_timestamp": 1792332909
}rental.auto_extend:租用号码已自动续期或续期失败
{
"event": "rental.auto_extend",
"event_id": "evt_9e8d7c6b5a4f3e2d1c0b9a8f",
"rental_code": "R1A2B3C4",
"phonenumber": "14155550123",
"success": 1,
"timestamp": "2026-10-18 02:15:09pm",
"unix_timestamp": 1792332909
}续期成功时 success 为 1,续期失败时为 0。
webhook.test:由“发送测试事件”按钮发送
{
"event": "webhook.test",
"event_id": "evt_1f2e3d4c5b6a7f8e9d0c1b2a",
"message": "This is a test event from SMSPool.",
"timestamp": "2026-10-18 02:15:09pm",
"unix_timestamp": 1792332909
}快速响应
请在 5 秒内(连接时间 3 秒)返回任意 2xx 状态码。如果处理耗时较长,请先响应,再进行处理。
其他任何情况(例如返回其他状态码、超时或连接被拒绝)都会在 15 秒、45 秒和 90 秒后重试。每个事件在约 3 分钟内最多尝试 4 次,之后将被丢弃,因为订单在购买几分钟后就会过期,过晚送达的验证码已无用处。如果您的服务器可能宕机更长时间,请同时通过 API 查询订单作为备用方案。
由于存在重试,同一事件可能会多次送达,事件也可能不按顺序到达。请使用 X-SMSPool-Event-Id(或正文中的 event_id)跳过已处理过的事件。
开发者选项卡中的 Webhook 投递记录会列出最近 25 次尝试及其结果。记录保留 30 天。
验证请求是否来自 SMSPool
任何知道您 URL 的人都可以向其发送请求。在信任请求之前,请先验证签名。
X-SMSPool-Signature 请求头格式如下:
X-SMSPool-Signature: t=1792843200,v1=268b68d207c255da63fcf785e80376ebc3699018a56f3a10b62a9d9fee6be899t是请求签名时的 Unix 时间(以秒为单位)。v1是 HMAC-SHA256 值,以小写十六进制表示,不带sha256=前缀。- 签名文本为时间戳、一个英文句点,再加上原始请求正文:
<t>.<raw body>。 - 密钥就是设置中显示的签名密钥,需完全一致,包括
whsec_前缀。请勿对其进行解码。
验证请求的步骤:
- 按
,拆分请求头,得到t和v1。 - 以您的密钥为键,计算
<t>.<raw body>的 HMAC-SHA256,并以十六进制表示。 - 使用恒定时间比较将结果与
v1进行比较。 - 如果
t与服务器时钟相差超过 5 分钟,则拒绝该请求,以防旧请求被重放。
请使用收到的原始正文。如果您先解析 JSON 再重新编码,字节会发生变化,签名将无法匹配。
示例
可使用此示例测试您的代码。其中的密钥为虚构值。
- 密钥:
whsec_0123456789abcdef0123456789abcdef0123456789abcdef t:1792843200- 原始正文:
{"event":"webhook.test","event_id":"evt_0000000000000000000000aa"} - 签名文本:
1792843200.{"event":"webhook.test","event_id":"evt_0000000000000000000000aa"} - 预期
v1:268b68d207c255da63fcf785e80376ebc3699018a56f3a10b62a9d9fee6be899
PHP
<?php
$secret = getenv('SMSPOOL_WEBHOOK_SECRET'); // whsec_..., exactly as shown in Settings
$body = file_get_contents('php://input'); // the raw body, before any JSON parsing
$header = $_SERVER['HTTP_X_SMSPOOL_SIGNATURE'] ?? '';
parse_str(str_replace(',', '&', $header), $parts);
$timestamp = (int) ($parts['t'] ?? 0);
$expected = hash_hmac('sha256', $timestamp . '.' . $body, $secret);
if (!hash_equals($expected, $parts['v1'] ?? '') || abs(time() - $timestamp) > 300) {
http_response_code(401);
exit;
}
http_response_code(200);
$event = json_decode($body, true);
// Skip $event['event_id'] values you have already handled, then process the event.Node.js (Express)
const crypto = require('crypto');
const express = require('express');
const app = express();
// express.raw keeps the body as bytes, so the signature can be checked.
app.post('/smspool-webhook', express.raw({ type: 'application/json' }), (req, res) => {
const header = req.get('X-SMSPool-Signature') || '';
const parts = Object.fromEntries(header.split(',').map((p) => p.split('=')));
const expected = crypto
.createHmac('sha256', process.env.SMSPOOL_WEBHOOK_SECRET)
.update(`${parts.t}.${req.body}`)
.digest('hex');
const valid =
parts.v1 &&
parts.v1.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(parts.v1), Buffer.from(expected)) &&
Math.abs(Date.now() / 1000 - Number(parts.t)) <= 300;
if (!valid) return res.sendStatus(401);
res.sendStatus(200);
const event = JSON.parse(req.body);
// Skip event.event_id values you have already handled, then process the event.
});Python (Flask)
import hashlib, hmac, os, time
from flask import Flask, request, abort
app = Flask(__name__)
SECRET = os.environ["SMSPOOL_WEBHOOK_SECRET"].encode()
@app.post("/smspool-webhook")
def smspool_webhook():
header = request.headers.get("X-SMSPool-Signature", "")
parts = dict(p.split("=", 1) for p in header.split(",") if "=" in p)
body = request.get_data() # raw bytes
expected = hmac.new(SECRET, parts.get("t", "").encode() b"." body, hashlib.sha256).hexdigest()
if not hmac.compare_digest(expected, parts.get("v1", "")) or abs(time.time() - int(parts.get("t", 0))) > 300:
abort(401)
event = request.get_json()
# Skip event["event_id"] values you have already handled, then process the event.
return "", 200简易验证
如果您不想计算 HMAC,可以在开发者选项卡中签名密钥下方开启简易验证。之后,发送到您 https:// URL 的每个请求都会在 X-SMSPool-Secret 请求头中附带您的密钥(与设置中显示的完全一致)。您的服务器只需将其与密钥进行比较:
if (!hash_equals(getenv('SMSPOOL_WEBHOOK_SECRET'), $_SERVER['HTTP_X_SMSPOOL_SECRET'] ?? '')) {
http_response_code(401);
exit;
}这种方式的安全性低于签名验证。任何会记录请求头的组件(例如代理或错误跟踪工具)都会获取到您的密钥,而且被截获的请求可以被重新发送。X-SMSPool-Signature 仍会照常发送,因此您可以随时切换到完整验证。
点击生成新密钥后,旧密钥会立即失效。请立即更新您的服务器配置。
故障排除
- 签名始终不匹配。请确保您对原始正文进行签名,而不是解析后重新编码的 JSON。请确保包含
t.前缀,并使用完整的密钥(包括whsec_)。然后将您的代码与上面的示例进行对照。 - 什么都没收到。请在开发者选项卡的 Webhook 投递记录中查看错误信息。常见原因包括防火墙拦截了我们的请求、存在重定向,或响应时间超过 5 秒。
- Webhook 已被关闭。每丢弃一个事件,您都会收到一条站内通知。丢弃 100 个事件后,Webhook 将被关闭。请先解决问题,然后重新保存您的 URL 以重新启用。
仍有问题?请联系客服,并提供投递失败的时间。
返回知识库