Rule node API wrapper for the Rule v2 API. Supports promise handling.
- Rule SDK for Node.js
# via npm
npm install rule-sdk-node
# via yarn
yarn add rule-sdk-node
More in-depth information regarding all routes and fields can be found in the Rule API Documentation.
To be able to send requests using the Rule API you must initialize the SDK with a valid Rule API key generated from the Rule developer tab.
const rule = new RuleSDK({ apiKey: “…” });
Creates new subscribers, you can also not specify the type
field in the fields
array if you're unsure of the field's value type and the SDK will attempt to automatically identify it
const subscriber = await rule.createSubscribers({
update_on_duplicate?: boolean;
automation?: false | "reset" | "force";
sync_subscribers?: boolean; // If omitted, automations are sent if there are less than 20 subscribers in the request.
fields_clear?: boolean;
tags: string[], // Can consist of both names and/or ID’s of already existing tags (N.B. this means that tags consisting only of integers will be interpreted as IDs and not as text). If a tag does not exist it will be created.
subscribers: [{
language?: string; // (Optional) Needs to be ISO 639-1 formatted. If no language is passed the subscriber will default to the account language.
fields: [{
key: string;
type?: "text" || "date" || "datetime" || "multiple" || "json";
value: string;
}]
}]
});
const subscribers = await rule.getSubscribers({
limit: number // Optional, max value is 100
});
const subscriber = await rule.getSubscriber({
limit: number // Optional, max value is 100
});
const subscriber = await rule.getSubscriberFields({
identifier: string;
identified_by?: "email" | "phone_number" | "id";
})
const subscriber = await rule.updateSubscriber({
id: number;
data?: { // All fields are optional, so only the ones you specify below will be updated
language?: string;
fields?: [{
key: string;
type?: "text" | "date" | "datetime" | "multiple" | "json";
value: string;
}]
};
})
const deleted = await rule.deleteSubscriber({
identifier: string;
identified_by?: "email" | "phone_number" | "id";
})
const tag = await rule.createSubscriberTag({
identifier: string;
identified_by?: "email" | "phone_number" | "id";
tags: string[]
})
const tags = await rule.getSubscriberTags({
identifier: string;
identified_by?: "email" | "phone_number" | "id";
})
const tags = await rule.clearSubscriberTags({
identifier: string;
identified_by?: "email" | "phone_number" | "id";
})
const tags = await rule.deleteSubscriberTag({
identifier: string;
identified_by?: "email" | "phone_number" | "id";
tag_identifier: string
})
There are three types of transaction content types, Block content type, HTML Content type and Plain-text Content Type
-
Block Content type (Array)
Only supported for
transaction_type: email
All items specified in the
content
array are rendered vertically, and each block can contain multiple elements in theblock_content
array which is rendered horizontallyExample:
interface EmailBlockContent = { block_id: string; block_content: [ { title: string; body: string; image?: string; url?: string; } ]; }
-
HTML Content type
Only supported for
transaction_type: email
You can also specify custom HTML for the content you'd like to display in your email in case you'd like to implement designs that aren't possible with the Block Content Type
Example:
interface EmailHTMLContent { html: string; plain: string; // In case the device viewing this email can't render the HTML properly, it will fallback to the plain text version of the email }
-
Text message Content type
Write down the message in plain-text format. You must specify this content type when you're sending a transaction message via
transaction_type: "text_message"
.
Here's an example of how you can specify those content types
const tags = await rule.createTransaction({
transaction_type: "email" | "text_message";
transaction_name: string;
subject: string;
from: { name: string; email?: string };
to: { name?: string; email: string } | { phone_number: string };
content: EmailBlockContent[] | EmailHTMLContent | string;
})
const templates = await rule.getTemplates();
const template = await rule.getTemplate({
id: number
});
const tags = await rule.getTags({
limit?: number;
page?: number;
});
const tag = await rule.getTag({
identifier: string | number;
identified_by?: "name" | "id";
with_count?: boolean;
});
const tag = await rule.updateTag({
identifier: string | number;
data: {
name?: string;
description?: string;
};
});
const tag = await rule.updateTag({
identifier: string | number;
});
const tag = await rule.updateTag({
identifier: string | number;
});
const segments = await rule.getSegments({
limit?: number;
page?: number;
});
const groupsCreated = await rule.createGroupsAndFields({
fields: [{
key: string;
type?: "text" | "date" | "datetime" | "multiple" | "json";
}];
});
const groups = await rule.getGroupsWithFields({
limit?: number;
page?: number;
});
const group = await rule.getGroupWithFields({
identifier: number | string;
});
const created = await rule.createSuppression({
subscribers: [
{
"email": string;
},
{
"phone_number": string;
},
{
"id": number;
}
];
suppress_on?: {
campaign?: ["text_message" | "email"];
transaction?: ["text_message" | "email"];
};
});
const suppressions = await rule.getSuppressions({
limit?: number;
page?: number;
});
const deleted = await rule.deleteSuppressions({
identifier: string | number;
identified_by?: "email" | "phone_number" | "id";
});
const campaigns = await rule.getCampaigns({
limit?: number;
page?: number;
});
const campaign = await rule.createCampaign({
message_type: "email" | "text_message";
language: string;
subject: string;
recipients: {
tags?: [{
identifier: string;
}];
segments?: [{
identifier: string;
negative: boolean;
}];
};
from: {
name: string;
email?: string;
phone_number?: string;
};
email_template_id?: number;
content: [{
block_id: string;
block_content: [
{
title: string;
body: string;
image?: string;
url?: string;
}
];
}] | {
plain: string;
html: string;
} | string;
});
const campaign = await rule.getCampaign({
id: number;
});
const statistics = await rule.getStatistics({
id: number;
});
const statistics = await rule.sendCampaign({
message_type: "email" | "text_message";
language: string;
subject: string;
recipients: {
tags?: [{
identifier: string;
}];
segments?: [{
identifier: string;
negative: boolean;
}];
};
from: {
name: string;
email?: string;
phone_number?: string;
};
email_template_id?: number;
content: [{
block_id: string;
block_content: [
{
title: string;
body: string;
image?: string;
url?: string;
}
];
}] | {
plain: string;
html: string;
} | string;
});
const deleted = await rule.deleteCampaign({
id: number;
});
const statistics = await rule.scheduleCampaign({
message_type: "email" | "text_message";
language: string;
subject: string;
recipients: {
tags?: [{
identifier: string;
}];
segments?: [{
identifier: string;
negative: boolean;
}];
};
from: {
name: string;
email?: string;
phone_number?: string;
};
email_template_id?: number;
send_at: string;
content: [{
block_id: string;
block_content: [
{
title: string;
body: string;
image?: string;
url?: string;
}
];
}] | {
plain: string;
html: string;
} | string;
});
const preferenceGroups = await rule.getPreferenceGroups({
preference_group_id: number;
identifier: string | number;
identified_by?: "phone_number" | "id" | "email";
});
const preferenceGroups = await rule.getPreferencesBySubscriberAndGroups({
preference_group_id: number;
identifier: string | number;
identified_by?: "phone_number" | "id" | "email";
});
const preferenceGroups = await rule.getPreferencesBySubscriberAndGroups({
preference_group_id: number;
identifier: string | number;
identified_by?: "phone_number" | "id" | "email";
preferences: [{
preference_id: number;
is_opted_in: boolean;
}]
});