00 / QUICKSTART
快速开始
在客户门户创建账号、验证发件域名、创建 API Key,然后发送第一封邮件。API Key 只在创建时显示一次,请保存到服务端环境变量,不要写入前端代码。
curl -X POST "https://mail.dotbright.cn/v1/messages" \
-H "Authorization: Bearer msk_xxx" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-20261202-001" \
-d '{
"from": "noreply@your-domain.com",
"to": ["user@example.com"],
"subject": "你的订单已发货",
"text": "订单已发货,点击查看详情。",
"html": "<p>订单已发货,点击查看详情。</p>"
}'domain_not_verified。先用控制台添加域名并完成 SPF/DKIM/DMARC。01 / AUTH
鉴权与通用约定
客户 API 使用 Bearer API Key。管理 API 使用平台 Admin Token,不应暴露给客户端或浏览器。
| 约定 | 说明 |
|---|---|
| 编码 | UTF-8;请求与响应均为 application/json |
| 时间 | ISO 8601 / RFC 3339,示例为 UTC |
| 金额 | 统一使用整数「分」,例如 1500 表示 ¥15.00 |
| 幂等 | 发送接口支持 Idempotency-Key;同 Key 不同 body 返回 409 |
| 限流 | 返回 429,并带 Retry-After 秒数 |
| 错误 | 统一为 {"error":{"code","message","details"}} |
幂等重放
同一租户、同一 Idempotency-Key 且 body 完全一致时,不会重复创建消息;平台返回第一次的响应,并带 Idempotent-Replayed: true。请求仍在处理中时返回 409 idempotency_in_progress。
02 / SEND
发送邮件
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
from | string | 是 | 发件地址;域名必须已验证,例如 noreply@acme.com |
to | string[] | 是 | 1–50 个收件人;自动去重并转小写 |
subject | string | 条件 | 主题;使用模板时可省略;不超过 998 字节,禁止换行 |
html | string | 条件 | HTML 正文;与 text 至少一个 |
text | string | 条件 | 纯文本正文;与 html 至少一个 |
reply_to | string | 否 | 回复地址 |
template | object | 否 | {"id":"...","version":1,"data":{"name":"..."}};与 subject/html/text 互斥 |
headers | object | 否 | 仅允许 X-* 自定义头,禁止换行注入 |
metadata | object | 否 | 业务扩展数据,原样保存在消息记录 |
tags | string[] | 否 | 业务标签 |
attachments | object[] | 否 | 最多 20 个,总大小 ≤ 8MB;filename、content_type、content_base64 |
{
"id": "01a2c9e4-...",
"message_id": "<01a2c9e4-...@your-domain.com>",
"status": "queued",
"recipients": [
{"address": "user@example.com", "status": "queued"}
]
}如果所有收件人都在抑制列表中,消息状态为 suppressed,不产生实际投递;部分抑制时,该收件人的 status 为 suppressed,其余正常进入队列。
03 / TEMPLATE
模板与附件
使用已发布模板
{
"from": "noreply@your-domain.com",
"to": ["user@example.com"],
"template": {
"id": "01a2c9e4-...",
"version": 2,
"data": {"name": "张三", "order_id": "A1024"}
}
}模板主体使用 Handlebars 变量。模板必须已发布;不传 version 时使用当前版本。变量缺失或 JSON 无效会返回 422,不会静默替换为空。
附件限制
- 最多 20 个附件;解码后总大小不超过 8MB;请求体最大 16MB。
content_base64必须是标准 Base64;文件名不能包含换行。content_type可选;非法 MIME 类型返回 422。
04 / MESSAGE
查询消息
返回消息状态、收件人状态和事件时间线。只能查询当前 API Key 所属租户的消息。
{
"id": "01a2c9e4-...",
"message_id": "<01a2c9e4-...@your-domain.com>",
"from": "noreply@your-domain.com",
"subject": "你的订单已发货",
"status": "handed_off",
"created_at": "2026-12-02T13:49:30Z",
"recipients": [{"address": "user@example.com", "status": "handed_off"}],
"events": [
{"type": "message.accepted", "recipient": "user@example.com", "occurred_at": "..."},
{"type": "message.handed_off", "recipient": "user@example.com", "occurred_at": "..."}
]
}常见消息状态:queued、handed_off、delivered、deferred、bounced、failed、suppressed。最终 delivered 依赖 MTA/relay 的投递回执。
05 / ACCOUNT
账户与用量
{
"balance_units": 9800,
"used_today": 200,
"billing_mode": "wallet"
}balance_units 为当前可用封数;billing_mode 为 shadow(只计量)或 wallet(受理时扣减)。
06 / ERRORS
错误码
{
"error": {
"code": "validation_error",
"message": "to must contain 1..50 addresses",
"details": null
}
}| HTTP | code | 处理建议 |
|---|---|---|
| 400 | invalid_request / invalid_json | 检查请求格式、JSON 和 Content-Type |
| 401 | invalid_api_key | Key 生成、复制或已吊销 |
| 402 | insufficient_units | 钱包余额不足,请联系平台 |
| 403 | domain_not_verified / tenant_suspended | 验证发件域名或联系平台 |
| 404 | not_found | 资源不存在或不属于当前租户 |
| 409 | idempotency_conflict / idempotency_in_progress | 同 Key 不同 body 或上次仍在处理 |
| 422 | validation_error / template_* | 按 message/details 修正字段 |
| 429 | rate_limited | 读取 Retry-After 后重试 |
| 500 | internal_error | 记录请求 ID 并联系支持 |