Guide

How to send email with Node.js

Send email from any Node.js app with the typed SDK or plain fetch.

In short

Create a Sendpository account, verify your domain, put your API key in SENDPOSITORY_API_KEY, and send with the code below - one HTTPS request per email, from server-side code only.

1. Verify your sending domain

Add your domain in the dashboard and publish the three DNS records it shows - SPF and a bounce record on an sp. subdomain, and a DKIM key on your apex. Your existing records are never edited. Verification usually lands in a few minutes, and you can set up the code below while you wait.

2. Create a key

Create an API key with the sending permission. It is shown once, so put it straight into your secrets. Read the key from process.env.SENDPOSITORY_API_KEY.

3. Install

Terminal
npm install sendpository

4. Send an email

Node.js
import { Sendpository } from "sendpository";

const sendpository = new Sendpository(process.env.SENDPOSITORY_API_KEY);

const { id } = await sendpository.emails.send({
  from: "Your App <hello@mail.yourdomain.com>",
  to: ["customer@example.com"],
  subject: "Your receipt",
  html: "<p>Thanks for your order.</p>",
});

console.log("queued", id);

A success returns { "id": "..." } - the message’s id for looking it up later. A failure returns { "error": { "type", "message" } } with a 4xx or 5xx status.

Where the send belongs

Anywhere on the server, but not in the request path if the caller is waiting on it. Resolve the HTTP response first and send afterwards, or push the job onto whatever queue you already run.

Create one client at module scope and reuse it. Constructing an SDK instance per request builds a new connection pool each time and loses keep-alive between calls.

Testing it

Things to know

Questions

Should I create a new client for each send?

No. Build one at module scope and reuse it. A new instance per request creates a new connection pool each time, so every send pays a fresh TLS handshake instead of reusing a warm connection.

How do I send email with Node.js?

Verify your sending domain, create an API key, put it in an environment variable, and POST the message as JSON to https://api.sendpository.com/v1/emails from server-side Node.js code. The example on this page runs as written once the domain is verified.

Can I send email with Node.js without SMTP?

Yes. This is plain HTTPS, so Node.js needs no mail library and no SMTP port - which matters on hosts that block outbound 25 and 587. Failures come back as a status code on the call instead of as a bounce message hours later.

More guides

Send your first email in minutes

6,000 emails a month (up to 200 a day), free forever. No card needed.