The official Node.js SDK wraps the api.email HTTP API in a small client with TypeScript types. It has no dependencies: it uses the fetch and Web Crypto built into the runtime, and it runs on Node.js 20 and later, Bun and Deno. Field names match the API reference one to one, in snake_case, so anything you read in the rest of the docs applies here unchanged.
Install
npm install api-email
The source, the changelog and the full reference live at github.com/api-email/api-email-node. The package is api-email on npm.
Send an email
import { ApiEmail } from 'api-email';
const apiEmail = new ApiEmail(process.env.API_EMAIL_KEY);
const email = await apiEmail.emails.send(
{
from: 'Acme <[email protected]>',
to: '[email protected]',
subject: 'Reset your password',
text: 'Reset it here: https://acme.com/r/8f2c',
html: '<p>Reset it <a href="https://acme.com/r/8f2c">here</a>.</p>',
},
{ idempotencyKey: 'password-reset-8f2c' },
);
console.log(email.id); // em_…
new ApiEmail() without an argument reads API_EMAIL_KEY from the environment. Keep the key on your server and never ship it to a browser or a mobile app. The sending domain must be verified first; the quickstart walks through it.
Attachments and scheduling
import { readFile } from 'node:fs/promises';
await apiEmail.emails.send({
from: 'Acme Billing <[email protected]>',
to: ['[email protected]'],
subject: 'Your receipt for order 1042',
text: 'Thanks for your order. Your receipt is attached.',
attachments: [
{ filename: 'receipt-1042.pdf', content: await readFile('receipt-1042.pdf'), content_type: 'application/pdf' },
],
scheduled_at: new Date(Date.now() + 60 * 60 * 1000),
tags: ['receipt'],
});
content takes a Buffer, a Uint8Array, an ArrayBuffer or a base64 string. scheduled_at takes a Date or an ISO 8601 string, up to 30 days ahead.
Read, list and cancel
const email = await apiEmail.emails.get('em_8f2c1a');
const page = await apiEmail.emails.list({ status: 'bounced' }); // { data, next_cursor }
for await (const email of apiEmail.emails.listAll({ tag: 'receipt' })) {
console.log(email.id, email.status);
}
await apiEmail.emails.cancel('em_8f2c1a'); // a scheduled email that has not been sent yet
Every resource has list() for one page and listAll(), which follows next_cursor for you.
Domains
const domain = await apiEmail.domains.create({ name: 'acme.com' });
// Publish the two CNAME records in domain.records, then check the status:
const { status } = await apiEmail.domains.get(domain.id); // 'pending' | 'verified' | 'failed'
The domains page explains both records.
Receive email
Point an MX record at mx.api.email, then create a route. Every message that matches it is posted to your URL as a signed email.received event.
const route = await apiEmail.routes.create({
match: '[email protected]',
forward: 'https://acme.com/hooks/inbound',
});
// route.secret signs every delivery from this route: store it like a password.
const message = await apiEmail.inbound.get('in_5d1e7a');
const pdf = await apiEmail.inbound.attachment(message.id, 0); // Uint8Array, kept for 7 days
Verify webhooks
Delivery events and inbound messages carry an api-email-signature header. verifyWebhook checks it against the raw body and returns the parsed event, or throws WebhookVerificationError. It does not need an API key.
import express from 'express';
import { verifyWebhook, WebhookVerificationError } from 'api-email';
const app = express();
app.post('/hooks/email', express.raw({ type: 'application/json' }), async (req, res, next) => {
try {
const event = await verifyWebhook(req.body, req.headers['api-email-signature'], process.env.API_EMAIL_WEBHOOK_SECRET);
if (event.type === 'email.bounced') {
// event.email_id, event.to, event.reason
}
res.sendStatus(200);
} catch (error) {
if (error instanceof WebhookVerificationError) return res.sendStatus(400);
next(error);
}
});
Pass the body exactly as received: parsed and re-serialised JSON no longer matches the signature. Both signatures sent during a secret rotation are accepted. The webhooks page lists every event.
Errors, retries and idempotency
A failed call throws ApiEmailError with status, code, message, hint and requestId. Branch on code, such as domain_not_verified, and include requestId when you contact support.
import { ApiEmailError } from 'api-email';
try {
await apiEmail.emails.send(message);
} catch (error) {
if (error instanceof ApiEmailError && error.code === 'domain_not_verified') {
// add the two CNAME records, then try again
}
throw error;
}
The SDK retries network errors, timeouts, 429 and 5xx responses up to twice, with exponential backoff, and honours Retry-After. Every POST carries an Idempotency-Key that stays the same across those retries, so a retried send never delivers twice. Pass your own key derived from your data, such as receipt-1042, to keep retries safe across process restarts too.
Prefer plain HTTP? Every example in the Node.js sending guide uses the built-in fetch, with no package to install.