跳到正文

API

从您的应用中退订

从您的服务器为联系人办理退订,或在您的应用中提供一个使用退订链接签名令牌的偏好设置页面;自定义默认偏好设置页面。

Voaaro 已经为每封营销邮件处理好了退订:带链接的页脚、一键生效的 List-Unsubscribe 邮件头,以及一个托管的偏好设置页面。本页介绍如何在您自己这边实现退订:从您的服务器,或从您应用中的某个页面。

从您的服务器

使用与订阅相同的密钥:域名的应用密钥,sending_access 即可。

Node / Bun

const { data } = await voaaro.newsletter.unsubscribe({ email: "ada@example.org" })

Python

r = voaaro.Newsletter.unsubscribe({"email": "ada@example.org"})

PHP

$r = $voaaro->newsletter->unsubscribe(['email' => 'ada@example.org']);

Go

sub, err := client.Newsletter.Unsubscribe(&voaaro.UnsubscribeRequest{Email: "ada@example.org"})

cURL

curl https://mail.example.com/api/v1/newsletter/unsubscribe \
  -H "Authorization: Bearer $VOAARO_API_KEY" -H "Content-Type: application/json" \
  -d '{"email": "ada@example.org"}'
JSON
{
  "object": "subscription",
  "email": "ada@example.org",
  "contact_id": "7c1d9e42-0a3b-4f6e-8d21-5b9f0c3e7a14",
  "status": "unsubscribed",
  "topics": [{ "id": "7a1c…", "name": "News", "subscription": "opt_in" }]
}
  • email 或 contact_id:必填;地址未知时返回 404。如果密钥覆盖多个域名,请提供 domain。
  • 不带 topic_ids:退订该域名的所有营销邮件;该联系人已在队列中的邮件会被取消。会产生 webhook contact.unsubscribed(source: "api")。
  • 带 topic_ids:只有这些主题变为 opt_out;联系人仍订阅其他营销邮件(status: "subscribed"),不会产生 contact.unsubscribed。
  • 此更改会记入日志(contact.unsubscribe,操作者为 apikey:<id>)。支持 Idempotency-Key。
  • 地址的状态可通过 GET /newsletter/subscriptions/{email} 查询。如需从您的服务器重新订阅,请再次调用 POST /newsletter/subscribe:这属于明确的请求。

您的应用可以拥有自己的“管理我的邮件”页面。该页面使用表单的公钥和相同的允许的来源调用表单的公开路由。

为什么使用令牌而不是地址

如果仅凭地址就能退订,任何人都可以替任何人退订。因此,公开路由要求提供退订链接中的签名令牌,只有邮件的收件人才拥有它。请使用变量 {{{VOAARO_UNSUBSCRIBE_TOKEN}}}(或 {{ unsubscribe_token }})将令牌放入营销邮件的链接中:

HTML
<a href="https://app.example.com/unsubscribe?token={{{VOAARO_UNSUBSCRIBE_TOKEN}}}">Manage my emails</a>

您的页面从其地址中读取 token,并将其传给下面的路由。如果令牌所属的域名与表单的域名不同,则返回 404 invalid_token。

方法路径(/api/v1/newsletter/public/{public_key}/…)请求体响应
GETpreferences?token=…偏好设置
POSTunsubscribetoken、topic_ids(可选)偏好设置
POSTresubscribetoken偏好设置
JSON
{
  "object": "preferences",
  "test": false,
  "email": "a***@example.org",
  "unsubscribed": false,
  "topics": [{ "id": "7a1c…", "name": "News", "description": "Once a month", "subscribed": true }]
}
  • email 经过掩码处理:即使有人拿到了链接,页面也不会暴露完整地址。
  • topics:该域名的公开主题,并标明该联系人是否已订阅。
  • 不带 topic_ids 的 unsubscribe 会退订全部内容(webhook contact.unsubscribed,source: "public_api");带上它则只退订这些主题。resubscribe 会撤销退订。
  • 测试发送的令牌返回 test: true,不会更改任何内容。
  • GET 从不更改任何内容:会打开链接地址的杀毒软件和链接预览不会让任何人退订。
HTML
<p id="prefs">Loading…</p>
<button id="stop">Unsubscribe me from everything</button>

<script type="module">
  const API = "https://mail.example.com/api/v1/newsletter/public/mlk_pub_0123456789abcdef0123456789abcdef"
  const token = new URLSearchParams(location.search).get("token")
  const show = (p) => (document.getElementById("prefs").textContent = p.unsubscribed ? `${p.email}: unsubscribed.` : `${p.email}: subscribed.`)

  fetch(`${API}/preferences?token=${encodeURIComponent(token)}`).then((r) => r.json()).then(show)
  document.getElementById("stop").addEventListener("click", async () => {
    const r = await fetch(`${API}/unsubscribe`, { method: "POST", headers: { "Content-Type": "text/plain" }, body: JSON.stringify({ token }) })
    show(await r.json())
  })
</script>

各 SDK 提供相同的调用:PublicForm 的 getPreferences(token)、unsubscribe(token, { topicIds })、resubscribe(token)(Node;Python 中为 get_preferences、unsubscribe、resubscribe,PHP 和 Go 中名称相同)。

默认偏好设置页面

Voaaro 托管的页面(链接 {{{unsubscribe_url}}}、{{ preferences_url }})可以按域名自定义:在控制台中(新闻通讯 › 新闻通讯设置,“退订与偏好页面”),或通过 PATCH /api/v1/newsletter/settings 的 preferences_page 设置:

字段规则
title最多 150 个字符
intro最多 1000 个字符;{email} 会替换为掩码处理后的地址
done_message最多 500 个字符,在退订后显示
languagefr、en、es、it、ru、zh-CN、de;未设置时使用浏览器的语言
accent_color、background_color#rrggbb
image_url与表单相同:https:// 地址或嵌入的图片,不接受 SVG
cURL
curl -X PATCH https://mail.example.com/api/v1/newsletter/settings \
  -H "Authorization: Bearer $VOAARO_API_KEY" -H "Content-Type: application/json" \
  -d '{"preferences_page": {"title": "Your Acme emails", "intro": "Choose what {email} receives.", "accent_color": "#d9480f"}}'

该对象会替换之前的设置;{} 或 null 会恢复原始页面(文字已翻译为七种语言)。可以通过 GET /api/v1/newsletter/settings 读取该设置。工作方式不变:GET 不会更改任何内容,List-Unsubscribe 一键生效。

使用 { preferences_page?, page: "form" | "done" } 调用 POST /api/v1/newsletter/preferences-page/preview,会针对一个示例联系人和该域名的公开主题渲染页面,但不保存任何内容:{ object: "preferences_page_preview", html }。

Esc