Skip to content

Latest commit

 

History

History
102 lines (72 loc) · 2.8 KB

loadTemplate.md

File metadata and controls

102 lines (72 loc) · 2.8 KB

Load Template

This guide will walk you through steps of loading existing template details using the NodeJS library.

Required Access Level: ViewTemplates

What's a template?

When using Elastic Email you send emails to your contacts. A single template is a body of email prepared and saved under given name. Till it's deleted it can be reused to send any number of emails.

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 TemplatesApi that will be used to load a template.

const templatesApi = new ElasticEmail.TemplatesApi();

To load a template you need to specfiy it's name:

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

const templateName = "hello_template";

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 template type of a template.

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

And finally, call templatesByNameGet method from the API to load a template:

templatesApi.templatesByNameGet(templateName, 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 templatesApi = new ElasticEmail.TemplatesApi();
const templateName = "hello_template";
const callback = (error, data, response) => {
    if (error) {
        console.error(error);
    } else {
        console.log('API called successfully.');
        console.log('Template name', data.Name);
        console.log('Template type', data.TemplateType);
    }
};
templatesApi.templatesByNameGet(templateName, callback);

Run the code

node snippet.js