This guide will walk you through the process of adding a new list of contacts to your account using the NodeJS library.
Required Access Level: ModifyContacts
When using Elastic Email, you send emails to contacts – recipients who receive your emails. Contacts can be grouped by created segments or lists. Segments add contacts automatically when specfied conditions are met, and contacts on lists are managed manually.
It's NodeJS code, so make sure you have it installed or download it here: https://nodejs.org/en/download/
Create a new JavaScript file snippet.js
and open it in editor of your preference eg. Visual Studio Code (https://code.visualstudio.com/)
Put the below code to your file.
Load library using below line:
const ElasticEmail = require('@elasticemail/elasticemail-client');
Get client instance:
const client = ElasticEmail.ApiClient.instance;
Generate and use your API key (remember to check a required access level):
const apikey = client.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";
Create an instance of ListsApi that will be used to create a new list.
const listsApi = new ElasticEmail.ListsApi();
Create an object with details about new list. Only ListName
is required.
You can also define if to allow unsubscription from list and pass an emails array of existing contacts on your account to add them to list during list creation.
Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/listsPost
const listData = {
ListName: "Best contacts",
AllowUnsubscribe: true,
Emails: ["johnsmith@domain.com"]
};
Create a callback function that will be called when response comes back.
In case of error it will display error details, otherwise it will display a success messages.
const callback = (error, data, response) => {
if (error) {
console.error(error);
} else {
console.log('API called successfully.');
console.log('List added.');
}
};
And finally, call listsPost
method from the API to create a list:
listsApi.listsPost(listData, callback);
const ElasticEmail = require('@elasticemail/elasticemail-client');
const client = ElasticEmail.ApiClient.instance;
const apikey = client.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";
const listsApi = new ElasticEmail.ListsApi();
const listData = {
ListName: "Best contacts",
AllowUnsubscribe: true,
Emails: ["johnsmith@domain.com"]
};
const callback = (error, data, response) => {
if (error) {
console.error(error);
} else {
console.log('API called successfully.');
console.log('List added.');
}
};
listsApi.listsPost(listData, callback);
node snippet.js