Customer.io Node.js Client
The customerio-node package provides an official Node.js client for interacting with the Customer.io Journeys REST API. It allows developers to perform core actions such as identifying customers, tracking events, deleting profiles, and sending transactional emails, SMS, push, and Inbox messages. The current stable version is 4.3.0, with regular patch and minor releases addressing bug fixes and adding new transactional messaging capabilities. Major versions, like v4.0.0, introduce breaking changes, primarily for API consistency. While this client is robust for the Journeys API, for new integrations, Customer.io generally recommends using their Data Pipelines JavaScript client (`@customerio/cdp-analytics-js`), which provides a unified interface across various data sources. This library is designed for Node.js environments and warns against use in alternative runtimes due to potential API differences.
Common errors
-
Customer.io API returned status 400: Bad Request
cause When using `getCustomersByEmail`, query parameters were not being correctly passed to the Customer.io API, resulting in a 400 error.fixThis issue was fixed in `v3.5.0` of the `customerio-node` client. Ensure you are on version `3.5.0` or higher. If the problem persists, double-check the email format and other query parameters. -
Attribute update failure in Customer.io
cause Attempting to update an existing customer's identifier (e.g., email) without using the `cio_id` prefix for the customer ID.fixWhen updating, the `id` argument for `cio.identify()` must be formatted as `cio_<cio_id_value>`.
Warnings
- breaking The parameter `amp_body` for transactional email requests was renamed to `body_amp` for consistency across Customer.io APIs.
- gotcha When updating an existing customer's identifier (e.g., email), you must reference the customer using their `cio_id` in the format `cio_<cio_id_value>`. Using any other ID will lead to attribute update failures or create a new profile.
- gotcha Using this library with alternative JavaScript runtimes (e.g., Deno, Cloudflare Workers) that are not Node.js may cause issues due to subtle differences in APIs and standard library implementations.
- gotcha The `cio.destroy(id)` method deletes a person but does not suppress them. This means the person can be re-added to Customer.io. If permanent suppression is desired, a different method is required.
- gotcha Customer.io accounts can be in different regions (US or EU). If you do not explicitly specify your region when initializing the client, it defaults to `RegionUS`. Incorrect region configuration can lead to data routing issues or logging data in the wrong region.
Install
-
npm install customerio-node -
yarn add customerio-node -
pnpm add customerio-node
Imports
- TrackClient
const TrackClient = require('customerio-node').TrackClient;import { TrackClient } = from 'customerio-node'; - RegionUS
import RegionUS from 'customerio-node';
import { RegionUS } from 'customerio-node'; - ApiClient
const ApiClient = require('customerio-node').Api;import { ApiClient } from 'customerio-node';
Quickstart
import { TrackClient, RegionUS } from 'customerio-node';
const siteId = process.env.CUSTOMERIO_SITE_ID ?? '';
const apiKey = process.env.CUSTOMERIO_API_KEY ?? '';
if (!siteId || !apiKey) {
console.error('CUSTOMERIO_SITE_ID and CUSTOMERIO_API_KEY environment variables are required.');
process.exit(1);
}
const cio = new TrackClient(siteId, apiKey, { region: RegionUS });
async function identifyCustomer() {
try {
await cio.identify('customer_123', {
email: 'customer@example.com',
created_at: Math.floor(Date.now() / 1000),
first_name: 'Jane',
last_name: 'Doe',
plan: 'premium'
});
console.log('Customer identified successfully.');
} catch (error) {
console.error('Failed to identify customer:', error);
}
}
identifyCustomer();