WhatsApp templates
List your WhatsApp numbers and templates. The script only reads by default; you can enable template creation or sending an approved template.
Before you start
- A Platica API key in
PLATIC_API_KEYand a connected WhatsApp number. - Node.js 20.3 or newer and
npm install platic-sdk.
Read, create and send
- Lists your channels and prints the WhatsApp numbers. Their
prefixedChannelId(wb-…) is thechannelIdyou send messages from. - Lists your templates and where Meta's review stands. An account that could not be read appears in
warnings. - Opt in: creates a template for Meta's review, reads it back and deletes it at the end.
- Opt in: sends an approved template to one number, with its example values or the ones you pass.
Important: Sending an approved template sends a WhatsApp message to the selected number. Check the Meta fees that apply to the template. Creating a template sends it to Meta's review. Neither happens unless you set the variables below.
The code
import { Platic, PlaticApiError } from "platic-sdk";
import type { WhatsAppTemplateWrite } from "platic-sdk";
const apiKey = process.env.PLATIC_API_KEY;
if (!apiKey) {
console.error("Set PLATIC_API_KEY to a Platica API key (pl_key_…).");
process.exit(1);
}
const platic = new Platic({ apiKey });
// 1. Your WhatsApp numbers: their `prefixedChannelId` (`wb-…`) is the `channelId` messages are sent from.
const channels = await platic.channels.list();
for (const channel of channels.filter((c) => c.prefixedChannelId?.startsWith("wb-"))) {
console.log(`channel ${channel.prefixedChannelId} ${channel.displayPhoneNumber ?? ""} (${channel.health.state})`);
}
// 2. Your templates and where Meta's review stands. An account that could not be read is in `warnings`, not hidden.
const { templates, warnings } = await platic.whatsapp.templates.list();
for (const template of templates.slice(0, 20)) console.log(`template ${template.name} [${template.language}] ${template.category} ${template.status}`);
console.log(`${templates.length} templates`);
for (const warning of warnings) console.warn(`${warning.wabaId ? `WhatsApp account ${warning.wabaId}: ` : ""}${warning.code} ${warning.message}`);
// 3. Opt in: create a template for review, then delete it.
if (process.env.PLATIC_EXAMPLE_CREATE_TEMPLATE === "1") {
const name = `sdk_example_${Date.now()}`;
const wabaId = process.env.PLATIC_EXAMPLE_WABA_ID || undefined;
let created: WhatsAppTemplateWrite;
try {
created = await platic.whatsapp.templates.create({
name,
language: "es_MX",
category: "UTILITY",
components: [{ type: "BODY", text: "Hola {{1}}, tu pedido {{2}} está listo para recoger.", example: { body_text: [["Ana", "1001"]] } }],
wabaId,
});
} catch (error) {
// Several accounts and none chosen: `details.available` lists them so you can pick one.
if (error instanceof PlaticApiError && error.code === "WABA_REQUIRED") {
console.error(`This workspace has several WhatsApp Business accounts. Set PLATIC_EXAMPLE_WABA_ID to one of: ${error.details.available?.join(", ")}`);
process.exit(1);
}
throw error;
}
try {
console.log(`created ${created.name}: ${created.status}. ${created.nextStep}`);
const read = await platic.whatsapp.templates.get(name, { language: "es_MX", wabaId: created.wabaId });
console.log(`read back: ${read.status} (languages: ${read.languages.join(", ")})`);
} finally {
const deleted = await platic.whatsapp.templates.delete(name, { wabaId });
console.log(`deleted ${deleted.name} (${deleted.deleted.length} language versions)`);
}
}
// 4. Opt in: send an approved template to one number.
const to = process.env.PLATIC_EXAMPLE_SEND_TO;
const channelId = process.env.PLATIC_EXAMPLE_CHANNEL_ID;
const templateName = process.env.PLATIC_EXAMPLE_TEMPLATE;
if (to && channelId && templateName) {
const template = await platic.whatsapp.templates.get(templateName);
if (template.status !== "APPROVED") throw new Error(`${templateName} is ${template.status || "not reviewed"}: only an approved template can be sent`);
const params = process.env.PLATIC_EXAMPLE_TEMPLATE_PARAMS?.split(",").map((value) => value.trim());
// `api_example` is a ready `template` filled with the examples: replace the values you need.
const sent = await platic.messages.sendTemplate({ channelId, conversationId: to, template: { ...template.api_example, language: template.language, ...(params ? { params } : {}) } });
console.log(`message ${sent.messageId}: ${sent.status}`);
} else if (to || channelId || templateName) {
console.error("To send, set all three: PLATIC_EXAMPLE_SEND_TO, PLATIC_EXAMPLE_CHANNEL_ID and PLATIC_EXAMPLE_TEMPLATE.");
process.exitCode = 1;
} Run it
Save the code in whatsapp-templates.ts and run it with tsx.
Set your API key and run the script:
export PLATIC_API_KEY=pl_key_...
npx tsx whatsapp-templates.ts Create a template for Meta's review, read it back and delete it. A workspace with several WhatsApp Business accounts also needs PLATIC_EXAMPLE_WABA_ID; without it the error lists the accounts to pick from:
PLATIC_EXAMPLE_CREATE_TEMPLATE=1 npx tsx whatsapp-templates.ts Send an approved template to one number. The message reaches the selected number. Check the Meta fees that apply to the template. Set all three variables; PLATIC_EXAMPLE_TEMPLATE_PARAMS replaces the template's example values:
PLATIC_EXAMPLE_SEND_TO=5215512345678 \
PLATIC_EXAMPLE_CHANNEL_ID=wb-... \
PLATIC_EXAMPLE_TEMPLATE=order_update \
PLATIC_EXAMPLE_TEMPLATE_PARAMS="Ana,1001" \
npx tsx whatsapp-templates.ts What to expect
channel wb-… +52… (…)
template order_update [es_MX] UTILITY APPROVED
12 templates If you enable creation, you will see the new template's status, usually pending Meta's review, its retrieval and its deletion. If you enable sending, you will see message <id>: <status>.
API reference used
- List channels
- List WhatsApp templates
- Create a WhatsApp template
- Get a WhatsApp template
- Delete a WhatsApp template
- Send a template
Next steps
All SDK examples: the next one is Webhook server.