Skip to main content

Template Messages

Template messages are pre-approved message formats that allow businesses to send notifications to customers according to WhatsApp's policy guidelines. They're essential for proactive, non-conversational messages and help ensure your communications comply with WhatsApp's Business Messaging policies.

Basic Usage

import WhatsApp from 'meta-cloud-api';

// Initialize client
const whatsapp = new WhatsApp({
phoneNumberId: YOUR_PHONE_NUMBER_ID,
accessToken: 'YOUR_ACCESS_TOKEN'
});

// Send template message
const response = await whatsapp.messages.template({
body: {
name: "hello_world",
language: {
code: "en_US",
policy: "deterministic"
},
components: [
{
type: "body",
parameters: [
{
type: "text",
text: "John"
}
]
}
]
},
to: "15551234567"
});

console.log(`Template message sent with ID: ${response.messages[0].id}`);

Parameters

The template() method accepts an object with the following parameters:

ParameterTypeDescription
bodyMessageTemplateObjectContains template configuration including name, language, and components
tostringThe recipient's phone number with country code
replyMessageIdstring (optional)ID of a message to reply to

MessageTemplateObject

The template body object contains the template configuration:

PropertyTypeDescription
namestringName of the template
languageobjectLanguage configuration with code and policy
componentsarrayArray of component objects for personalizing the template

Language Object

{
language: {
code: "en_US", // Language code
policy: "deterministic"
}
}

Components Array

The components array contains objects for header, body, footer, and buttons:

{
components: [
{
type: "body",
parameters: [
{
type: "text",
text: "John"
},
{
type: "currency",
currency: {
code: "USD",
amount: 100
}
}
]
},
{
type: "header",
parameters: [
{
type: "image",
image: {
link: "https://example.com/image.jpg"
}
}
]
},
{
type: "button",
sub_type: "url",
index: 0,
parameters: [
{
type: "text",
text: "View Details"
}
]
}
]
}

Parameter Types

TypeDescriptionExample
textText parameter{ type: "text", text: "John" }
currencyCurrency with code and amount{ type: "currency", currency: { code: "USD", amount: 100 } }
date_timeDate/time value{ type: "date_time", date_time: { fallback_value: "May 6, 2023" } }
imageImage URL{ type: "image", image: { link: "https://example.com/image.jpg" } }
documentDocument URL{ type: "document", document: { link: "https://example.com/doc.pdf" } }
videoVideo URL{ type: "video", video: { link: "https://example.com/video.mp4" } }

Template Components

Templates can have several component types:

Component TypeDescription
headerThe top section of the template (optional)
bodyThe main content of the template (required)
footerThe bottom section of the template (optional)
buttonsInteractive buttons (optional)

Examples

Text-Only Template

const response = await whatsapp.messages.template({
body: {
name: "appointment_reminder",
language: {
code: "en_US",
policy: "deterministic"
},
components: [
{
type: "body",
parameters: [
{
type: "text",
text: "John"
},
{
type: "text",
text: "May 15, 2023"
},
{
type: "text",
text: "2:30 PM"
}
]
}
]
},
to: "15551234567"
});

Template with Header Image

const response = await whatsapp.messages.template({
body: {
name: "order_confirmation",
language: {
code: "en_US",
policy: "deterministic"
},
components: [
{
type: "header",
parameters: [
{
type: "image",
image: {
link: "https://example.com/order_confirmed.jpg"
}
}
]
},
{
type: "body",
parameters: [
{
type: "text",
text: "John"
},
{
type: "text",
text: "123456"
}
]
}
]
},
to: "15551234567"
});

Template with Dynamic URL Button

const response = await whatsapp.messages.template({
body: {
name: "shipping_update",
language: {
code: "en_US",
policy: "deterministic"
},
components: [
{
type: "body",
parameters: [
{
type: "text",
text: "123456"
},
{
type: "text",
text: "May 15, 2023"
}
]
},
{
type: "button",
sub_type: "url",
index: 0,
parameters: [
{
type: "text",
text: "123456"
}
]
}
]
},
to: "15551234567"
});

Template with Currency and Date Parameters

const response = await whatsapp.messages.template({
body: {
name: "payment_receipt",
language: {
code: "en_US",
policy: "deterministic"
},
components: [
{
type: "body",
parameters: [
{
type: "text",
text: "T12345"
},
{
type: "currency",
currency: {
code: "USD",
amount: 100.50
}
},
{
type: "date_time",
date_time: {
fallback_value: "May 6, 2023"
}
}
]
}
]
},
to: "15551234567"
});

Replying with a Template

const originalMessageId = "wamid.abcd1234...";

const response = await whatsapp.messages.template({
body: {
name: "support_response",
language: {
code: "en_US",
policy: "deterministic"
},
components: [
{
type: "body",
parameters: [
{
type: "text",
text: "John"
},
{
type: "text",
text: "123456"
}
]
}
]
},
to: "15551234567",
replyMessageId: originalMessageId
});

Creating and Managing Templates

Templates must be created and approved through Meta's WhatsApp Business Platform before they can be used. This is done through the Facebook Business Manager or Meta Developer Dashboard.

  1. Create a template - Design your template with header, body, footer, and buttons
  2. Submit for approval - Templates are reviewed against WhatsApp's guidelines
  3. Use approved templates - Once approved, you can send the template via the API

Template approvals typically take 1-2 business days.

Template Categories

WhatsApp templates fall into different categories based on their purpose:

CategoryDescriptionExamples
UTILITYService updates, remindersAppointment reminders, shipping notifications
MARKETINGPromotional contentSales, special offers, new product launches
AUTHENTICATIONVerification codesTwo-factor authentication codes

Error Handling

try {
const response = await whatsapp.messages.template({
body: {
name: "appointment_reminder",
language: {
code: "en_US",
policy: "deterministic"
},
components: [
{
type: "body",
parameters: [
{
type: "text",
text: "John"
},
{
type: "text",
text: "May 15, 2023"
}
]
}
]
},
to: "15551234567"
});
console.log("Template message sent successfully:", response);
} catch (error) {
console.error("Error sending template message:", error);

// Handle specific error cases
if (error.response && error.response.data) {
console.log("Error details:", error.response.data);

// Handle template not found
if (error.response.data.error && error.response.data.error.code === 132000) {
console.log("Template not found or not approved");
}

// Handle template parameter mismatch
if (error.response.data.error && error.response.data.error.code === 132001) {
console.log("Template parameter count mismatch");
}
}
}

Common Error Codes

Error CodeDescriptionSolution
132000Template not foundVerify template name and that it's approved
132001Component parameter mismatchEnsure parameter count matches template
132002Template language not supportedUse a supported language code
132003Template components invalidCheck component structure

Best Practices

  1. Use templates appropriately: Use templates for notifications and non-conversational messages.

  2. Keep templates concise: Design clear, focused templates that deliver value.

  3. Variable management: Ensure all variables in your template have appropriate values.

  4. Test thoroughly: Test your templates with various parameter values before production use.

  5. Monitor performance: Track delivery rates and user engagement with your templates.

  6. Follow approval guidelines: Adhere to WhatsApp's guidelines to increase approval chances.

  7. Update templates periodically: Review and update your templates to improve effectiveness.

  8. Localize templates: Create templates in multiple languages for international customers.