0

Send a WhatsApp message

by
Published Apr 8, 2025

This endpoint is used to send a WhatsApp message. (**The first message you send using the API must contain a Template ID. You must create a template on WhatsApp on the Brevo platform to fetch the Template ID.**)

Script brevo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Brevo = {
3
  apiKey: string;
4
};
5
/**
6
 * Send a WhatsApp message
7
 * This endpoint is used to send a WhatsApp message. (**The first message you send using the API must contain a Template ID. You must create a template on WhatsApp on the Brevo platform to fetch the Template ID.**)
8
 */
9
export async function main(
10
  auth: Brevo,
11
  body:
12
    | {
13
        templateId: number;
14
        senderNumber: string;
15
        params?: {};
16
        contactNumbers: string[];
17
      }
18
    | { senderNumber: string; text: string; contactNumbers: string[] },
19
) {
20
  const url = new URL(`https://api.brevo.com/v3/whatsapp/sendMessage`);
21

22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      "Content-Type": "application/json",
26
      "api-key": auth.apiKey,
27
    },
28
    body: JSON.stringify(body),
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36