Skip to content

Latest commit

 

History

History
105 lines (72 loc) · 2.78 KB

loadCampaign.md

File metadata and controls

105 lines (72 loc) · 2.78 KB

Load Campaign

This guide will walk you through the process of loading details about an existing campaign in your account using the NodeJS library. Example details you can get are like: name, status, recipients, subject etc.

Required Access Level: ViewCampaigns

What's a campaign?

When using Elastic Email, when you send an email to any group of contacts we call that a "campaign".

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 CampaignsApi that will be used to create a campaign.

const campaignsApi = new ElasticEmail.CampaignsApi()

The only thing you need to specify is a campaign name

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

const campaignName = "example";

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 name and status of given campaign.

const callback = (error, data, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API called successfully.');
        console.log('Campaign name', data.Name);
        console.log('Campaign status', data.Status);
    }
};

And finally, call campaignsByNameGet method from the API to get a campaign:

campaignsApi.campaignsByNameGet(campaignName, 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 campaignsApi = new ElasticEmail.CampaignsApi()
const campaignName = "example";

const callback = (error, data, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API called successfully.');
        console.log('Campaign name', data.Name);
        console.log('Campaign status', data.Status);
    }
};
campaignsApi.campaignsByNameGet(campaignName, callback);

Run the code

node snippet.js