Tutorial

How to Send Email with Node.js (Nodemailer & REST API)

Two ways to do it — Nodemailer over SMTP, or a plain fetch call to a REST API — with working code and the DNS setup that keeps you out of spam.

By the Sendcorex team·Updated Sep 21, 2026·9 min read

Short answer: to send email with a REST API in Node.js, make a POST request with fetch (built into Node 18+) to the provider's send endpoint, with your API key in the Authorization header and a JSON body containing from, to, subject and html. No SDK is needed — see Option 2 for the full code.

There are two good ways to send email from Node.js: over SMTP with Nodemailer, or with a plain fetch call to a REST API. Node.js 18 or newer is enough for either — you'll also need a Sendcorex account and a verified sending domain (publish the SPF, DKIM and DMARC records the dashboard gives you before you send anything real).

Option 1: Nodemailer (SMTP)

npm install nodemailer
import nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({
  host: 'smtp.sendcorex.com',
  port: 587,
  secure: false, // STARTTLS on 587
  auth: { user: process.env.SENDCOREX_API_KEY, pass: process.env.SENDCOREX_API_KEY },
});

const info = await transporter.sendMail({
  from: '"Your App" <hello@yourdomain.com>',
  to: 'customer@example.com',
  subject: 'Welcome!',
  text: 'You are in.',
  html: '<h1>You are in.</h1>',
});

console.log('Sent:', info.messageId);

Option 2: REST API with fetch

No dependencies, works in serverless and edge runtimes where outbound SMTP is often blocked:

const response = await fetch('https://graph.sendcorex.com/v3.0/emails', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.SENDCOREX_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    to: 'customer@example.com',
    from: 'hello@yourdomain.com',
    subject: 'Welcome!',
    html: '<h1>You are in.</h1>',
  }),
});

if (!response.ok) throw new Error(`Send failed: ${response.status}`);

Errors and retries

SymptomFix
Invalid loginAPI key missing or wrong — check the env var loaded and has no stray whitespace.
ETIMEDOUTThe host blocks the SMTP port — try 587/465, or switch to the REST API.
Lands in spamCheck SPF/DKIM in a real Gmail header, send text and html, ramp volume gradually.

4xx responses mean the request itself is wrong — don't retry. 429 and 5xx are temporary — retry with exponential backoff.

Frequently asked questions

How do I send email using a REST API in Node.js?

Call the provider's send endpoint with fetch: POST a JSON body (from, to, subject, html) and put your API key in the Authorization header. Node.js 18+ has fetch built in, so you don't need any package. Check the response status and retry only 429 and 5xx errors.

Should I use SMTP (Nodemailer) or a REST API?

A REST API is usually faster and gives you a message ID and clear error codes in the response. SMTP with Nodemailer is the better fit when a library or framework already expects SMTP. Both deliver the same way.

How do I track sent emails in Node.js?

Save the message ID returned by the send call, then use webhooks (delivered, bounced, opened, clicked) or the email logs API to follow what happened to each email.

Send your first email in minutes

Create a free account, verify your domain and paste in the code above.

No credit card required