This guide provides everything you need to send messages, build bots, and automate communication using the WhatsApp Business API with Python. We'll provide production-ready code examples and best practices using SendZen.io to ensure a world-class developer experience.Whether you want to write a simple Python script to send WhatsApp messages or build a sophisticated AI chatbot, you're in the right place. Unofficial libraries that rely on web scraping are unreliable and will get your number banned. Using the official WhatsApp Business API is the only stable and scalable method for professional applications.SendZen makes the official API incredibly simple. Let's dive in.Sending your first message with a Python script.
Receiving messages using a Flask webhook.
Building a complete, interactive WhatsApp bot.
Best practices for production, including Django integration.
Prerequisites#
Before you start, you'll need:3.
The requests and flask Python libraries. Install them with pip:
Part 1: How to Send a WhatsApp Message with Python#
Sending a transactional message, like an order confirmation or a 2FA code, is a core use case. This Python code uses the requests library to make a simple API call.Create a file named send_message.py and add the following code.1.
Replace YOUR_API_TOKEN and RECIPIENT_NUMBER with your details.
2.
Run the file from your terminal: python send_message.py.
You've just successfully sent an automated WhatsApp message using Python. This simple function is the foundation for all outbound messaging.
Part 2: Receiving Messages with a Python Webhook#
To build a WhatsApp bot, your application needs to listen for incoming messages. The API does this using webhooks. When a user messages your number, WhatsApp sends the message data as a JSON POST request to a URL you specify.We'll use Flask, a lightweight web framework, to create a simple webhook server.Create a file named app.py:1.
Run the Flask App: python app.py. Your server is now running on localhost:5000.
2.
Expose to the Web: WhatsApp can't reach localhost. Use a tool like ngrok to create a public URL for your local server: 3.
Configure the Webhook: Copy the https://....ngrok.io URL provided by ngrok, append your route (/webhooks/whatsapp), and paste it into the Webhook URL field in your SendZen dashboard.
Now, when you send a message to your WhatsApp number, you'll see the message data printed in your terminal.
Part 3: Building a Complete WhatsApp Bot in Python#
Let's combine what we've learned to create a simple but complete WhatsApp bot. This bot will reply to specific keywords.We'll modify our app.py to import and use the send_whatsapp_message function we wrote earlier.Run this updated app.py with ngrok again. Now, when you send "hello" or "status" to your WhatsApp number, the bot will reply automatically! This is the foundation for all WhatsApp automation with Python.
Advanced Topics & Best Practices#
Scaling Up: Python WhatsApp Bulk Sender#
To send bulk messages, such as alerts to all your users, simply loop through a list of recipient numbers and call the send_whatsapp_message function for each.Important: To initiate conversations with users, WhatsApp requires you to use pre-approved Message Templates. This is a crucial policy to prevent spam. You can easily manage and send templates via the SendZen API. For bulk campaigns, use templates.Django WhatsApp API Integration#
Integrating with Django follows the same principles.1.
Sending: Create a "service" or "utility" function (like send_whatsapp_message) that you can call from anywhere in your Django project (views, Celery tasks, etc.).
2.
Receiving: Create a Django view for your webhook URL. Remember to exempt it from CSRF protection, as the POST request will come from an external service.
Start Building Now#
You now have a complete toolkit for WhatsApp API programming in Python. By combining a simple function to send messages and a Flask webhook to receive them, you can build powerful, automated communication workflows.With SendZen's developer-first API, you can stop wrestling with complex setups and focus on creating amazing applications. Modified at 2025-11-04 09:10:34