Paubox NodeJS
This is the official NodeJS wrapper for the Paubox Email API.
The Paubox Email API allows your application to send secure, compliant email via Paubox and track deliveries and opens. The API wrapper allows you to construct and send messages.
Table of Contents
- Installation
- Usage
- Supported Node Versions
- Contributing
- License
- Copyright
Further documentation can be found at docs.paubox.com.
Installation
Using npm:
npm install --save paubox-node
Getting Paubox API Credentials
You will need to have a Paubox account. You can sign up here.
Once you have an account, follow the instructions on the Rest API dashboard to verify domain ownership and generate API credentials.
Configuring API Credentials
Include your API credentials in your environment file.
Base URL: https://api.paubox.com/v1/
echo "API_KEY='YOUR_API_KEY'" > .env
echo ".env" >> .gitignore
Or pass them as parameters when creating emailService
const pbMail = require('paubox-node');
const pauboxConfig = {
apiKey: 'your-api-key',
};
const service = pbMail.emailService(pauboxConfig);
Usage
To send email, prepare a Message object and call the sendMessage method of emailService.
Send Message
Please also see the API Documentation.
Please also see Sending a Dynamically Templated Message for sending a message using a dynamic template.
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
var options = {
from: 'sender@domain.com',
to: ['recipient@example.com'],
subject: 'Testing!',
text_content: 'Hello World!',
html_content: '<html><head></head><body><h1>Hello World!</h1></body></html>',
};
var message = pbMail.message(options);
service
.sendMessage(message)
.then((response) => {
console.log('Send Message method Response: ' + JSON.stringify(response));
})
.catch((error) => {
console.log('Error in Send Message method: ' + JSON.stringify(error));
});
Allowing non-TLS message delivery
If you want to send non-PHI mail that does not need to be HIPAA compliant, you can allow the message delivery to take place even if a TLS connection is unavailable.
This means the message will not be converted into a secure portal message when a nonTLS connection is encountered. To do this, include allowNonTLS: true in the options, as shown below:
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
var options = {
allowNonTLS: true,
from: 'sender@domain.com',
to: ['recipient@example.com'],
subject: 'Testing!',
text_content: 'Hello World!',
html_content: '<html><head></head><body><h1>Hello World!</h1></body></html>',
};
var message = pbMail.message(options);
Forcing Secure Notifications
Paubox Secure Notifications allow an extra layer of security, especially when coupled with an organization's requirement for message recipients to use 2-factor authentication to read messages (this setting is available to org administrators in the Paubox Admin Panel).
Instead of receiving an email with the message contents, the recipient will receive a notification email that they have a new message in Paubox.
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
var options = {
forceSecureNotification: 'true',
from: 'sender@domain.com',
to: ['recipient@example.com'],
subject: 'Testing!',
text_content: 'Hello World!',
html_content: '<html><head></head><body><h1>Hello World!</h1></body></html>',
};
var message = pbMail.message(options);
Adding the List-Unsubscribe Header
The List-Unsubscribe header provides the recipient with the option to easily opt-out of receiving any future communications. A more detailed explanation and usage guide for this header can be found at our docs here.
This header can be used by adding the list_unsubscribe: '<Email Unsubscribe Address>, <Web Unsubscribe URL' and list_unsubscribe_post: 'List-Unsubscribe=One-Click' key-value pairs to the options object as follows:
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
var options = {
from: 'sender@domain.com',
to: ['recipient@example.com'],
subject: 'Testing!',
text_content: 'Hello World!',
html_content: '<html><head></head><body><h1>Hello World!</h1></body></html>',
list_unsubscribe:
'<mailto: unsubscribe@example.com?subject=unsubscribe>, <http://www.example.com/unsubscribe.html>',
list_unsubscribe_post: 'List-Unsubscribe=One-Click',
};
var message = pbMail.message(options);
Adding Attachments
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
var attachmentContent = Buffer.from('Hello! This is the attachment content!').toString('base64');
var options = {
from: 'sender@domain.com',
reply_to: 'reply_to@domain.com',
to: ['recipient@example.com'],
bcc: ['recipient2@example.com'],
cc: ['recipientcc@example.com'],
subject: 'Testing!',
text_content: 'Hello World!',
html_content: '<html><head></head><body><h1>Hello World!</h1></body></html>',
attachments: [
{
fileName: 'HelloWorld.txt',
contentType: 'text/plain',
content: attachmentContent,
},
],
};
var message = pbMail.message(options);
Adding Custom Headers
You can add custom headers to a message by passing a custom_headers object to the message options.
As mentioned in the API Documentation, custom
headers must be prepended with X- (or x-). Custom headers should be passed as a JSON object as a key-value pair. Example:
{
"X-My-First-Header": "My First Value",
"X-My-Second-Header": "My Second Value"
}
Full example:
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
var options = {
from: 'sender@domain.com',
to: ['recipient@example.com'],
subject: 'Testing custom headers',
custom_headers: {
'X-My-First-Header': 'My First Value',
'X-My-Second-Header': 'My Second Value',
},
text_content: 'Hello World!',
html_content: '<html><head></head><body><h1>Hello World!</h1></body></html>',
};
var message = pbMail.message(options);
service
.sendMessage(message)
.then((response) => {
console.log('Send Message method Response: ' + JSON.stringify(response));
})
.catch((error) => {
console.log('Error in Send Message method: ' + JSON.stringify(error));
});
Send Bulk Messages
Please also see the API Documentation.
We recommend batches of 50 (fifty) or less. Source tracking ids are returned in order messages appear in the messages array.
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
// Create a Message for Alice
var messageAlice = pbMail.message({
from: 'sender@domain.com',
to: ['alice@example.com'],
subject: 'Hello Alice!',
text_content: 'Hello Alice!',
html_content: '<html><head></head><body><h1>Hello Alice!</h1></body></html>',
});
// Create a Message for Bob
var messageBob = pbMail.message({
from: 'sender@domain.com',
to: ['bob@example.com'],
custom_headers: {
// Custom headers are also supported for bulk messages, and can differ per message
'X-Custom-Header-1': 'Value 1',
'X-Custom-Header-2': 'Value 2',
},
subject: 'Hello Bob!',
text_content: 'Hello Bob!',
html_content: '<html><head></head><body><h1>Hello Bob!</h1></body></html>',
});
service
.sendBulkMessages([messageAlice, messageBob])
.then((response) => {
console.log('Send Message method Response: ' + JSON.stringify(response));
})
.catch((error) => {
console.log('Error in Send Message method: ' + JSON.stringify(error));
});
The same options as the sendMessage method are available for the sendBulkMessages method, including custom headers.
Get Email Disposition
Please also see the API Documentation.
The SOURCE_TRACKING_ID of a message is returned in the response of the sendMessage method. To check the status for any email, use its source tracking id and call the getEmailDisposition method of emailService:
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
service.getEmailDisposition('SOURCE_TRACKING_ID').then(function (response) {
console.log('Get Email Disposition method Response: ' + JSON.stringify(response));
});
Dynamic Templates
Create Dynamic Template
Please also see the API Documentation.
You can create a dynamic template by passing in a string, a file Buffer, or file Stream.
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
const templateName = 'your_template_name';
const templateContent = '<html><body><h1>Hello {{firstName}}!</h1></body></html>';
service.createDynamicTemplate(templateName, templateContent).then(function (response) {
console.log('Create Dynamic Template method Response: ' + JSON.stringify(response));
});
In a simple express app, this could look something like this:
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
app.post('/api/create-dynamic-template', upload.single('templateFile'), async (req, res) => {
try {
const { templateName } = req.body;
const templateFile = req.file;
const content = templateFile.buffer;
const response = await service.createDynamicTemplate(templateName, content);
res.json(response);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
Update Dynamic Template
Please also see the API Documentation.
You can update a dynamic template's content and/or name:
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
const templateId = 123; // You would get this from the listDynamicTemplates method (see below)
const templateName = 'New Name';
const templateContent = '<html><body><h1>Hello {{firstName}}!</h1></body></html>'; // New content
service.updateDynamicTemplate(templateId, templateName, templateContent).then(function (response) {
console.log('Update Dynamic Template method Response: ' + JSON.stringify(response));
});
// Or just update the content
service.updateDynamicTemplate(templateId, null, templateContent).then(function (response) {
console.log('Update Dynamic Template method Response: ' + JSON.stringify(response));
});
In a simple express app, this could look something like this:
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
app.patch(
'/api/update-dynamic-template/:templateId',
upload.single('templateFile'),
async (req, res) => {
try {
const { templateId } = req.params;
const { templateName } = req.body;
const templateFile = req.file;
const content = templateFile.buffer;
const response = await service.updateDynamicTemplate(templateId, templateName, content);
res.json(response);
} catch (error) {
res.status(500).json({ error: error.message });
}
},
);
Delete Dynamic Template
Please also see the API Documentation.
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
const templateId = 123; // You would get this from the listDynamicTemplates method (see below)
service.deleteDynamicTemplate(templateId).then(function (response) {
console.log('Delete Dynamic Template method Response: ' + JSON.stringify(response));
});
Get Dynamic Template
Please also see the API Documentation.
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
const templateId = 123; // You would get this from the listDynamicTemplates method (see below)
service.getDynamicTemplate(templateId).then(function (response) {
console.log('Get Dynamic Template method Response: ' + JSON.stringify(response));
});
List Dynamic Templates
Please also see the API Documentation.
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
service.listDynamicTemplates().then(function (response) {
console.log('List Dynamic Templates method Response: ' + JSON.stringify(response));
});
Send a Dynamically Templated Message
Please also see the API Documentation.
For example, assume you have a dynamic template named welcome_email with the following content:
<html>
<body>
<h1>Welcome {{firstName}} {{lastName}}!</h1>
</body>
</html>
You can send a message using this template by doing the following:
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.emailService();
const templateName = 'welcome_email';
const templateValues = {
firstName: 'John',
lastName: 'Doe',
};
var templatedMessage = pbMail.templatedMessage({
from: 'sender@domain.com',
to: ['recipient@example.com'],
subject: 'Welcome!',
template_name: templateName,
template_values: templateValues,
});
service
.sendTemplatedMessage(templatedMessage)
.then((response) => {
console.log('Send Templated Message method Response: ' + JSON.stringify(response));
})
.catch((error) => {
console.log('Error in Send Templated Message method: ' + JSON.stringify(error));
});
Note: Custom headers are currently not supported for templated messages.
Paubox Forms
The Paubox Forms endpoints for fetching a form definition and submitting responses are public — no API key is required. Use pbMail.formService() to get a service instance. Form-management endpoints require a scoped API key — see Authenticated form management below.
Get Form
Please also see the API Documentation.
Returns the full form definition (HTML, JSON schema, CSS) for a given form. This is typically called before rendering a form embed.
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.formService();
const formId = '550e8400-e29b-41d4-a716-446655440000';
service
.getForm(formId)
.then((form) => {
console.log('Get Form Response: ' + JSON.stringify(form));
// form.form_html contains the renderable HTML
// form.form_json contains the field schema
})
.catch((error) => {
console.log('Error in Get Form: ' + JSON.stringify(error));
});
Submit Form
Please also see the API Documentation.
Submits a respondent's answers for a form. The keys in formData should match the form's field schema (form_json). Returns null on success (HTTP 201 No Content). Maximum request size is 250 MB to support file attachments.
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.formService();
const formId = '550e8400-e29b-41d4-a716-446655440000';
const formData = {
first_name: 'Jane',
last_name: 'Smith',
email: 'jane@example.com',
};
service
.submitForm(formId, formData)
.then(() => {
console.log('Form submitted successfully');
})
.catch((error) => {
console.log('Error submitting form: ' + JSON.stringify(error));
});
To submit a form with file attachments, pass an array of attachment objects with name (filename) and content (base64-encoded file content):
'use strict';
const fs = require('fs');
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.formService();
const formId = '550e8400-e29b-41d4-a716-446655440000';
const formData = { first_name: 'Jane' };
const attachments = [
{
name: 'consent.pdf',
content: fs.readFileSync('./consent.pdf').toString('base64'),
},
];
service
.submitForm(formId, formData, attachments)
.then(() => {
console.log('Form submitted successfully');
})
.catch((error) => {
console.log('Error submitting form: ' + JSON.stringify(error));
});
Authenticated form management
The form-management methods require a scoped API key with the forms scope. Pass it as { apiKey } when creating the service, or set the FORMS_API_KEY environment variable:
echo "FORMS_API_KEY='YOUR_SCOPED_API_KEY'" >> .env
'use strict';
require('dotenv').config();
const pbMail = require('paubox-node');
const service = pbMail.formService(); // reads FORMS_API_KEY from the environment
// Or pass the key explicitly:
// const service = pbMail.formService({ apiKey: 'your-scoped-api-key' });
Calling an authenticated method without an API key throws an error. The public getForm and submitForm methods work with or without a key.
The service targets production (https://api.paubox.com/forms) by default. To point it at another environment, pass { baseURL } or set the FORMS_BASE_URL environment variable (config wins over the env var):
const service = pbMail.formService({
apiKey: 'your-scoped-api-key',
baseURL: 'https://api.staging.paubox.com/forms',
});
Do not log the whole error object. A failed authenticated request produces an error whose request config would otherwise carry your
Authorizationheader and request body. This SDK strips theAuthorizationheader off propagated errors, but you should still log only the fields you need —error.message,error.response && error.response.status, anderror.response && error.response.data— rather than serializing the entire error, so credentials and payloads never reach your logs or error tracker. The examples below follow that pattern.
List forms, with filtering, ordering, and pagination (customer_id is required — the server returns 403 without it):
service
.listForms({
customer_id: 123,
search: 'intake',
order_by: 'updated_at',
order: 'desc',
page: 1,
items: 25,
})
.then((response) => {
console.log('Forms: ' + JSON.stringify(response.results));
console.log('Page info: ' + JSON.stringify(response.page_info));
})
.catch((error) => {
console.log('Error listing forms: ' + error.message);
if (error.response)
console.log('Status ' + error.response.status + ': ' + JSON.stringify(error.response.data));
});
Get a single form by id (unlike the public getForm, this returns any of the customer's forms, including archived and inactive ones):
const formId = '550e8400-e29b-41d4-a716-446655440000';
service
.getFormById(formId)
.then((form) => {
console.log('Form: ' + JSON.stringify(form));
})
.catch((error) => {
console.log('Error getting form: ' + error.message);
if (error.response)
console.log('Status ' + error.response.status + ': ' + JSON.stringify(error.response.data));
});
Create a form:
service
.createForm({
title: 'Patient Intake',
form_json: { fields: [{ label: 'First Name', type: 'text' }] },
customer_id: 123,
version: 1,
description: 'New patient intake form',
recipient: 'intake@example.com',
active: true,
})
.then((response) => {
console.log('New form id: ' + response.id);
})
.catch((error) => {
console.log('Error creating form: ' + error.message);
if (error.response)
console.log('Status ' + error.response.status + ': ' + JSON.stringify(error.response.data));
});
Update a form (only the provided keys are changed; omitted keys are left unchanged):
const formId = '550e8400-e29b-41d4-a716-446655440000';
service
.updateForm(formId, { title: 'Patient Intake (v2)', active: false })
.then((response) => {
console.log('Update Form Response: ' + JSON.stringify(response));
})
.catch((error) => {
console.log('Error updating form: ' + error.message);
if (error.response)
console.log('Status ' + error.response.status + ': ' + JSON.stringify(error.response.data));
});
Archive or unarchive a form (archiving also sets active to false):
service.archiveForm(formId).then((response) => {
console.log(response.detail); // "Form archived."
});
service.unarchiveForm(formId).then((response) => {
console.log(response.detail); // "Form unarchived."
});
Copy a form (the copy gets a fresh id, no vanity URL, and a submission count of 0):
service
.copyForm(formId, 'Patient Intake (copy)')
.then((newForm) => {
console.log('New form: ' + JSON.stringify(newForm));
})
.catch((error) => {
console.log('Error copying form: ' + error.message);
if (error.response)
console.log('Status ' + error.response.status + ': ' + JSON.stringify(error.response.data));
});
Get aggregate form statistics (defaults to the API key's customer when no customer ID is given):
service.getFormStats().then((stats) => {
console.log('Active forms: ' + stats.active_form_count);
console.log('Total submissions: ' + stats.total_submission_count);
console.log('Submissions in the last 7 days: ' + stats.submissions_last_7_days);
});
List a form's submissions:
service
.listSubmissions(formId, { order: 'desc', page: 1, items: 50 })
.then((response) => {
console.log('Submissions: ' + JSON.stringify(response.data));
console.log('Total: ' + response.total);
})
.catch((error) => {
console.log('Error listing submissions: ' + error.message);
if (error.response)
console.log('Status ' + error.response.status + ': ' + JSON.stringify(error.response.data));
});
Export submissions as CSV — all of a form's submissions, or a single one:
const fs = require('fs');
service.exportSubmissionsCsv(formId).then((csv) => {
fs.writeFileSync('./submissions.csv', csv);
});
// Export a single submission
const submissionId = 'b3b8c7e2-1d2f-4c5a-9e8d-7f6a5b4c3d2e';
service.exportSubmissionsCsv(formId, submissionId).then((csv) => {
fs.writeFileSync('./submission.csv', csv);
});
Export a single submission as a PDF:
const fs = require('fs');
service.exportSubmissionPdf(formId, submissionId).then((pdf) => {
fs.writeFileSync('./submission.pdf', Buffer.from(pdf));
});
Supported Node Versions
Currently supported Node versions are:
- Node v22.16.x (LTS Jod)
- Node v24
Contributing
See CONTRIBUTING.md
License
See LICENSE
Copyright
Copyright 2025, Paubox, Inc.
Community & support
Questions, ideas, or want to share what you built? Join the Paubox Community — the single home for discussions across every Paubox SDK and API.
Found a security issue? Email devops@paubox.com — please don't post it publicly.