Javascript | Elastic E-mail

0
7

[ad_1]

Authentication

To offer legitimate authentication and begin utilizing our API, you’ll need an API key. To generate your API key, enter settings in your Elastic E-mail account and go to Settings -> Handle API Keys -> Create or you can too click on on the hyperlink: 

https://elasticemail.com/account#/settings/new/create-api

At this level, you’ll be able to set customized permissions and optionally available entry on your API key. 

Safety tip: The restriction forces the API Key to work solely with an IP or IP vary that will probably be specified on this subject.

Create API key 2

When you create your API key, maintain it secure as it will likely be used for each API name you make as a way to determine you and ensure your account’s credentials. You’ll be able to create both as much as 15 or an infinite quantity of API keys primarily based in your pricing plan. 

Your API key ought to be despatched contained in the header with the parameter identify ‘x-elasticemail-apikey’ and your API key as a price.

Set up and Utilization

Set up

Our official downloadable Javascript library is accessible on GitHub.

We’ve ready for you the steps to take to put in and implement our Javascript library:

If you wish to set up the repository with Node.js

npm

To publish the library as an npm, please observe the process in “Publishing npm packages”.

Then set up it by way of:

Copied!

npm set up @elasticemail/elasticemail-client --save

Lastly, it is advisable construct the module:

Copied!

npm run construct
Native growth

To make use of the library regionally with out publishing to a distant npm registry, first set up the dependencies by becoming the listing containing bundle.json (and this README). Let’s name this JAVASCRIPT_CLIENT_DIR. Then run:

Copied!

npm set up

Subsequent, hyperlink it globally in npm with the next, additionally from JAVASCRIPT_CLIENT_DIR:

Copied!

npm hyperlink

To make use of the hyperlink you simply outlined in your challenge, change to the listing you wish to use your @elasticemail/elasticemail-client from, and run:

Copied!

npm hyperlink /path/to/

Lastly, it is advisable construct the module:

Copied!

npm run construct

git

If the library is hosted at a git repository, e.g.https://github.com/elasticemail/elasticemail-js then set up it by way of:

Copied!

   npm set up elasticemail/elasticemail-js --save

For browser

The library additionally works within the browser surroundings by way of npm and browserify. After following the above steps with Node.js and putting in browserify with npm set up -g browserify, carry out the next (assuming major.js is your entry file):

Copied!

browserify major.js > bundle.js

Then embrace bundle.js within the HTML pages.

Webpack Configuration

Utilizing Webpack chances are you’ll encounter the next error: “Module not discovered: Error: Can not resolve module”, most definitely you need to disable AMD loader. Add/merge the next part to your webpack config:

Copied!

module: {
  guidelines: [
    {
      parser: {
        amd: false
      }
    }
  ]
}

Please observe the set up instruction and execute the next JS code:

Copied!

var ElasticEmail = require('@elasticemail/elasticemail-client');

var defaultClient = ElasticEmail.ApiClient.occasion;
// Configure API key authorization: apikey
var apikey = defaultClient.authentications['apikey'];
apikey.apiKey = "YOUR API KEY"
// Uncomment the next line to set a prefix for the API key, e.g. "Token" (defaults to null)
//apikey.apiKeyPrefix['X-ElasticEmail-ApiKey'] = "Token"

var api = new ElasticEmail.CampaignsApi()
var identify = "name_example"; // {String} Identify of Marketing campaign to delete
var callback = operate(error, knowledge, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API known as efficiently.');
  }
};
api.campaignsByNameDelete(identify, callback);

In case you are in search of extra details about all our libraries, you could find it right here:
https://assist.elasticemail.com/en/articles/2388011-integration-libraries 
Right here you could find different repositories to obtain the Javascript library

https://www.npmjs.com/~elasticemail

https://www.nuget.org/profiles/ElasticEmail

https://pypi.org/consumer/ElasticEmail/

https://packagist.org/customers/ElasticEmail/

https://rubygems.org/profiles/ElasticEmail

Fast begin information

On this part, we’ll let you know the steps to begin sending emails with our electronic mail API.

1. Register a free account

Register and activate your Elastic E-mail account to get entry to our product.

2. Confirm your area

Observe the directions on our settings web page to confirm your area and begin sending with us.

3. Create an API Key

Go to your setting to generate an API Key so as to add to your code later.

4. Set up our libraries

Set up our Javascript library as defined within the Set up and utilization part.

5. Ship your first electronic mail with API

Now it’s time to ship your first check electronic mail with API to ensure that every little thing is ready up appropriately after downloading the library and the authentication course of.

Copied!

let ElasticEmail = require('@elasticemail/elasticemail-client');
 
let defaultClient = ElasticEmail.ApiClient.occasion;
 
let apikey = defaultClient.authentications['apikey'];
apikey.apiKey = "895A382DF3DCC13A97E72EXAMPLEKEY"
 
let api = new ElasticEmail.EmailsApi()
 
let electronic mail = ElasticEmail.EmailMessageData.constructFromObject({
  Recipients: [
    new ElasticEmail.EmailRecipient("MeowWow ")
  ],
  Content material: {
    Physique: [
      ElasticEmail.BodyPart.constructFromObject({
        ContentType: "HTML",
        Content: "My test email content ;)"
      })
    ],
    Topic: "JS EE lib check",
    From: "MyEmail "
  }
});
 
var callback = operate(error, knowledge, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API known as efficiently.');
  }
};
api.emailsPost(electronic mail, callback);

Code Samples

Upon getting despatched the check electronic mail with our API, you can begin sending transactional emails and carry out numerous operations immediately by means of your app. We now have ready for you some code samples for many important actions, together with managing contacts and lists, creating templates, and sending and monitoring emails. Due to them, your integration with our API will probably be a good simpler and extra nice expertise.
In case any of the next snippets are modified within the meantime, the newest model of this API Library is accessible on GitHub.

Managing Contacts

On this part, we’ll stroll you thru managing the contacts in your account utilizing the Javascript library.

Add Contacts

So as to add contacts to your account, you’ll need Entry Degree: ModifyContacts.

Put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

apikey.apiKey = "YOUR_API_KEY";

Create an occasion of ContactsApi that will probably be used so as to add contacts.

Copied!

const contactsApi = new ElasticEmail.ContactsApi();

Create an array with new contacts.

You’ll be able to cross an array with as much as 1000 contacts.

The E-mail subject is obligatory, the remainder is optionally available.

Discover out extra by checking our API’s documentation.

Copied!

const contacts = [{
    Email: "johnsmith@domain.com",
    FirstName: "John",
    LastName: "Smith"
}];

Specify an present checklist identify in choices, in any other case, contacts will probably be added to all contacts.

Copied!

const choices = {
    listnames: ["New list"]
};

Create a callback operate that will probably be known as when the response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Contacts added.');
    }
};

And eventually, name contactsPost methodology from the API so as to add contacts:

Copied!

contactsApi.contactsPost(contacts, choices, callback);

Delete contacts

If you wish to delete a contact, you’ll need Entry Degree: ModifyContacts.

To delete a contact, put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of ContactsApi that will probably be used to delete contacts.

Copied!

const contactsApi = new ElasticEmail.ContactsApi();

Create an object with an array of contacts to delete.

Discover out extra by checking our API’s documentation.

Copied!

const contacts = {
    Emails: ["johnsmith@domain.com"]
};

Create a callback operate that will probably be known as when the response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Contacts deleted.');
    }
};

And eventually, name contactsDeletePost methodology from the API to delete contacts:

Copied!

contactsApi.contactsDeletePost(contacts, callback);

Add Contacts

If you wish to import contacts to your account utilizing the Javascript library, you’ll need Entry Degree: ModifyContacts.

To import contacts, put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

On this instance we’ll load a CSV file that comprises an inventory of contacts.

We’ll use built-in fs api for dealing with recordsdata in NodeJS.

Copied!

const fs = require('fs');

Create an occasion of ContactsApi that will probably be used to add contacts.

Copied!

const contactsApi = new ElasticEmail.ContactsApi();

Create a stream to learn knowledge from a file.

Copied!

const readStream = fs.createReadStream('./recordsdata/contacts.csv');

The best CSV file requires just one column E-mail, eg.:

Copied!

E-mail
john@johnsmith.com

Discover out extra by checking our API’s documentation.

Create an choices object the place we’ll cross a file knowledge, its encoding and optionaly an inventory identify to which contacts ought to be added, in any other case contacts will probably be added to all contacts.

Copied!

const choices = {
    listName: "New checklist",
    encodingName: 'utf-8',
    file: readStream,
};

Create a callback operate that will probably be known as when the response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Contacts added.');
    }
};

And eventually, name contactsImportPost methodology from the API to add contacts:

Copied!

contactsApi.contactsImportPost(choices, callback);

Export Contacts

If you wish to export the chosen contact to a downloadable file, you’ll need Entry Degree: Export.

Put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

On this instance we’ll export contacts to a CSV file.

Create an occasion of ContactsApi that will probably be used to export contacts.

Copied!

const contactsApi = new ElasticEmail.ContactsApi();

Create an choices object:

  • fileFormat – specify format by which file ought to be created, choices are: “Csv” “Xml” “Json”.
  • emails – choose contacts to export by offering array of emails
  • fileName – you’ll be able to specify file identify of your alternative

Different choices:

  • rule – eg. rule=Standingpercent20=%20Engaged – Question used for filtering
  • compressionFormat – “None” “Zip”

Discover out extra by checking our API’s documentation.

Copied!

const choices = {
    fileFormat: 'Csv',
    emails: ["johnsmith@domain.com"],
    fileName: 'exported.csv'
};

Create a callback operate that will probably be known as when the response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and hyperlink to a file.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Contacts exported');
        console.log('Hyperlink', knowledge.Hyperlink);
    }
};

And eventually, name contactsExportPost methodology from the API to export contacts:

Copied!

contactsApi.contactsExportPost(choices, callback);

Managing Lists

On this part, we’ll stroll you thru managing your contact checklist in your account utilizing the Javascript library.

Add Checklist

So as to add an inventory, you’ll need Entry Degree: ModifyContacts.

Put the next code into your file. 

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of ListsApi that will probably be used to create a brand new checklist.

Copied!

const listsApi = new ElasticEmail.ListsApi();

Create an object with particulars concerning the new checklist. Solely ListName is required.

You too can outline if to permit unsubscription from the checklist and cross an electronic mail array of present contacts in your account so as to add them to the checklist throughout checklist creation.

Discover out extra by checking our API’s documentation.

Copied!

const listData = {
    ListName: "Finest contacts",
    AllowUnsubscribe: true,
    Emails: ["johnsmith@domain.com"]
};

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to displaya success message.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Checklist added.');
    }
};

And eventually, name listsPost methodology from the API to create an inventory:

Copied!

listsApi.listsPost(listData, callback);

Load Checklist

To load an inventory, you’ll need Entry Degree: ViewContacts.

Put the next code into your file. 

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of ListsApi that will probably be used to load an inventory.

Copied!

const listsApi = new ElasticEmail.ListsApi();

The one factor wanted is an inventory identify.

Discover out extra by checking our API’s documentation.

Copied!

const listName="Finest contacts";

Create a callback operate that will probably be known as when the response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and ListName and PublicListID. Different fields returned are: DateAdded and AllowUnsubscribe.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Checklist loaded', knowledge.ListName);
        console.log('PublicListID', knowledge.PublicListID);
    }
};

And eventually, name listsByNameGet methodology from the API to load checklist particulars:

Copied!

listsApi.listsByNameGet(listName, callback);

Delete Checklist

To take away a contact checklist out of your account, you’ll need Entry Degree: ModifyContacts.

Put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine the required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of ListsApi that will probably be used to delete the checklist.

Copied!

const listsApi = new ElasticEmail.ListsApi();

The one factor wanted is an inventory identify.

Discover out extra by checking our API’s documentation.

Copied!

const listName="Finest contacts";

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Checklist deleted');
    }
};

And eventually, name listsByNameDelete methodology from the API to delete an inventory:

Copied!

listsApi.listsByNameDelete(listName, callback);

Creating Templates

An electronic mail template is a physique of electronic mail ready and saved beneath a given identify. On this part, you’re going to get to know the way to add and cargo electronic mail templates.

Add Template

So as to add a template, you’ll need Entry Degree: ModifyTemplates.

Put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine the required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of TemplatesApi that will probably be used to create a brand new template.

Copied!

const templatesApi = new ElasticEmail.TemplatesApi();

Create an object with particulars concerning the new template:

  • Identify – the identify of your template by which it may be recognized and used
  • Topic – specify the default topic for this template
  • Physique – specify precise physique content material eg. in HTML, PlainText or each
  • TemplateScope – specify scope, “Private” template will not be shared, “International” template could be shared along with your sub accounts.

Discover out extra by checking our API’s documentation.

Copied!

const template = {
    Identify: 'My new template',
    Topic: 'Default topic',
    Physique: [{
        ContentType: 'HTML',
        Charset: 'utf-8',
        Content: 'My template'
    }],
    TemplateScope: 'Private',
};

Create a callback operate that will probably be known as when the response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and identify and template kind of newly created template.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Template identify', knowledge.Identify);
        console.log('Template kind', knowledge.TemplateType);
    }
};

And eventually, name templates_post methodology from the API to create a template:

Copied!

templatesApi.templatesPost(template, callback);

Load Template

To load present template particulars, you’ll need Entry Degree: ViewTemplates.

Put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of TemplatesApi that will probably be used to load a template.

Copied!

const templatesApi = new ElasticEmail.TemplatesApi();

To load a template, it is advisable specify its identify:

Discover out extra by checking our API’s documentation.

Copied!

const templateName = "hello_template";

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and identify and template kind of a template.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Template identify', knowledge.Identify);
        console.log('Template kind', knowledge.TemplateType);
    }
};

And eventually, name templatesByNameGet methodology from the API to load a template:

Copied!

templatesApi.templatesByNameGet(templateName, callback);

Delete Template

To delete a template, you’ll need Entry Degree: ModifyTemplates.

Put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of TemplatesApi that will probably be used to delete a template.

Copied!

const templatesApi = new ElasticEmail.TemplatesApi();

To delete a template, it is advisable specify its identify:

Discover out extra by checking our API’s documentation.

Copied!

const templateName = "hello_template";

Create a callback operate that will probably be known as when the response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and identify and template kind of a template.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Template deleted.');
    }
};

And eventually, name templatesByNameDelete methodology from the API to delete a template:

Copied!

templatesApi.templatesByNameDelete(templateName, callback);

Sending Emails

On this part, we’ll let you know how one can begin sending emails utilizing the Javascript library. You’ll get to know the way to ship bulk and transactional emails.

Ship Transactional Emails

To ship transactional emails, you’ll need Entry Degree: SendHttp.

Put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of EmailsApi that will probably be used to ship a transactional electronic mail.

Copied!

const templatesApi = new ElasticEmail.TemplatesApi();

First, it is advisable specify electronic mail particulars:

electronic mail recipients:

this instance demonstrates merge fields utilization, for every recipient {identify} will probably be modified to the recipient’s identify

electronic mail content material:

physique components – in HTML, PlainText or in each from electronic mail – it must be your validated electronic mail deal with electronic mail topic

Discover out extra by checking our API’s documentation.

Copied!

const emailData = {
    Recipients: {
        To: ["johnsmith@domain.com"]
    },
    Content material: {
        Physique: [
            {
                ContentType: "HTML",
                Charset: "utf-8",
                Content: "Mail content."
            },
            {
                ContentType: "PlainText",
                Charset: "utf-8",
                Content: "Mail content."
            }
        ],
        From: "myemail@area.com",
        Topic: "Instance transactional electronic mail"
    }
};) 

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('E-mail despatched.');
    }
};

And eventually, name emailsTransactionalPost methodology from the API to ship an electronic mail:

Copied!

emailsApi.emailsTransactionalPost(emailData, callback);

Ship Bulk Emails

Put the next code to your file to ship a bulk electronic mail, which means a single electronic mail despatched to a big group directly.

You have to Entry Degree: SendHttp.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine the required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of EmailsApi that will probably be used to ship a bulk electronic mail.

Copied!

const emailsApi = new ElasticEmail.EmailsApi();

First, it is advisable specify electronic mail particulars:

electronic mail recipients:

this instance demonstrates merge fields utilization, for every recipient {identify} will probably be modified to the recipient’s identify

electronic mail content material:

physique components – in HTML, PlainText or in each from electronic mail – it must be your validated electronic mail deal with electronic mail topic

Discover out extra by checking our API’s documentation.

Copied!

const emailData = {
    Recipients: [
        {
            Email: "johnsmith@domain.com",
            Fields: {
                name: "John"
            }
        }
    ],
    Content material: {
        Physique: [
            {
                ContentType: "HTML",
                Charset: "utf-8",
                Content: "Hi {name}!"
            },
            {
                ContentType: "PlainText",
                Charset: "utf-8",
                Content: "Hi {name}!"
            }
        ],
        From: "myemail@area.com",
        Topic: "Instance electronic mail"
    }
};

Create a callback operate that will probably be known as when the response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('E-mail despatched.');
    }
};

And eventually, name emailsPost methodology from the API to ship an electronic mail:

Copied!

emailsApi.emailsPost(emailData, callback);

Managing Campaigns

Add Marketing campaign

So as to add your first marketing campaign utilizing the Javascript library, you’ll need Entry Degree: ModifyCampaigns. Thoughts that when utilizing Elastic E-mail, whenever you ship an electronic mail to any group of contacts, we name it a “marketing campaign”.

So as to add a marketing campaign, put the next code into your file.

Load library utilizing the next line:

Copied!
const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine the required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of CampaignsApi that will probably be used to create a marketing campaign.

Copied!

const campaignsApi = new ElasticEmail.CampaignsApi()

Create an instance marketing campaign object:

Identify: defines the marketing campaign identify by which you’ll determine it later

Recipients: outline your viewers

Content material: outline your message particulars

Standing: outline the standing by which the marketing campaign ought to be created

Discover out extra by checking our API’s documentation.

Ship will probably be triggered instantly or postponed, relying on given choices. As a result of we outline Standing as Draft, so on this case it will likely be postponed and marketing campaign will probably be added to drafts.

Copied!

const marketing campaign = {
    Identify: 'whats up marketing campaign',
    Recipients: {
        ListNames: ["my list name"],
        SegmentNames: null,
    },
    Content material: [{
        From: 'myemail@domain.com',
        ReplyTo: 'myemail@domain.com',
        TemplateName: "hello_template",
        Subject: 'Hello'
    }],
    Standing: "Draft"
};

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and chosen particulars about newly created marketing campaign.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Marketing campaign identify', knowledge.Identify);
        console.log('Marketing campaign standing', knowledge.Standing);
    }
};

And eventually, name campaignsPost methodology from the API to create a marketing campaign:

Copied!

campaignsApi.campaignsPost(marketing campaign, callback);

Delete Marketing campaign

To delete an present marketing campaign, you’ll need Entry Degree: ModifyCampaigns. Thoughts that when utilizing Elastic E-mail, whenever you ship an electronic mail to any group of contacts, we name it a “marketing campaign”.

To delete a marketing campaign, put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of CampaignsApi that will probably be used to delete a marketing campaign.

Copied!

const campaignsApi = new ElasticEmail.CampaignsApi()

The one factor it is advisable specify is a marketing campaign identify

Discover out extra by checking our API’s documentation.

Copied!

const campaignName = "instance";

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Marketing campaign deleted.');
    }
};

And eventually, name campaignsByNameDelete methodology from the API to delete a marketing campaign:

Copied!

campaignsApi.campaignsByNameDelete(campaignName, callback);

Load Marketing campaign

To load particulars about an present marketing campaign in your account utilizing the Javascript library, you’ll need Entry Degree: ViewCampaigns. Thoughts that when utilizing Elastic E-mail, whenever you ship an electronic mail to any group of contacts, we name it a “marketing campaign”.

To load a marketing campaign, put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of CampaignsApi that will probably be used to create a marketing campaign.

Copied!

const campaignsApi = new ElasticEmail.CampaignsApi()

The one factor it is advisable specify is a marketing campaign identify

Discover out extra by checking our API’s documentation. 

Copied!

const campaignName = "instance";

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and identify and standing of given marketing campaign.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Marketing campaign identify', knowledge.Identify);
        console.log('Marketing campaign standing', knowledge.Standing);
    }
};

And eventually, name campaignsByNameGet methodology from the API to get a marketing campaign:

Copied!

campaignsApi.campaignsByNameGet(campaignName, callback);

Replace Marketing campaign

To replace present campaigns in your account utilizing the Javascript library, you’ll need Entry Degree: ModifyCampaigns. Thoughts that when utilizing Elastic E-mail, whenever you ship an electronic mail to any group of contacts, we name it a “marketing campaign”.

To replace a marketing campaign, put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

configuration = ElasticEmail.Configuration()
configuration.api_key['apikey'] = 'YOUR_API_KEY'

Create an occasion of CampaignsApi that will probably be used to delete a marketing campaign.

Copied!

const campaignsApi = new ElasticEmail.CampaignsApi()

Create a complete marketing campaign object that you just wish to put rather than a present model:

  • Identify: defines marketing campaign identify by which you’ll determine it later
  • Recipients: outline your viewers
  • Conent: outline your message particulars
  • Standing: outline standing by which marketing campaign ought to be created

Discover out extra by checking our API’s documentation. 

Ship will probably be triggered instantly or postponed, relying on given choices. As a result of we outline Standing as Draft, so on this case it will likely be postponed and marketing campaign will probably be added to drafts.

Copied!

const marketing campaign = {
    Identify: 'whats up marketing campaign up to date',
    Recipients: {
        ListNames: ["my list name"],
        SegmentNames: null,
    },
    Content material: [{
        From: 'myemail@domain.com',
        ReplyTo: 'myemail@domain.com',
        TemplateName: "hello_template",
        Subject: 'Thanks for Subscribing'
    }],
    Standing: "Draft"
};

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and particulars from the up to date model.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log('Marketing campaign identify', knowledge.Identify);
        console.log('Marketing campaign standing', knowledge.Standing);
    }
};

And eventually, name campaignsByNamePut methodology from the API to replace a marketing campaign:

Copied!

campaignsApi.campaignsByNamePut(campaignName, marketing campaign, callback);

Monitoring

On this part, we’ll stroll you thru the steps of managing actions associated to electronic mail monitoring. You’ll get to know the way to load supply and marketing campaign statistics out of your account utilizing the Javascript library.

Load Statistics

To load supply statistics out of your account, you’ll need Entry Degree: ViewReports.

Put the next code into your file.

Load library utilizing the road:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of StatisticsApi that will probably be used to get fundamental ship statistics.

Copied!

const statisticsApi = new ElasticEmail.StatisticsApi();

First, it is advisable specify a date vary:

  • from date
  • to this point – optionally available

Discover out extra by checking our API’s documentation. 

Copied!

const fromDate = new Date('2022-01-17').toJSON();
const toDate = new Date('2022-04-17').toJSON();
const choices = {
    to: toDate
};

Create a callback operate that will probably be known as when the response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and stringified knowledge.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        console.log(JSON.stringify(knowledge));
    }
};

And eventually, name statisticsGet methodology from the API to fetch statistics:

Copied!

statisticsApi.statisticsGet(fromDate, choices, callback); 

Load Channels Statistics

To load statistics for every channel out of your account, you’ll need Entry Degree: ViewChannels.

Put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of StatisticsApi that will probably be used to get fundamental ship statistics.

Copied!

const statisticsApi = new ElasticEmail.StatisticsApi();

Channels statistics reponse is paginated it is advisable specfiy pagination choices:

  • restrict – most returned objects, restrict = 0 means to return every little thing until the top of the checklist
  • offset – what number of objects ought to be skipped from begging

Eg. to return second web page of components paginated 20 components per web page specify pagination choices as follows

Copied!

    restrict = 20
    offset = 20

Discover out extra by checking our API’s documentation.

Let’s fetch first 100 channels:

Copied!

{
    restrict: 20,
    offset: 0,
};

Let’s fetch everthing:

Copied!

const pageinationOptions = {
    restrict: 0,
    offset: 0,
};

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and stringified knowledge for every channel.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        knowledge.forEach((channel) => {
            console.log(JSON.stringify(channel));
        });
    }
};

And eventually, name statisticsChannelsGet methodology from the API to fetch statistics:

Copied!

statisticsApi.statisticsChannelsGet(pageinationOptions, callback);

Load Campaigns Stats

To load statistics for every electronic mail marketing campaign out of your account, you’ll need Entry Degree: ViewChannels.

Put the next code into your file.

Load library utilizing the next line:

Copied!

const ElasticEmail = require('@elasticemail/elasticemail-client');

Get shopper occasion:

Copied!

const shopper = ElasticEmail.ApiClient.occasion;

Generate and use your API key (bear in mind to examine a required entry stage):

Copied!

const apikey = shopper.authentications['apikey'];
apikey.apiKey = "YOUR_API_KEY";

Create an occasion of StatisticsApi that will probably be used to get fundamental ship statistics.

Copied!

const statisticsApi = new ElasticEmail.StatisticsApi();

Campaigns statistics reponse is paginated it is advisable specfiy pagination choices:

  • restrict – most returned objects, restrict = 0 means to return every little thing until the top of the checklist
  • offset – what number of objects ought to be skipped from begging

Eg. to return second web page of components paginated 20 components per web page specify pagination choices as follows

Copied!

{
    restrict: 20,
    offset: 0,
};

Discover out extra by checking our API’s documentation.

Let’s fetch everthing:

Copied!

const pageinationOptions = {
    restrict: 0,
    offset: 0,
};

Create a callback operate that will probably be known as when response comes again.

In case of error it is going to show error particulars, in any other case it is going to show successful message and stringified knowledge for every marketing campaign.

Copied!

const callback = (error, knowledge, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API known as efficiently.');
        knowledge.forEach((marketing campaign) => {
            console.log(JSON.stringify(marketing campaign));
        });
    }
};

And eventually, name statisticsCampaignsGet methodology from the API to fetch statistics:

Copied!

statisticsApi.statisticsCampaignsGet(pageinationOptions, callback);

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here