Sailthru Node.js Client

5.0.5 · active · verified Tue Apr 21

The `sailthru-client` is the official Node.js client library for interacting with the Sailthru REST API. It facilitates common operations such as sending emails, managing user profiles, and executing jobs, primarily communicating in JSON format. The current stable version is `5.0.5`. This library typically sees minor patch releases for bug fixes and dependency updates, with major version increments reserved for significant internal refactors or breaking changes in API interaction or environment requirements. Key differentiators include its direct integration with the Sailthru ecosystem, handling API authentication, rate limiting information, and supporting various HTTP methods including multipart uploads for tasks like importing user data. It's designed for server-side Node.js applications and has a clear focus on robust API interaction rather than front-end integration.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Sailthru client with API credentials and optional proxy settings, then make an `apiPost` call to add a user's email to a specified list, including error handling.

const ProxyAgent = require('proxy-agent');
const sailthruClient = require('sailthru-client');

const apiKey = process.env.SAILTHRU_API_KEY ?? '';
const apiSecret = process.env.SAILTHRU_API_SECRET ?? '';
const proxyUrl = process.env.HTTP_PROXY || ''; // e.g., 'http://168.63.43.102:3128'

const clientOptions = {};
if (proxyUrl) {
  clientOptions.agent = new ProxyAgent(proxyUrl);
}

const sailthru = sailthruClient.createSailthruClient(apiKey, apiSecret, clientOptions);

// Enable logging for debugging (optional)
sailthru.enableLogging();

// Make a POST request to add an email to a list
const data = {
  email: 'testuser@example.com',
  lists: {
    'my-newsletter-list': 1 // 1 for subscribe, 0 for unsubscribe
  },
  vars: {
    firstName: 'Test',
    lastName: 'User'
  }
};

sailthru.apiPost('email', data, function(err, response) {
  if (err) {
    console.error('Error adding email:', err.message || err);
    // Log detailed error from Sailthru API if available
    if (err.statusCode) console.error('Status Code:', err.statusCode);
    if (err.error) console.error('Error Code:', err.error);
    if (err.errormsg) console.error('Error Message:', err.errormsg);
  } else {
    console.log('Successfully added email to list:', response);
  }
});

view raw JSON →