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:
Codebeispiel
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:
Codebeispiel
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);Codebeispiel
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
Verwandte Ressourcen
Weiter mit der Dokumentation, Anleitungen und Beispielen zu diesem Thema. Die Ressourcen sind auf Englisch.
Anleitung ansehenVerify phone numbers at signupDas Konzept verstehenWhat does OTP mean? One-time passwords explainedDie Funktion erkundenConnect WhatsAppDem Lernpfad folgenBuild your first integration
Übung ausprobieren und ein Implementierungs-Briefing erhalten