Skip to content

Latest commit

 

History

History
104 lines (72 loc) · 2.86 KB

loadList.md

File metadata and controls

104 lines (72 loc) · 2.86 KB

Load List

This guide will walk you through the process of loading details about contacts list on your account using the NodeJS library.

Required Access Level: ViewContacts

What's a list?

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.

Preparation

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/)

Let's dig into the code

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 load a list.

const listsApi = new ElasticEmail.ListsApi();

The only thing needed is a list name.

Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/listsByNameGet

const listName = 'Best contacts';

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 message and ListName and PublicListID. Other fields returned are: DateAdded and AllowUnsubscribe.

const callback = (error, data, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API called successfully.');
        console.log('List loaded', data.ListName);
        console.log('PublicListID', data.PublicListID);
    }
};

And finally, call listsByNameGet method from the API to load list details:

listsApi.listsByNameGet(listName, callback);

The whole code to copy and paste:

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 listName = 'Best contacts';

const callback = (error, data, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API called successfully.');
        console.log('List loaded', data.ListName);
        console.log('PublicListID', data.PublicListID);
    }
};
listsApi.listsByNameGet(listName, callback);

Run the code

node snippet.js