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:
Exemplo de código
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:
Exemplo de código
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);Exemplo de código
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
Recursos relacionados
Continue com a documentação, guias e exemplos sobre este tópico. Os recursos estão em inglês.
Assista ao guiaVerify phone numbers at signupEntenda o conceitoWhat does OTP mean? One-time passwords explainedExplore a funcionalidadeConnect WhatsAppSiga o percurso de aprendizagemBuild your first integration
Experimente na prática e obtenha um resumo de implementação