Guide · JavaScript & TypeScript
How to send email with Node.js
Send email from Node.js with a single fetch call to the Stack Emails API, or with Nodemailer over SMTP. Works on Node 18+ with no extra dependencies.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
Node 18 and later include fetch, so no HTTP library is needed. Store your API key in an environment variable:
export STACKEMAILS_API_KEY=se_live_xxxxxxxxxxxx
2. Send your first email
const res = await fetch("https://stackemails.com/v1/emails", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.STACKEMAILS_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
from: "Acme <hello@mail.acme.com>",
to: "ada@example.com",
subject: "Welcome to Acme",
html: "<p>Thanks for signing up!</p>",
text: "Thanks for signing up!",
}),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error?.message ?? "Send failed");
console.log("Queued", data.id);
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
3. Or send over SMTP with Nodemailer
// npm install nodemailer
import nodemailer from "nodemailer";
const transport = nodemailer.createTransport({
host: "smtp.stackemails.com",
port: 465,
secure: true,
auth: { user: "stackemails", pass: process.env.STACKEMAILS_API_KEY },
});
await transport.sendMail({
from: "Acme <hello@mail.acme.com>",
to: "ada@example.com",
subject: "Welcome to Acme",
html: "<p>Thanks for signing up!</p>",
});
Using the SDK instead
npm install stackemails
import { StackEmails } from "stackemails";
const stack = new StackEmails(process.env.STACKEMAILS_API_KEY);
const { id } = await stack.emails.send({ from: "Acme <hello@mail.acme.com>", to: "ada@example.com", subject: "Hi", html: "<p>Hi</p>" });
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.