@trycourier/courier
A node.js module for communicating with the Courier REST API.
Installation (via npm)
npm install @trycourier/courier
Usage
import { CourierClient } from "@trycourier/courier";
const courier = CourierClient({ authorizationToken: "<AUTH_TOKEN>" });
const { messageId } = await courier.send({
eventId: "<EVENT_ID>",
recipientId: "<RECIPIENT_ID>",
profile: {
email: "example@example.com",
phone_number: "555-228-3890",
},
data: {},
});
const { messageId } = await courier.lists.send({
event: "<EVENT_ID>",
list: "<LIST_ID>",
data: {},
});
const { messageId } = await courier.lists.send({
event: "<EVENT_ID>",
pattern: "<PATTERN>",
data: {},
});
Environment Variables
courier-node
supports credential storage in environment variables. If no authorizationToken
is provided when instantiating the Courier client (e.g., const courier = CourierClient();
), the value in the COURIER_AUTH_TOKEN
env var will be used.
If you need to use a base url other than the default https://api.courier.com, you can set it using the COURIER_BASE_URL
env var.
Advanced Usage
import { CourierClient } from "@trycourier/courier";
const courier = CourierClient({ authorizationToken: "<AUTH_TOKEN>" });
async function run() {
const { messageId } = await courier.send({
eventId: "<EVENT_ID>",
recipientId: "<RECIPIENT_ID>",
profile: {},
data: {},
brand: "<BRAND_ID>",
preferences: {},
override: {},
});
console.log(messageId);
const messageStatus = await courier.getMessage(messageId);
console.log(messageStatus);
const { status: replaceStatus } = await courier.replaceProfile({
recipientId: "<RECIPIENT_ID>",
profile: {
email: "example@example.com",
},
});
console.log(replaceStatus);
const { status: mergeStatus } = await courier.mergeProfile({
recipientId: "<RECIPIENT_ID>",
profile: {
sms: "555-555-5555",
},
});
console.log(mergeStatus);
const { profile } = await courier.getProfile({
recipientId: "<RECIPIENT_ID>",
});
console.log(profile);
const { paging, results } = await courier.getBrands({
cursor: "<CURSOR>",
});
console.log(results);
const brand = await courier.getBrand("<BRAND_ID>");
console.log(brand);
const newBrand = await courier.createBrand({
name: "My Brand",
settings: {
colors: {
primary: "#0000FF",
secondary: "#FF0000",
tertiary: "#00FF00",
},
},
});
console.log(newBrand);
const replacedBrand = await courier.replaceBrand({
id: "<BRAND_ID>",
name: "My New Brand",
settings: {
color: {
primary: "#FF0000",
secondary: "#00FF00",
tertiary: "#0000FF",
},
},
});
console.log(replacedBrand);
await courier.deleteBrand("<BRAND_ID>");
const { paging, items } = await courier.lists.list({
cursor: "<CURSOR>",
});
console.log(items);
const list = await courier.lists.get("<LIST_ID>");
console.log(list);
const replacedList = await courier.lists.put("<LIST_ID>", {
name: "My New List",
});
console.log(replacedList);
await courier.lists.delete("<LIST_ID>");
await courier.lists.restore("<LIST_ID>");
const { paging, items } = await courier.lists.getSubscriptions("<LIST_ID>");
console.log(items);
await courier.lists.putSubscriptions("<LIST_ID>", [
{ recipientId: "RECIPIENT_ID_1" },
{ recipientId: "RECIPIENT_ID_2" },
]);
const { recipient } = courier.lists.subscribe("<LIST_ID>", "<RECIPIENT_ID>");
console.log(recipient);
await courier.lists.unsubscribe("<LIST_ID>", "<RECIPIENT_ID>");
const { paging, items } = await courier.lists.findByRecipientId(
"<RECIPIENT_ID>"
);
console.log(items);
await courier.preferences.put(recipientId, {
notifications: {
"<NOTIFICATION_ID>": { status: "<OPT_IN_STATUS>" },
},
});
const prefs = await courier.preferences.list();
console.log(prefs);
const profilePrefs = await courier.preferences.get(recipientId);
console.log(profilePrefs);
const { runId } = await courier.automations.invokeAdHocAutomation({
automation: {
cancelation_token: "I_AM_TOKEN",
steps: [
{
action: "send",
},
],
},
brand: "BRAND_ID",
data: {
example: "EXAMPLE_DATA",
},
profile: {
email: "foo@bar.com",
},
recipient: "RECIPIENT_ID",
template: "TEMPLATE_NAME_OR_ID",
});
console.log(runId);
const { runId } = await courier.automations.invokeAutomationTemplate({
templateId: "AUTOMATION_TEMPLATE_ID",
brand: "BRAND_ID",
data: {
example: "EXAMPLE_DATA",
},
profile: {
email: "foo@bar.com",
},
recipient: "RECIPIENT_ID",
template: "TEMPLATE_NAME_OR_ID",
});
console.log(runId);
}
run();
Idempotency
For POST
methods, you can supply an idempotencyKey
in the config parameter to ensure the idempotency of the API Call. We recommend that you use a V4 UUID
for the key. Keys are eligible to be removed from the system after they're at least 24 hours old, and a new request is generated if a key is reused after the original has been removed. For more info, see Idempotent Requests in the Courier Documentation.
import { CourierClient } from "@trycourier/courier";
import uuid4 from "uuid4";
const courier = CourierClient();
const idempotencyKey = uuid4();
async function run() {
const { messageId } = await courier.send(
{
eventId: "<EVENT_ID>",
recipientId: "<RECIPIENT_ID>",
profile: {
email: "example@example.com",
phone_number: "555-867-5309",
},
data: {
world: "JavaScript!",
},
},
{
idempotencyKey,
}
);
console.log(messageId);
}
run();
License
MIT License
Author
Courier (support@courier.com)