This guide will walk you through steps of deleting existing template from your account using the NodeJS library.
Required Access Level: ModifyTemplates
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.
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 TemplatesApi that will be used to delete a template.
const templatesApi = new ElasticEmail.TemplatesApi();
To delete 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/templatesByNameDelete
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 deleted.');
}
};
And finally, call templatesByNameDelete
method from the API to delete a template:
templatesApi.templatesByNameDelete(templateName, callback);
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 = 'My new template';
const callback = (error, data, response) => {
if (error) {
console.error(error);
} else {
console.log('API called successfully.');
console.log('Template deleted.');
}
};
templatesApi.templatesByNameDelete(templateName, callback);
node snippet.js