Skip to content

Templates

Igor Balos edited this page Jun 16, 2022 · 13 revisions

For these API requests you will need to use a server API token. Once you obtain it, you will need to use server API client.

let postmark = require("postmark")
const serverToken = "xxxx-xxxxx-xxxx-xxxxx-xxxxxx"
let client = new postmark.ServerClient(serverToken);

Get list of available templates

client.getTemplates().then(result => {
    console.log(result);
    console.log(result.Templates[0].Name);
    console.log(result.Templates[0].TemplateId);
    console.log(result.Templates[0].Active);
});

Get list of available layouts

There are three options when retrieving templates. You can retrieve layouts, templates or both. You would need to use templateType options with one of following values:

  • Layout
  • Standard
  • All

Standard would retrieve standard templates for example.

client.getTemplates({count:5, offset:0, templateType: 'Layout'}).then( result => {
    console.log(result);
    console.log(result.Templates[0].Name);
    console.log(result.Templates[0].TemplateId);
    console.log(result.Templates[0].Active);
});

Get template by ID

client.getTemplate(12345).then(result => {
    console.log(result.Active);
    console.log(result.AssociatedServerId);
    console.log(result.TemplateId);
    console.log(result.Subject);
});

Get template or layout by Alias

client.getTemplate("password_reset").then(result => {
    console.log(result.Active);
    console.log(result.AssociatedServerId);
    console.log(result.TemplateId);
    console.log(result.Subject);
});

Create a new template

client.createTemplate({Name: "welcome email", HtmlBody: "test", Subject: "test"}).then(result => {
    console.log(result.Name);
});

Create a new layout

client.createTemplate({Name: "welcome email", HtmlBody: "test {{{@content}}}", TextBody: "test {{{@content}}}", TemplateType: "Layout"}).then(result => {
    console.log(result.Name);
});

Update template by ID

client.editTemplate(8773462, {Name: "changeName"}).then(result => {
    console.log(result.Name);
});

Update template or layout by Alias

client.editTemplate("password_reset", {Name: "changeName"}).then(result => {
    console.log(result.Name);
});

Delete template by ID

client.deleteTemplate(123456).then(result => {
    console.log(result.Message);
});

Delete template or layout by Alias

client.deleteTemplate("password_reset").then(result => {
    console.log(result.Message);
});

Validate template

client.validateTemplate({
    Subject: "{{#company}}{{name}}{{/company}} {{subjectHeadline}}",
    HtmlBody: "{{#company}}{{address}}{{/company}}{{#each person}} {{name}} {{/each}}",
    TextBody: "{{#company}}{{phone}}{{/company}}{{#each person}} {{name}} {{/each}}",
}).then(result => {
    console.log(result.AllContentIsValid);
    console.log(result.HtmlBody.ContentIsValid);
    console.log(result.TextBody.ContentIsValid);
    console.log(result.Subject.ContentIsValid);
    console.log(result.Subject.ValidationErrors);
    console.log(result.Subject.RenderedContent);
}).catch(error => {
    console.log(error);
});

Validate layout

client.validateTemplate({
    TemplateType: "Layout",
    HtmlBody: "Html body {{#company}}test{{/company}} {{{@content}}}",
    TextBody: "Text body {{#company}}test{{/company}} {{{@content}}}",
}).then(result => {
    console.log(result.AllContentIsValid);
    console.log(result.HtmlBody.ContentIsValid);
    console.log(result.TextBody.ContentIsValid);
}).catch(error => {
    console.log(error);
});

Send email with template

client.sendEmailWithTemplate({
    TemplateId:1234567,
    From: "[email protected]",
    To: "[email protected]",
    TemplateModel: {company: "ActiveCampaign"}
});

In case you would like to attach attachments and you are not sure how to do it, feel free to check out email sending tutorial.

Send batch email with templates

client.sendEmailBatchWithTemplates([
    {
        TemplateId:1234567,
        From: "[email protected]",
        To: "[email protected]",
        TemplateModel: {company: "ActiveCampaign"}
    },
    {
        TemplateId:1234567,
        From: "[email protected]",
        To: "[email protected]",
        TemplateModel: {company: "ActiveCampaign"}
    }
]);

Push templates from one server to another

If you would like to push templates from one server to another one, check out the following article..