Postmark Node.js Library
The `postmark` package is the official Node.js client library for interacting with the Postmark HTTP API, facilitating robust email sending and parsing capabilities within Node.js applications. It provides high delivery rates, comprehensive bounce and spam processing, and detailed email statistics. The current stable version is 4.0.7, which has recently updated its underlying HTTP client (`axios`). The library sees frequent patch releases to keep dependencies current and make minor model adjustments, with major version increments typically aligned with Node.js End-of-Life (EOL) cycles. Its key differentiators include being an official, actively maintained client with full support for Postmark's REST API, including features for both sending transactional emails and parsing incoming emails.
Common errors
-
TypeError: postmark.Client is not a constructor
cause Attempting to use `new postmark.Client()` with newer versions (v2+) of the library which have renamed the primary client class to `ServerClient` or `AccountClient`.fixReplace `new postmark.Client('YOUR_API_TOKEN')` with `new postmark.ServerClient('YOUR_API_TOKEN')` for server-level operations or `new postmark.AccountClient('YOUR_API_TOKEN')` for account-level operations. -
Error: "From" and "To" fields are required.
cause The `sendEmail` or `sendEmailWithTemplate` method was called without providing valid `From` and `To` email addresses in the message payload, or they were provided with incorrect casing.fixEnsure the email object passed to `sendEmail` or `sendEmailWithTemplate` includes `From: 'sender@example.com'` and `To: 'recipient@example.com'` with correct, valid email addresses and exact casing. -
Error: Not Found - The server token you provided was not found.
cause An invalid or expired Postmark API Server Token was provided during client initialization, or an Account API Token was used where a Server Token is expected.fixVerify that the API token used is a valid Postmark Server API Token, not an Account API Token, and that it is active in your Postmark account. Double-check for typos or leading/trailing spaces. -
SyntaxError: Named export 'ServerClient' not found. The requested module 'postmark' is a CommonJS module, which may not support all module.exports as named exports.
cause Trying to use ESM `import { ServerClient } from 'postmark'` in a CommonJS (CJS) environment, or an older Node.js version that doesn't fully support ESM, when the package's `package.json` might be configured for ESM or dual-packaging.fixIf running in a CJS environment, use `const { ServerClient } = require('postmark');`. Ensure your project's `package.json` has `"type": "module"` for ESM, or use a bundler that handles module interoperability correctly.
Warnings
- breaking Version 4.0.0 and above dropped support for Node.js versions older than 14.0.0. Applications running on Node.js < v14.0.0 must use `postmark` library version 3.x.x.
- gotcha Using the correct client (`ServerClient` vs `AccountClient`) and API token type is crucial. `ServerClient` is for sending emails and managing server-level resources, requiring a server API token. `AccountClient` is for managing Postmark account-level resources and requires an account API token.
- gotcha When sending emails, properties such as `From`, `To`, `Subject`, `TextBody`, and `HtmlBody` are case-sensitive according to the Postmark API specification. Mismatched casing will result in API errors.
- breaking A malicious `postmark-mcp` package was identified on npm, which was *not* the official Postmark library, but a typosquatting attempt. This package stole emails by secretly BCC'ing messages to an external server.
Install
-
npm install postmark -
yarn add postmark -
pnpm add postmark
Imports
- ServerClient
const postmark = require('postmark'); const client = new postmark.Client('YOUR_API_TOKEN');import { ServerClient } from 'postmark'; - AccountClient
const client = new postmark.AccountClient('YOUR_API_TOKEN');import { AccountClient } from 'postmark'; - Models
import type * as Models from 'postmark/dist/client/models';
Quickstart
import { ServerClient } from 'postmark';
const POSTMARK_SERVER_TOKEN = process.env.POSTMARK_SERVER_TOKEN ?? '';
if (!POSTMARK_SERVER_TOKEN) {
console.error('POSTMARK_SERVER_TOKEN environment variable is not set.');
process.exit(1);
}
const client = new ServerClient(POSTMARK_SERVER_TOKEN);
async function sendExampleEmail() {
try {
const response = await client.sendEmail({
From: 'sender@example.com',
To: 'recipient@example.com',
Subject: 'Hello from Postmark.js!',
TextBody: 'Hello, this is a test email sent using the official Postmark Node.js library!',
HtmlBody: '<html><body><strong>Hello</strong>, this is a test email sent using the official Postmark Node.js library!</body></html>',
MessageStream: 'outbound'
});
console.log('Email sent successfully:', response);
} catch (error) {
console.error('Failed to send email:', error);
if (error instanceof Error) {
console.error('Error message:', error.message);
}
}
}
sendExampleEmail();