How to Send Email with Node.js

This example uses built-in fetch in modern Node.js runtimes.

Node.js fetch example


const apiKey = process.env.SENDWICH_API_KEY;

const response = await fetch('https://sendwich.dev/api/v1/message', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    from: 'Acme ',
    to: ['customer@example.com'],
    subject: 'Password reset',
    html: '

Reset your password using the secure link.

', text: 'Reset your password using the secure link.', }), }); if (!response.ok) { throw new Error(`Sendwich request failed: ${response.status} ${await response.text()}`); }

Operational tip

Run sends inside a background worker queue for retries and better API response times.