How to Send Email with Node.js

Install the official @mailsurge/node package and send transactional email from Node.js 18+.

Before you start

You need a verified domain and API key in the Mailsurge dashboard. The from address must use that domain. See the API docs for field formats and response codes.

Install the official package

Add @mailsurge/node to your project. Node.js 18+ is required.


                                                1
                                npm install @mailsurge/node
                    

Send with the SDK

Set MAILSURGE_API_KEY in your environment. A successful send resolves without a response body — the message was accepted and queued for delivery.


                                                1
                                import Mailsurge from '@mailsurge/node';
                                                2
                                import { MailsurgeRateLimitError } from '@mailsurge/node';
                                                3
                                
                                                4
                                const mailsurge = new Mailsurge();
                                                5
                                
                                                6
                                try {
                                                7
                                  await mailsurge.messages.send({
                                                8
                                    from: 'Acme <no-reply@yourdomain.com>',
                                                9
                                    to: ['customer@example.com'],
                                                10
                                    subject: 'Password reset',
                                                11
                                    html: '<p>Reset your password using the secure link.</p>',
                                                12
                                    text: 'Reset your password using the secure link.',
                                                13
                                  });
                                                14
                                } catch (error) {
                                                15
                                  if (error instanceof MailsurgeRateLimitError) {
                                                16
                                    console.error('Sending limit reached:', error.body.limit);
                                                17
                                  }
                                                18
                                
                                                19
                                  throw error;
                                                20
                                }
                    

Production checklist

Move sends off the request cycle and plan for failures before you ship.

  • Run sends inside a background worker (BullMQ, your platform job runner, or a queue) so API latency never blocks HTTP handlers.
  • Use verified domains for every from address.
  • Catch typed SDK errors for auth, validation, and rate limits instead of parsing raw HTTP bodies.
  • Retry transient failures and surface permanent errors to your observability stack.

Continue reading

Related guides to help you ship production-ready transactional email.

Sending Email with Python

Official Python client for scripts and workers.

Read guide

Sending Email with Ruby

Net::HTTP example for Rails and standalone Ruby.

Read guide
Also available: the official Python SDK and the Laravel package.

Ready to send your first email?

Verify a domain, create an API key, and call POST /api/v1/message from your stack.