Skip to content

Send a WhatsApp template message from Node.js / TypeScript

Template messages are the only message type you can send to a WhatsApp user outside the 24-hour customer service window, and you can also send them inside it. Meta describes the mechanics in Template fundamentals.

A template message is a POST to /<PHONE_NUMBER_ID>/messages with type set to template. The template object carries the template name, a language.code, and components that fill the template’s variables. Meta’s examples for positional and named parameters show both shapes.

Rules from Meta’s docs that matter when you send:

  • The template must have the status APPROVED. A template in any other status cannot be sent (template status).
  • Template names are not unique across languages, so language.code has to match the language the template was created in.
  • Parameter values go in components, in the order the placeholders appear in the template text (positional format).

If the number of parameters is wrong, the API answers with error 132000. A name and language pair that does not exist, or a template that is not approved, gives 132001. See the error code reference.

website/snippets/send-template.ts
const response = await client.messages.sendTemplate('15551234567', {
name: 'order_confirmation', // an APPROVED template in your account
language: { code: 'en_US' }, // must match the language the template was created in
components: [
{
type: 'body',
parameters: [
{ type: 'text', text: 'Jessica' },
{ type: 'text', text: 'SKBUP2-4CPIG9' },
],
},
],
});
console.log(response.messages[0]?.id); // the WhatsApp message ID (WAMID)

sendTemplate posts exactly this payload and returns the API response, so response.messages[0]?.id is the message ID you will see again in status webhooks. Components use the same field names as the API (sub_type, index, parameters).

Header media and button parameters use the same components array:

website/snippets/send-template.ts
await client.messages.sendTemplate('15551234567', {
name: 'shipping_update',
language: { code: 'en_US' },
components: [
{
type: 'header',
parameters: [{ type: 'image', image: { id: '2762702990552401' } }],
},
{
type: 'button',
sub_type: 'url',
index: '0',
parameters: [{ type: 'text', text: 'track/12345' }],
},
],
});

A wrong template name, language or parameter count comes back as a WhatsAppApiError carrying the Graph error code in error.code. The SDK does not check template parameters against the template before sending. See Errors and retries for the class hierarchy and Templates for creating and listing templates with client.templates.

If you send a free-form message outside the 24-hour window, the API rejects it with code 131047. See Rate limits and the 24-hour window.

Independent open-source project. Not affiliated with, endorsed by or sponsored by Meta or WhatsApp. Released under the MIT licence. Source on GitHub.