Skip to content

Documentation

Send email in Node.js.

Works with: Node 18 or newer, Bun, Deno and edge runtimes (it uses fetch). No dependencies.

Install

Get a key under API keys in the dashboard and verify a sending domain first (how). Then add the library:

Terminal
npm install mailtruss

Send an email

This sends one message and passes an idempotency key, so repeating the request can never deliver it twice. A send is retried only when you pass an idempotencyKey; reads and deletes retry on 429 and 5xx.

send.ts
import { MailClient } from "mailtruss";
​
const mail = new MailClient({ apiKey: process.env.MAILTRUSS_API_KEY! });
​
const { id } = await mail.emails.send(
{ from: "Ada <ada@yourdomain.com>", to: "you@example.com", subject: "Hello", html: "<p>It works.</p>" },
{ idempotencyKey: "order-42" }, // makes retries safe
);

More on what you can put in a message (cc, bcc, attachments, tags, templates, scheduling) is under Sending.

Verify webhook signatures

Mailtruss signs every webhook with the Standard Webhooks scheme. Check the signature before you trust a request. Read the raw body with req.text(): re-serialised JSON will not match the signature.

app/api/mailtruss/route.ts
import { verifyWebhook } from "mailtruss";
​
export async function POST(req: Request) {
try {
const event = await verifyWebhook(await req.text(), req.headers, process.env.MAILTRUSS_WEBHOOK_SECRET!);
// event.type: "email.delivered", "email.bounced", ...
return new Response("ok");
} catch {
return new Response("invalid signature", { status: 400 });
}
}

Events, payloads and retries are on the Webhooks page.

Handle errors

Failures throw MailError with statusCode, name_ and requestId. Search the request ID in the dashboard's API logs. Every error name, with its cause and fix, is in the error catalogue.

Other languages

Last updated

Something wrong or unclear on this page?

Report an issue