@wakio/sdk
v0.2.0
Published
Send email through Wakio from Node, Bun and edge runtimes
Maintainers
Readme
@wakio/sdk
Send email through Wakio from Node, Bun, Cloudflare Workers and Vercel Edge.
npm install @wakio/sdkimport { Wakio } from "@wakio/sdk"
const wakio = new Wakio({ apiKey: process.env.WAKIO_API_KEY })
await wakio.emails.send({
to: user.email,
subject: "Welcome to Acme",
html: "<p>Thanks for signing up.</p>",
})- Recipients by address or with a name:
to: "Maya Lee <[email protected]>"works forto,ccandbcc. - React Email:
new Wakio({ render })withrenderfrom@react-email/render, then sendreact: <Welcome />. - Nodemailer 6 to 10:
nodemailer.createTransport(wakioTransport())from@wakio/sdk/nodemailer. - Retries with idempotency keys, so a retry never sends twice.
- Errors that say whether retrying can help:
WakioRejectedError,WakioMisconfiguredError,WakioError. - Contacts on lists:
wakio.contacts.create({ email, subscribed: true, lists: ["lst_8mQ2xYtV4kP7cNwR3dZfHa"] })answers with each list the contact is on or has left, and each list you named that Wakio did not add them to, with the reason as itsstatus, such aswithdrawn.
Native integrations: @wakio/sdk/nestjs, @wakio/sdk/authjs, @wakio/sdk/payload, @wakio/sdk/medusa. Guides for Express, Next.js, Astro, NestJS, Auth.js, Better Auth, Payload and Medusa. Full reference: wakio.net/docs/api/node.
Attaching metadata to a send
Pass metadata, an object with string values (the server refuses a number, so use String(order.id); limits are on the webhooks page), and it comes back on every webhook event for that email:
await wakio.emails.send({ to: user.email, subject: "Your receipt", html, metadata: { order_id: String(order.id) } })Through @wakio/sdk/nodemailer and @wakio/sdk/payload, set the X-Wakio-Metadata header to a JSON object instead: headers: { "X-Wakio-Metadata": JSON.stringify({ order_id: String(order.id) }) }. It becomes metadata and is never sent as a mail header. A value that is not a JSON object throws WakioRejectedError before any request.
Verifying webhooks
verifyWebhook checks the signature and the timestamp of a webhook request and returns the parsed event. Pass the body as text, not as parsed JSON.
import { verifyWebhook, WakioWebhookError } from "@wakio/sdk"
export async function POST(request: Request) {
const payload = await request.text()
try {
const event = await verifyWebhook({ payload, headers: request.headers, secret: process.env.WAKIO_WEBHOOK_SECRET! })
await handleWakioEvent(event)
return new Response(null, { status: 200 })
} catch (error) {
if (error instanceof WakioWebhookError) return new Response(null, { status: 400 })
throw error
}
}headers can be a Headers or a plain record; names are matched without regard to case. A timestamp more than 300 seconds from now, in either direction (toleranceSeconds changes it), a missing header, a signature that does not match and a secret that is not a whsec_ secret all throw WakioWebhookError. During a secret rotation the request carries two signatures and either one passes. It uses Web Crypto, so it runs on Node, Bun, Cloudflare Workers and Vercel Edge. Full reference: wakio.net/docs/api/webhooks.
MIT licensed.
