MailerSend Node.js SDK
The MailerSend Node.js SDK provides a comprehensive programmatic interface for interacting with the MailerSend API, enabling developers to efficiently send transactional and marketing emails, manage SMS campaigns, configure domains, set up webhooks, utilize templates, and track analytics data. The current stable version is `2.8.0`, released with a consistent and active development cadence, typically seeing updates monthly or bi-monthly. This commitment ensures the library remains up-to-date with API changes and introduces new features regularly. Key differentiators include its focus on high email deliverability, an intuitive API design that simplifies complex email operations, and built-in integrations for streamlined workflows. The SDK fully supports both email and SMS functionalities and ships with comprehensive TypeScript types, enhancing developer experience through improved type safety and autocompletion.
Common errors
-
TypeError: MailerSend is not a constructor
cause Attempting to import `MailerSend` as a default export (`import MailerSend from 'mailersend'`) or using CommonJS `require` in an ESM context.fixUse a named import for the `MailerSend` class: `import { MailerSend } from 'mailersend';` -
Error sending email: { "message": "Unauthenticated." }cause The provided API token is invalid, missing, or has insufficient permissions.fixVerify that your `MAILERSEND_API_TOKEN` environment variable (or wherever you've configured it) is correctly set to a valid MailerSend API key with the necessary permissions to send emails. Ensure there are no typos or leading/trailing spaces. -
Error sending email: { "message": "The given data was invalid.", "errors": { "from": [ "The from field is required." ] } }cause Required email parameters, such as 'from', 'to', or 'subject', were not provided or were invalid when constructing the `EmailParams` object.fixEnsure that all mandatory fields are set on your `EmailParams` instance. For example, `emailParams.setFrom(sender)`, `emailParams.setTo(recipients)`, and `emailParams.setSubject('...')` must be called with valid data.
Warnings
- breaking The SDK underwent significant breaking changes between v1 and v2. Applications built with v1 will require code modifications to work with v2 and newer versions. Refer to the specific V1 documentation if you are migrating or maintaining an older application.
- breaking The method for 'simple personalization' was removed in version 2.3.0. This might affect how dynamic data is injected into email templates if previous methods were used.
- gotcha The MailerSend SDK relies on an API token for authentication. Storing this directly in code or committing it to version control is a security risk. API tokens have full access to your MailerSend account.
Install
-
npm install mailersend -
yarn add mailersend -
pnpm add mailersend
Imports
- MailerSend
import MailerSend from 'mailersend'; // Not a default export const MailerSend = require('mailersend'); // CommonJS import in ESM contextimport { MailerSend } from 'mailersend'; - EmailParams
const { EmailParams } = require('mailersend'); // CommonJS importimport { EmailParams } from 'mailersend'; - Sender
import { Sender } from 'mailersend';
Quickstart
import { MailerSend, EmailParams, Sender, Recipient } from 'mailersend';
import * as dotenv from 'dotenv';
dotenv.config();
const MAIL_FROM_ADDRESS = process.env.MAIL_FROM_ADDRESS || 'info@yourdomain.com';
const MAIL_FROM_NAME = process.env.MAIL_FROM_NAME || 'Your Company Name';
const MAIL_TO_ADDRESS = process.env.MAIL_TO_ADDRESS || 'recipient@example.com';
const MAIL_TO_NAME = process.env.MAIL_TO_NAME || 'Recipient Name';
const mailerSend = new MailerSend({
apiKey: process.env.MAILERSEND_API_TOKEN || '',
});
if (!process.env.MAILERSEND_API_TOKEN) {
console.error('Error: MAILERSEND_API_TOKEN environment variable is not set.');
process.exit(1);
}
async function sendTestEmail() {
const sender = new Sender(MAIL_FROM_ADDRESS, MAIL_FROM_NAME);
const recipients = [new Recipient(MAIL_TO_ADDRESS, MAIL_TO_NAME)];
const emailParams = new EmailParams()
.setFrom(sender)
.setTo(recipients)
.setReplyTo(sender)
.setSubject('MailerSend Node.js SDK Test Email')
.setHtml('<strong>This is an automated test email from MailerSend Node.js SDK!</strong>')
.setText('This is an automated test email from MailerSend Node.js SDK!');
try {
const response = await mailerSend.email.send(emailParams);
console.log('Email sent successfully:', response.body);
} catch (error: any) {
console.error('Error sending email:', error.response?.body || error.message);
}
}
sendTestEmail();