Back to blogWhatsApp Business

WhatsApp API with Python: Integration Tutorial [2026]

WhatsApp Business API con Python: editor de código enviando mensajes mediante REST API

If you're a developer and want to connect your application directly with WhatsApp, the combination of WhatsApp Cloud API + Python is the most direct path. In this tutorial you'll understand how to send messages, receive notifications via webhooks, manage templates, and build a basic chatbot -- all through Meta's official REST API. It's not about copying and pasting code: it's understanding the architecture so you can adapt it to your use case. You'll need Python 3.9+, a verified Meta Business account, and access to the WhatsApp Business API. If you're still not clear what the API is or how it fits in the ecosystem, start with the complete WhatsApp Business API guide.

Prerequisites

Before writing a single line of code, you need to have the following prepared:

  • Python 3.9 or higher installed in your development environment.
  • A verified Meta Business account. Without business verification you won't have production access.
  • Access to the WhatsApp Cloud API through the Meta Developer panel, or through a BSP (Business Solution Provider) that provides you credentials.
  • Your access token generated from the Meta Developer dashboard. It's the temporary token for testing; for production you'll need a permanent system token with OAuth authentication.
  • A phone number registered in the WhatsApp Business API. It can be the test number Meta provides in the sandbox or your verified business number.
  • Python libraries: requests for synchronous HTTP calls, flask to mount the webhook server, and python-dotenv to securely manage environment variables. If you prefer working in async, replace requests with httpx and flask with FastAPI.

If you need a step-by-step guide to obtain these credentials and configure your Meta account, check how to configure WhatsApp Business API. Here we assume you already have everything ready.

Sending Messages with the Cloud API

Sending messages reduces to a POST call against Meta's REST API. The endpoint is https://graph.facebook.com/v18.0/{phone_number_id}/messages. You need two headers: Authorization with your access token in Bearer format and Content-Type as application/json.

To send a plain text message, the request body in JSON includes four fields: messaging_product always as "whatsapp", to with the recipient's number in international format (e.g., 34612345678), type as "text", and a text object with the body field containing the message content.

To send an approved template -- mandatory when you initiate the conversation without the user having written first -- you change the type to "template" and include an object with the template name, language, and dynamic components (header, body, and button variables). Templates allow massive personalization: a single approved format can serve for thousands of different sends by injecting variables at runtime.

The API response returns a message_id that you'll use to track delivery status. If there's an error, you receive a specific code: 131047 indicates the number isn't valid, 131026 that the user doesn't have WhatsApp, 130429 that you've exceeded the rate limit. Properly handling these errors is what separates a test integration from a production one.

On performance: Meta establishes a rate limit of 80 messages per second for standard tier and up to 1,000 for accounts with higher volume. If you need bulk sending, implement a queue with exponential retries. For synchronous calls use the requests library; for high volume where you need concurrency without blocking, httpx with async is the right option. If you want to understand more about how messages flow through the architecture, check how WhatsApp Business API works.

Receiving Messages with Webhooks

Sending messages is half the equation. The other half is receiving them, and for that you need a webhook server. A webhook is an HTTP endpoint on your server that Meta sends POST notifications to each time an event occurs: a customer writes you, a message is delivered, read, or fails.

Configuration has two phases. First, verification: when you register your webhook URL in the Meta panel, it sends a GET request with a hub.verify_token parameter you define and a hub.challenge you must return. It's a handshake to confirm the endpoint is yours. With Flask or FastAPI you set up this route in minutes.

Second, event reception: each incoming message arrives as a POST with a JSON payload structured in entry[].changes[].value.messages[]. From each message you extract the sender's number (from), type (text, image, document, interactive, location), and content. A text message brings the body field; an image brings an id you must download with another API call.

Security: Meta signs each notification with an X-Hub-Signature-256 header. You must verify this signature using your application's secret to confirm the request really comes from Meta and not from an attacker. In Python, this is resolved with hmac and hashlib comparing the SHA-256 hash of the payload.

The golden webhook rule: respond with HTTP 200 immediately. Process business logic asynchronously -- in a secondary thread, a Celery task, or an internal queue. If you take more than 20 seconds to respond, Meta considers your server failed and retries, causing duplicate messages and state problems.

Creating a Basic Chatbot

With sending and receiving working, building a chatbot is connecting both flows with intermediate logic. The pattern is:

  1. You receive a message through the webhook.
  2. You analyze the user's intent. In its simplest version, keyword matching: if the message contains "order," you query order status; if it contains "hours," you return hours. For something more sophisticated, you integrate a language model like OpenAI's GPT-4o or Anthropic's Claude through their API, sending the conversation context and receiving a generated response.
  3. You query your internal data: database, CRM, order system, calendar.
  4. You compose the response and send it back through the Cloud API.

For an AI-powered chatbot, the flow adds an intermediate call to the language model. You send the conversation history as context, receive the generated response, format it, and send it via WhatsApp. This converts a rigid-flow chatbot into a conversational AI agent capable of understanding natural language, handling ambiguity, and resolving complex queries. If you want to automate this architecture with no-code tools, check how to do it with n8n and AI agents.

For real production -- with conversational memory management, connected tools, human escalation, and analytics -- a platform like GuruSup solves in minutes what developing from scratch takes weeks.

DIY Approach Limitations

Building the integration from scratch with Python is ideal for learning and prototyping. But for production it means maintaining a 24/7 webhook server, managing errors and retries, storing conversation state, implementing analytics, building multi-agent inbox, and designing human escalation. All that is code you maintain.

For specific connectors, the direct approach makes sense. For customer service at scale, a BSP provider with complete platform saves months of development.

Conclusion

Python + WhatsApp Cloud API give you total control over message sending, webhook reception, and chatbot building -- from simple flows to advanced AI agents. It's the technical foundation on which any serious integration is built. For the complete ecosystem overview, return to the WhatsApp Business API guide.

GuruSup converts all this architecture into a production-ready platform: AI agents on WhatsApp, integrations with your CRM, multi-agent inbox, and analytics. Without setting up servers, without managing webhooks. Start with GuruSup.

Related articles