Send a WhatsApp message with Python
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 Python 3.10 or later. In a new project directory:
Przykład kodu
python3 -m venv .venv
source .venv/bin/activate
python -m pip install messagebird-sdk
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.py:
Przykład kodu
import os
from bird import Bird
with Bird(api_key=os.environ["BIRD_API_KEY"]) as client:
message = client.whatsapp.send(
to=os.environ["BIRD_TEST_PHONE"],
template="bird_otp",
language="en",
components=[
{"type": "body", "parameters": [{"type": "text", "text": "123456"}]}
],
)
print(message.id, message.status)Przykład kodu
python whatsapp.py4. 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
Use this operation in FastAPI to send and inspect messages through an application route.
Powiązane zasoby
Kontynuuj z dokumentacją, przewodnikami i przykładami dotyczącymi tego tematu. Zasoby są w języku angielskim.
Obejrzyj przewodnikVerify phone numbers at signupZrozum koncepcjęWhat does OTP mean? One-time passwords explainedPoznaj możliwościConnect WhatsAppPodążaj ścieżką naukiBuild your first integration
Wypróbuj ćwiczenie i uzyskaj brief wdrożeniowy