Templates
SDK methods for managing and rendering message templates.
The SDK provides methods for creating, listing, rendering, and sending message templates.
List templates
const result = await px.listTemplates();
for (const tpl of result.data.templates) {
console.log(`${tpl.id}: ${tpl.name}`);
}
Create a template
const result = await px.createTemplate({
name: 'order-confirmation',
subject: 'Order {{orderId}} Confirmed',
body: '<h1>Hi {{firstName}}</h1><p>Your order #{{orderId}} has been confirmed.</p>',
description: 'Sent after order placement',
});
console.log(result.data.id); // "tpl_abc123"
Parameters
| Field | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Unique template name |
subject | string | No | Subject line with variables |
body | string | Yes | HTML body with Handlebars variables |
description | string | No | Internal description |
Render a template
Render a template with variables without sending it. Useful for previews.
const result = await px.renderTemplate('tpl_abc123', {
variables: {
firstName: 'Ada',
orderId: 'ORD-12345',
},
});
console.log(result.data.subject); // "Order ORD-12345 Confirmed" (plain text: variable values are not HTML-escaped; escape it before writing it into HTML)
console.log(result.data.html); // rendered HTML
Send a template
Render and send in a single call.
const result = await px.sendTemplate('tpl_abc123', {
to: ['ada@example.com'],
variables: {
firstName: 'Ada',
orderId: 'ORD-12345',
},
});
console.log(result.data.send.messageId); // "msg_xyz789" (absent when queued)
Parameters
| Field | Type | Required | Description |
|---|---|---|---|
to | string[] | Yes | Recipient email addresses |
variables | Record<string, string> | No | Template variables (default {}) |
from | string | No | Sender address, at most 320 characters (longer: 400 VALIDATION_ERROR) |
The endpoint takes no replyTo (it is ignored). A rendered email every provider refuses fails with 400 BAD_REQUEST and is not queued; see Render & Send.
Update a template
await px.updateTemplate('tpl_abc123', {
body: '<h1>Welcome, {{firstName}}!</h1><p>Your order is ready.</p>',
});
Delete a template
await px.deleteTemplate('tpl_abc123');
Template names must be unique within a tenant. Use descriptive slugs like order-confirmation, password-reset, or welcome-email.
Scopes required
| Method | Scope |
|---|---|
px.listTemplates() | templates:read |
px.createTemplate() | templates:write |
px.renderTemplate() | templates:write |
px.sendTemplate() | messaging:send |