Access Whatsapp Business API Using Python

This tutorial will help to consume business WhatsApp API using python flask. You can handle all operations using rest API as like mobile app. WhatsApp is providing HTTP rest API to access its features.

I am just discovering WhatsApp API using python 3. WhatsApp is a fast, secure, and reliable way for businesses to reach their customers all over the world.

This Whatsapp Business API helps medium and large businesses to connect users in a simple, secure, and reliable way.

WhatsApp Business API Using Python

We will use WhatsApp Business API to get all group lists. You can interact with their customers using API WhatsApp Business API. As like other REST wrappers, The WhatsApp Business API also uses JSON format for request and response data.

I am assuming, You have a WhatsApp Business API account and hosted WhatsApp docker client into the machine. We just get the hostname URL of the WhatsApp client.

You can also read: How To Consume CloudFlare API Using Python

Let’s install Python dependency

We will use the requests package to handle HTTP requests in this application. You can install request and flask package using the following command –

Create API to Consume Whatsapp API

Let’s create a whatsapp.py file and add the below code into this file.

The above HTTP request includes the following components:-

  • URL — The URL of the server along with the API endpoint for the request.
  • Method — The action being requested of the endpoint. You can use methods like POST, GET, DELETE, PATCH, and PUT.
  • Headers — You can add Meta information about the request like request type, token etc.
  • Body — This contains the payload data.

The above call return with the response object, The response object will have all available groups with ids. The response as per docs as like below –

The Whatsapp Response Object will have the following properties –

  • HTTP Status Code — Status codes are issued by a server in response to a request made to the server. See HTTP Status Codes for more information.
  • Meta — Contains generic information such as WhatsApp Business API Client version.
  • Payload — Only returned with a successful response. The payload object name and contents vary based on the request. In the above examples, the payload is contacts because we sent a request to the contacts endpoint.
  • Errors — Only returned with a failed request. Contains an array of error objects that are present when there is an error. See the Error Codes for more information.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.