NotifyHub API 文档

NotifyHub 是一站式消息通知服务平台,提供统一的消息通知接口,支持短信、邮件、Push推送、语音等多种通知渠道。本文档涵盖所有 API 接口的详细说明。

快速开始

NotifyHub 提供统一的消息通知接口,支持短信、邮件、Push推送、语音等多种通知渠道。以下以短信API为例快速上手。

准备工作

  1. 获取 api-token:联系接口人获取,使用时将 YOUR_API_TOKEN 替换为您的实际 Token。
  2. 安装 cURL工具:确保您的系统已安装 curl 命令行工具。

步骤1:发送请求

curl -X POST https://api.notifyhub.example.com/v1/sms/send \
  -H "api-token: YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "to": "+8613800138000",
    "content": "您的验证码是:123456,5分钟内有效。"
}'

步骤2:处理响应

{
  "success": true,
  "result": {
    "message_id": "msg_abc123456",
    "status": "queued"
  },
  "errors": []
}

身份认证

NotifyHub API 使用 api-token 进行身份认证。您需要在每个请求的 Header 中包含 api-token。

curl -X POST https://api.notifyhub.example.com/v1/sms/send \
  -H "api-token: YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"to": "+8613800138000", "content": "您的验证码是:123456"}'

1. sms/send - 发送短信

向指定手机号发送短信通知,支持单发和批量发送。

POST /v1/sms/send

参数说明

参数名类型是否必填说明
tostring 或 Array<string>接收方手机号,支持国际格式如 +8613800138000,批量发送时传入数组,最多 1000 个
contentstring短信内容,与 template_id 二选一
template_idstring短信模板ID,与 content 二选一
template_paramsRecord<string, string>模板变量,当使用 template_id 时传入对应的模板参数
channelstring指定短信通道,可选值:"default"(自动选择)、"marketing"(营销)、"transactional"(事务型)
callback_urlstring状态回调地址,消息状态变更时主动推送
extraRecord<string, string>自定义扩展字段,会原样透传到回调通知中

2. email/send - 发送邮件

向指定邮箱地址发送邮件通知,支持 HTML 富文本和纯文本格式。

POST /v1/email/send

参数说明

参数类型是否必填说明
tostring 或 Array<string>收件人邮箱地址,支持批量发送,最多 100 个
subjectstring邮件主题
htmlstringHTML 格式邮件正文,与 text 至少填写一个
textstring纯文本格式邮件正文,与 html 至少填写一个
from_namestring发件人显示名称,默认值 "NotifyHub"
reply_tostring回复邮箱地址
ccArray<string>抄送邮箱列表
bccArray<string>密送邮箱列表
callback_urlstring状态回调地址

3. push/send - 发送Push通知

向指定设备或用户发送 App Push 推送通知,支持 iOS、Android 及鸿蒙平台。

POST /v1/push/send

参数说明

参数类型是否必填说明
tostring 或 Array<string>目标用户ID或设备Token,支持批量,最多 1000 个
titlestring推送标题
bodystring推送正文内容
platformstring目标平台,可选值:"all"、"ios"、"android"、"harmony",默认 "all"
dataRecord<string, string>自定义业务数据,随推送一起下发到客户端
push_options.badgeintiOS 角标数字
push_options.soundstring通知声音,"default" 为系统默认声音,"none" 为静音
push_options.ttlint消息存活时长(秒),超时未送达则丢弃,默认 86400

4. voice/call - 发送语音通知

向指定手机号发起语音电话通知,支持文本转语音(TTS)和预录音文件播报。

POST /v1/voice/call

参数说明

参数类型是否必填说明
tostring接收方手机号,支持国际格式如 +8613800138000
tts_textstringTTS 文本转语音内容,与 audio_url 二选一
audio_urlstring预录音频文件URL(支持 mp3/wav),与 tts_text 二选一
repeatint语音播报重复次数,取值范围 1-5,默认 1
tts_options.voicestring语音类型,可选值:"female"(女声)、"male"(男声)
tts_options.speedfloat语速倍率,取值范围 0.5-2.0,默认 1.0
tts_options.volumeint音量大小,取值范围 0-10,默认 5

5. batch/send - 批量多渠道发送

在一次请求中同时向多个渠道(短信、邮件、Push)发送通知,支持按优先级自动降级。

POST /v1/batch/send

参数说明

参数类型是否必填说明
channelsArray<string>要发送的渠道列表,按优先级排序,可选值:"sms"、"email"、"push"、"voice"
fallbackbool是否启用降级:true 时若高优先级渠道送达成功则跳过低优先级渠道,默认 false
smsObject短信发送参数,结构同 /v1/sms/send(channels 包含 "sms" 时必填)
emailObject邮件发送参数,结构同 /v1/email/send(channels 包含 "email" 时必填)
pushObjectPush 发送参数,结构同 /v1/push/send(channels 包含 "push" 时必填)
voiceObject语音发送参数,结构同 /v1/voice/call(channels 包含 "voice" 时必填)

6. message/status - 查询消息状态

根据消息ID查询消息的发送状态,支持查询短信、邮件、Push及语音的发送详情。

POST /v1/message/status

参数说明

参数类型是否必填说明
message_idstring消息唯一ID,由发送接口返回
channelstring消息渠道,可选值:"sms"、"email"、"push"、"voice"。不填则自动识别

消息状态说明:queued(排队中)、sent(已发送)、delivered(已送达)、failed(发送失败)、skipped(已跳过)

7. smart/notify - AI 智能通知(内测中)

根据用户画像和历史行为,由 AI 自动选择最佳通知渠道、时机和内容,提升通知触达率和转化率。

POST /v1/smart/notify

参数说明

参数名类型是否必填说明
user_idstring目标用户ID,用于 AI 读取用户画像和历史行为
intentstring通知意图描述,AI 根据此文本自动生成最适合的通知内容
prioritystring通知优先级,可选值:"low"、"normal"、"high"、"urgent"。urgent 级别将忽略免打扰设置,默认 "normal"
user_preference.preferred_channelsArray<string>偏好渠道列表,按优先级排序
user_preference.quiet_hoursstruct免打扰时段设置,包含 start、end、timezone 字段
user_preference.languagestring通知语言,影响 AI 内容生成,默认 "zh-CN"

8. template/list - 消息模板管理

创建、查询和管理消息模板,统一维护各渠道的通知内容。

POST /v1/template/list

参数说明

参数类型是否必填说明
channelstring按渠道筛选,可选值:"sms"、"email"、"push"、"voice"。不填则返回全部
statusstring按状态筛选,可选值:"pending"(审核中)、"approved"(已通过)、"rejected"(已拒绝)
keywordstring按模板名称或ID关键词搜索
pageint页码,从 1 开始,默认 1
page_sizeint每页数量,最大值 100,默认 20

错误处理

NotifyHub API 使用标准的HTTP状态码来表示请求结果,所有响应都遵循统一的格式规范。

HTTP 状态码含义说明
200请求成功success 为 true 表示发送成功,errors 为空数组
400参数错误请求参数缺失或格式不正确,errors 中包含具体错误信息
401认证失败api-token 无效或已过期,请检查 Header 中的 api-token
422发送失败参数格式正确但发送失败,如手机号格式不正确
429请求限流请求频率超限,当前限制:100次/分钟,请稍后重试

Serverless 接入

通过 Serverless 函数调用 NotifyHub,支持 sms.send()、email.send()、push.send()、voice.call()、batch.send() 等方法。

addEventListener('fetch', async (event) => {
    const result = await NotifyHub.sms.send({
        to: "+8613800138000",
        template_id: "tpl_verify_code",
        template_params: { code: "123456", expire: "5" }
    });
    event.respondWith(new Response(JSON.stringify(result.data), { status: 200 }));
});

Webhook 接入

NotifyHub 支持通过 Webhook 方式接入,当消息状态发生变更(送达、失败、已读等)时,系统将主动向您配置的地址推送回调通知。

在发送接口中通过 callback_url 参数指定回调地址。每次回调请求的 Header 中会携带 X-NotifyHub-Signature 签名字段,用于验证回调来源。

SDK 接入

NotifyHub 提供多语言官方 SDK,封装了鉴权、重试、错误处理等通用逻辑。

安装

# Python
pip install notifyhub-sdk

# Node.js
npm install @notifyhub/sdk

# Go
go get github.com/notifyhub/notifyhub-go

Python 示例

from notifyhub import NotifyHubClient

client = NotifyHubClient(api_token="YOUR_API_TOKEN")

# 发送短信
result = client.sms.send(
    to="+8613800138000",
    template_id="tpl_verify_code",
    template_params={"code": "123456", "expire": "5"}
)

# 发送邮件
result = client.email.send(
    to="user@example.com",
    subject="您的订单已发货",
    html="<p>您的订单已发货,请注意查收。</p>"
)

Node.js 示例

const { NotifyHubClient } = require('@notifyhub/sdk');

const client = new NotifyHubClient({ apiToken: 'YOUR_API_TOKEN' });

// 发送短信
const result = await client.sms.send({
  to: '+8613800138000',
  templateId: 'tpl_verify_code',
  templateParams: { code: '123456', expire: '5' }
});

// 发送 Push
const pushResult = await client.push.send({
  to: 'user_id_12345',
  title: '您有一条新消息',
  body: '点击查看详情'
});