Sendzen API Documentation
HomeGetting Started
HomeGetting Started
Visit Website
Sign In
Create an account
  1. Programming Languages
  • Start Guide
    • Getting Started
    • API Reference
  • Messages
    • Template Messages
      • Send Text Message
      • Send Interactive Message
      • Send Media Message
    • Session Messages
      • Send Text Message
      • Send Image Message
      • Send Video Message
      • Send Audio Message
      • Send Document Message
      • Send Interactive Reply Message
    • Incoming Messages
      • Received Text Message
      • Received Message with Reaction
      • Received Media Message with Image
      • Received Callback from a Quick Reply Button Click
  • Template
    • List Templates
      GET
    • Create Template
      POST
    • Delete Template
      DELETE
  • Project
    • Create Project
      POST
    • List Projects
      GET
    • Update Project
      PUT
    • Retrieve Project
      GET
  • WABA
    • List WABA
      GET
    • Retrieve WABA Details from Meta
      GET
    • Retrieve WhatsApp Business Profile Detail
      GET
    • Update WhatsApp Business Profile
      POST
  • Activity Logs
    • All Event Logs
    • Incoming Message Logs
    • Message Status Logs
    • Other Event Logs
    • Multiple Event Types
  • Knowledge Base
    • Programming Languages
      • Python
      • Node.js
      • PHP
      • Laravel
      • Java
      • C# .NET
    • Onboarding
      • WhatsApp Setup
  • Schemas
    • Schemas
      • Template Message
      • Url
      • 401/403 Message Response
      • AccountLoginRequest
      • APIKeyDto
      • Address
      • Account
      • AccountRegisterRequest
      • CommonResponse
      • AccountDetailDto
      • CreateOrUpdateProjectRequest
      • Contact
      • Error
      • PartnerConfigDto
      • ContactName
      • Button
      • ErrorResponse
      • CarouselCard
      • LoginResponse
      • Email
      • PartnerInfoDto
      • Component
      • ProjectDetailDto
      • ProjectWABADetail
      • Interactive
      • WABADetail
      • WABASignUp
      • InteractiveAction
      • WABADto
      • InteractiveBody
      • CreateTemplateRequest
      • InteractiveButton
      • InteractiveFooter
      • InteractiveHeader
      • InteractiveMedia
      • Example
      • InteractiveProductItem
      • InteractiveProductSection
      • InteractiveReply
      • InteractiveRow
      • InteractiveSection
      • Location
      • Media
      • MessageRequest
      • MessageResponse
      • MessageTemplate
      • Organization
      • LimitedTimeOffer
      • Phone
      • Reaction
      • Response
      • TemplateAction
      • NamedParameter
      • TemplateButtons
      • OrderCheckoutDto
      • TemplateCarousel
      • OrderDto
      • TemplateComponent
      • OrderStatusEnum
      • TemplateCurrency
      • TemplateDateTime
      • PaymentGatewayDto
      • TemplateLimitedTimeOffer
      • TemplateMedia
      • TemplateParameter
      • TemplateProductItem
      • TemplateSection
      • TemplateTapTargetConfiguration
      • SupportedApp
      • Text
      • WhatsAppBusinessProfile
      • UploadMedia
    • Knowledge Base
  1. Programming Languages

WhatsApp API with Python

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.
This guide covers:
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:
1.
Python 3.6+ installed.
2.
A free SendZen.io Account to get your API Key.
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.
To run this script:
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:
To make this work:
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.
Sign Up for Free & Get Your API Key →
Modified at 2025-11-04 09:10:34
Previous
Programming Languages
Next
Node.js
Built with