跳到正文

SDK

Node 和 Bun SDK

@deveryidea/voaaro 0.6.0:TypeScript,支持 ESM 和 CommonJS,无任何依赖,兼容 Bun,内置重试和分页。

安装

Shell
npm install @deveryidea/voaaro
# or
bun add @deveryidea/voaaro

版本 0.6.0。需要 Node 18 或更高版本(原生 fetch)。也可以在 Bun 上运行(已在 Bun 1.3 上验证:发送邮件和验证 webhook)。

配置

Node / Bun
import { Voaaro } from "@deveryidea/voaaro"

const voaaro = new Voaaro(process.env.VOAARO_API_KEY, {
  baseUrl: "https://mail.example.com",
  maxRetries: 2, // default; 0: no retry and no idempotency key drawn
})

选项:baseUrl、maxRetries、fetch(您自己的 fetch 函数)、headers、sleep(两次尝试之间的等待,用于加快测试)。不传参数时,密钥和地址从 VOAARO_API_KEY 和 VOAARO_BASE_URL 中读取。

发送

Node / Bun
const { data, error } = await voaaro.emails.send(
  {
    from: "Acme <notifications@example.com>",
    to: ["client@example.org"],
    subject: "Welcome",
    html: "<p>Your account is ready.</p>",
    scheduledAt: "in 10 min",
  },
  { idempotencyKey: "welcome-42" },
)
if (error) {
  console.error(error.name, error.message) // e.g. validation_error, daily_quota_exceeded
} else {
  console.log(data.id)
}

字段沿用 Resend 的结构,采用 camelCase 命名(replyTo、scheduledAt、contentType、contentId);附件的 content 可以是 Buffer、Uint8Array、ArrayBuffer 或 base64 字符串。

重试与幂等性

  • 遇到网络错误、429 rate_limit_exceeded 或 500、502、503、504 时,请求会被重试(maxRetries 次,默认 2 次);遇到 daily_quota_exceeded 或其他 4xx 错误时从不重试。等待时间:以 Retry-After 为准(最多 60 秒),否则为 0.5 秒 × 2^n 并加入随机抖动,最多 8 秒。
  • 所有创建操作都接受将 { idempotencyKey } 作为最后一个参数:voaaro.forms.create({ name }, { idempotencyKey: "newsletter-form" })。如果未提供,SDK 会为每次调用生成一个键(UUID v4),并在各次尝试之间保持不变。
  • 当服务返回了 Retry-After 时,会填充 error.retryAfter(秒);响应头(RateLimit-Remaining 等)位于 headers 中。

分页

Node / Bun
for await (const contact of voaaro.contacts.iterate({ segmentId, unsubscribed: false })) {
  console.log(contact.email)
}

// Any list: the function receives { limit, after } to merge with its filters
for await (const e of voaaro.paginate((page) => voaaro.emails.events({ type: "bounced", ...page }), { limit: 50 })) {
  console.log(e.email_id, e.recipient)
}
  • iterate(params) 可用于 emails、emails.receiving、apiKeys、domains、suppressions、contacts、contacts.imports、segments、audiences、topics、contactProperties、broadcasts、forms、newsletterTemplates、automations、webhooks、webhooks.deliveries(webhookId, params);此外还有 emails.iterateEvents、broadcasts.iterateRecipients、forms.iterateSignups、automations.iterateRuns。
  • 每页 100 条(或指定的 limit),只要 has_more 为真就沿着 after 继续;使用 break 即可停止请求。
  • 某一页被拒绝(重试之后)时会抛出 VoaaroApiError(name、message、statusCode、retryAfter):这是 SDK 唯一会抛出异常的情况。

其他功能

Node / Bun
await voaaro.batch.send([{ from, to: "a@example.org", subject, text }, { from, to: "b@example.org", subject, text }])
await voaaro.emails.get(id)
await voaaro.emails.list({ limit: 20 })
await voaaro.emails.update({ id, scheduledAt: "2026-11-20T08:00:00Z" })
await voaaro.emails.cancel(id)
await voaaro.emails.events({ type: "bounced", createdAfter: "2026-11-01T00:00:00Z" })
await voaaro.emails.receiving.list({ limit: 10 }) // personal key with receiving
await voaaro.apiKeys.create({ name: "CI", permission: "sending_access" })
await voaaro.domains.create({ name: "notify.example.com" })
await voaaro.suppressions.add({ email: "former.customer@example.org", domain: "example.com" })
await voaaro.contacts.create({ email: "ada@example.org", firstName: "Ada" })
await voaaro.broadcasts.create({ segmentId, from, subject, html, send: true })

资源:emails(含 attachments 和 receiving)、batch、apiKeys、domains、suppressions、contacts(含 segments、topics、imports)、segments、audiences、topics、contactProperties、broadcasts、newsletter、forms、newsletterTemplates、automations、events、webhooks(含 deliveries)、organizations(运营方密钥 mko_…)。

新闻通讯

Node / Bun
await voaaro.newsletter.updateSettings({ from: "Acme <news@example.com>", postalAddress: "Acme\n12 Lilac Street\nSpringfield" })
const { data: overview } = await voaaro.newsletter.overview({ days: 30 })

const { data: form } = await voaaro.forms.create({ name: "Website newsletter", doubleOptIn: true, segmentIds: [segmentId] })
const { data: preview } = await voaaro.segments.preview({ filter: { match: "all", conditions: [{ field: "opened", op: "none", days: 90 }] } })

await voaaro.broadcasts.links(broadcastId)
await voaaro.broadcasts.chooseWinner(broadcastId, { variant: "b" })
const { data: csv } = await voaaro.broadcasts.export(broadcastId) // CSV text

const { data: template } = await voaaro.newsletterTemplates.create({ name: "Monthly letter", subject: "What's new", html })
await voaaro.automations.create({ name: "After purchase", trigger: { type: "event", event: "purchase" }, steps, status: "active" })
await voaaro.events.send({ email: "ada@example.org", name: "purchase", data: { total: 42 } })
await voaaro.contacts.cleanup({ days: 180 }) // simulation; dryRun: false to act

嵌套对象(trigger、steps、filter、ab)保留 API 中的字段名(segment_id、sample_pct 等)。路由详情:新闻通讯。当密钥覆盖多个域名时,用 domain 参数指定域名。

从您的应用进行订阅和退订(服务器端:newsletter.subscribe、unsubscribe、getSubscription;浏览器端:PublicForm、solveChallenge),以及营销邮件定时发送(schedule、unschedule、sendNow、listScheduled、check):请参见指南您自己的订阅表单、从您的应用退订和定时发送营销邮件,其中提供了每种 SDK 的示例。

联系人质量与送达率:验证地址但不保存(contacts.verifyEmail)或重新验证联系人的地址(contacts.reverify),按参与度和地址状态筛选(engagement、emailStatus),导出 CSV(contacts.export),导入时排除无效地址(skipInvalid),以及营销邮件的送达率评分(broadcasts.deliverability):请参见联系人、分组和主题和营销邮件,其中提供了每种 SDK 的示例。

Webhook

Node / Bun
const event = voaaro.webhooks.verify(rawBody, req.headers, process.env.VOAARO_WEBHOOK_SECRET!)

如果签名错误或已过期(5 分钟),会抛出 WebhookVerificationError。多个签名密钥:传入数组 [新密钥, 旧密钥] 代替单个签名密钥;只要其中一个与请求头中的某个 v1 匹配,通知即被接受。完整示例和轮换:签名。

管理端点(详情):

Node / Bun
const { data: hook } = await voaaro.webhooks.create({ endpoint: "https://app.example.com/webhooks/voaaro", events: ["email.bounced"] })
const secret = hook!.signing_secret // shown only once
await voaaro.webhooks.rotateSecret(hook!.id, { graceHours: 24 })
for await (const d of voaaro.webhooks.deliveries.iterate(hook!.id, { status: "failed" })) {
  await voaaro.webhooks.deliveries.replay(hook!.id, d.id)
}

从 Resend SDK 迁移

resend.emails.send 变为 voaaro.emails.send,参数和 {data, error} 响应均相同;只需更改导入、密钥和 baseUrl。请参见有哪些不同。

Esc