Send a WhatsApp message with Node.js
Send a managed WhatsApp template to a phone you control using the Bird SDK.
1. Prepare the workspace
Use a Bird workspace with WhatsApp sending enabled and balance for the message. Use your own WhatsApp phone number in international format and follow the recipient-permission requirements for the message. This sample uses the managed bird_otp template. The literal code is demonstration content; use Verify for an authentication flow. See WhatsApp setup for managed and business-owned senders.
2. Install and configure
Use Node.js 20.3 or later. In a new project directory:
Esempio di codice
npm init -y
npm install @messagebird/sdk tsx
export BIRD_API_KEY="YOUR_API_KEY"
export BIRD_TEST_PHONE="YOUR_WHATSAPP_NUMBER"Replace the environment values with your own workspace credentials and test destination. The SDK selects the API region from the key. Run this example on your server or development machine.
3. Run the example
Save as whatsapp.mts:
Esempio di codice
import { BirdClient } from "@messagebird/sdk";
const apiKey = process.env.BIRD_API_KEY;
if (!apiKey) throw new Error("Set BIRD_API_KEY before running the sample.");
const bird = new BirdClient({ apiKey });
const to = process.env.BIRD_TEST_PHONE;
if (!to) throw new Error("Set BIRD_TEST_PHONE to your WhatsApp number.");
const message = await bird.whatsapp.send({
to,
template: {
slug: "bird_otp",
components: [{ type: "body", parameters: [{ type: "text", text: "123456" }] }],
},
});
console.log(message.id, message.status);Esempio di codice
npx tsx whatsapp.mts4. Inspect the outcome
Keep the message ID and inspect the message on your phone. An accepted response means the send was accepted for processing; follow the delivery result through WhatsApp events. Every new run requests another send.
For your own branded conversation and incoming replies, follow phone-number setup and create the appropriate message templates. A successful managed-template test does not automatically configure your business-owned number.
Next steps
Risorse correlate
Prosegui con la documentazione, le guide e gli esempi per questo argomento. Le risorse sono in inglese.
Guarda la guidaVerify phone numbers at signupComprendi il concettoWhat does OTP mean? One-time passwords explainedEsplora la funzionalitàConnect WhatsAppSegui il percorso di apprendimentoBuild your first integration
Prova l'esercitazione e ottieni un brief di implementazione