Skip to main content

Templates

A template is a reusable name + subject + content triple you can reference from a NotificationRequest by templateId instead of inlining subject/message on every send. Template operations live under client.templates().

Create a template

import com.notificationhub.sdk.model.TemplateRequest;

var request = TemplateRequest.builder()
.name("welcome-email")
.subject("Welcome to {{appName}}!")
.content("<h1>Hi {{firstName}}</h1><p>Thanks for joining {{appName}}.</p>")
.build();

var template = client.templates().create(request);

name, subject, and content are all required — build() throws if any is missing or blank.

Preview a template

Render a saved template with a specific set of variables, without sending anything.

var rendered = client.templates().preview(templateId, Map.of(
"firstName", "Ada",
"appName", "Acme"
));

Preview raw HTML without saving

Useful for a live template editor — render arbitrary HTML through the same engine before it's saved as a template.

var rendered = client.templates().previewRaw(
"<p>Hi {{firstName}}</p>",
Map.of("firstName", "Ada")
);

Update, list, and delete

client.templates().update(templateId, request);
client.templates().list(0, 20); // page, size
client.templates().delete(templateId);

Sending with a template

Reference a template by ID from a NotificationRequest instead of setting subject/message directly:

var request = NotificationRequest.builder()
.addChannel(ChannelType.EMAIL)
.toEmail("user@example.com")
.templateId(templateId)
.addVariable("firstName", "Ada")
.addVariable("appName", "Acme")
.build();

client.notifications().send(request);