-
Notifications
You must be signed in to change notification settings - Fork 33
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);
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);
});
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);
});
client.getTemplate(12345).then(result => {
console.log(result.Active);
console.log(result.AssociatedServerId);
console.log(result.TemplateId);
console.log(result.Subject);
});
client.getTemplate("password_reset").then(result => {
console.log(result.Active);
console.log(result.AssociatedServerId);
console.log(result.TemplateId);
console.log(result.Subject);
});
client.createTemplate({Name: "welcome email", HtmlBody: "test", Subject: "test"}).then(result => {
console.log(result.Name);
});
client.createTemplate({Name: "welcome email", HtmlBody: "test {{{@content}}}", TextBody: "test {{{@content}}}", TemplateType: "Layout"}).then(result => {
console.log(result.Name);
});
client.editTemplate(8773462, {Name: "changeName"}).then(result => {
console.log(result.Name);
});
client.editTemplate("password_reset", {Name: "changeName"}).then(result => {
console.log(result.Name);
});
client.deleteTemplate(123456).then(result => {
console.log(result.Message);
});
client.deleteTemplate("password_reset").then(result => {
console.log(result.Message);
});
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);
});
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);
});
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.
client.sendEmailBatchWithTemplates([
{
TemplateId:1234567,
From: "[email protected]",
To: "[email protected]",
TemplateModel: {company: "ActiveCampaign"}
},
{
TemplateId:1234567,
From: "[email protected]",
To: "[email protected]",
TemplateModel: {company: "ActiveCampaign"}
}
]);
If you would like to push templates from one server to another one, check out the following article..
For additional information about the capabilities of the Postmark API, see Postmark Developers Documentation.
- Overview
- Migration from older version
- Getting started
- Email sending
- Bounces
- Templates
- Templates Push
- Server
- Servers
- Message Streams
- Webhooks
- Messages
- Domains
- Sender Signatures
- Stats
- Trigger Inbound Rules
- Suppressions
- Data Removal
- Embedding images in emails
- Error Handling
- Handling Web Hooks
- Mocking requests
- Troubleshooting
- Known issues and how to resolve them