Managing Template Variables
Template variables allow you to personalize your emails with dynamic content. This guide covers everything you need to know about using variables in your templates.
Variable Syntax
Basic Usage
Variables use the {variable_name}
syntax:
<h1>Hello, {first_name}!</h1>
<p>Your order #{order_id} has shipped.</p>
Naming Conventions
- Use lowercase letters
- Separate words with underscores
- Keep names descriptive and concise
- Avoid special characters
Good examples:
{customer_name}
{order_total}
{shipping_address}
Variable Types
Subject Line
First of all, yes, you can use variables in the subject line of your email.
Hi, {first_name}!
Text Variables
Basic string replacement in text blocks:
Dear {customer_name},
Your order #{order_id} has shipped.
URL Variables
Links and images:
Image URL
{product_image_url}
Image Alt Text
{product_image_alt_text}
The Social Media block can also take dynamic variables.
Twitter/x
{twitter_url}
Instagram
{instagram_url}
Passing Variables to Templates
When you send an email using Notify, dynamic variables in your template (like {name}
or {company}
) need values. Notify makes this simple:
-
Sending from the Notify Website Pick a saved template, and Notify automatically prompts you to fill in required variables before sending. No API calls, no extra setup — just fill in the values and hit send.
-
Sending via the Notify NPM Package or API
Pass variable values in the request body when sending an email from your backend. Works with both the Notify SDK and direct API calls.
Example: Sending an Email with Variables
Using the Notify NPM Package
First, install the Notify NPM package:
npm install notifycx
Then, use the following code to send an email with variables:
import Notify from "notifycx";
const notify = new Notify("<your_api_key>");
async function sendTemplatedEmail() {
try {
const response = await notify.sendEmailFromTemplate({
to: "recipient@example.com",
from: "noreply@notify.cx",
templateId: "<your_template_id>",
variables: {
name: "John Doe",
company: "Example Inc.",
},
});
if (response.error) {
console.error("Error sending template email:", response.error);
return;
}
console.log("Template email sent successfully:", response.data);
} catch (error) {
console.error("Failed to send template email:", error);
}
}
sendTemplatedEmail();
Using the Notify API
If you’re not using the SDK, send emails via a simple HTTP request:
async function sendEmailFromTemplate(params: SendEmailFromTemplateParams): Promise<void> {
const response = await fetch(`https://notify.cx/api/send-email-from-template`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey,
},
body: JSON.stringify(params),
});
if (!response.ok) {
throw new Error(`Failed to send email from template: ${response.statusText}`);
}
console.log("Email from template sent successfully:", await response.json());
}
No need to overthink email handling — pick a template, pass some data, and Notify does the rest.
Next Steps
- Learn how to use Notify with Node.js
- Learn how to use Notify with JavaScript
- Learn about sending emails with variables
- Explore template testing
- Review email delivery best practices