This guide will walk you through the process of adding new 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.
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 ContactsApi that will be used to add contacts.
const contactsApi = new ElasticEmail.ContactsApi();
Create an array with new contacts.
You can pass an array with up to 1000 contacts.
The Email
field is mandatory, the rest is optional.
Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/contactsPost
const contacts = [{
Email: "johnsmith@domain.com",
FirstName: "John",
LastName: "Smith"
}];
Specify an existing list name in options, otherwise contacts will be added to all contacts.
const options = {
listnames: ["New list"]
};
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.
const callback = (error, data, response) => {
if (error) {
console.error(error);
} else {
console.log('API called successfully.');
console.log('Contacts added.');
}
};
And finally, call contactsPost
method from the API to add contacts:
contactsApi.contactsPost(contacts, options, callback);
const ElasticEmail = require('@elasticemail/elasticemail-client');
const client = ElasticEmail.ApiClient.instance;
const ElasticEmail = require('@elasticemail/elasticemail-client');
const client = ElasticEmail.ApiClient.instance;
const apikey = client.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";
const contactsApi = new ElasticEmail.ContactsApi()
const contacts = [{
Email: "johnsmith@domain.com",
FirstName: "John",
LastName: "Smith"
}];
const options = {
listnames: ["New list"]
};
const callback = (error, data, response) => {
if (error) {
console.error(error);
} else {
console.log('API called successfully.');
console.log('Contacts added.');
}
};
contactsApi.contactsPost(contacts, options, callback);
node snippet.js