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 –

pip install requests
pip install flask

Create API to Consume Whatsapp API

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

from flask import Flask
from flask_restful import Resource, Api
from flask_cors import CORS
import requests

app = Flask(__name__)
CORS(app) ## To allow direct AJAX calls

@app.route('/getGroups', methods=['GET'])
def home():
   try:        
    headers = {'Authorization': 'Bearer your-auth-token', 'Content-Type': 'application/json'}
    res = requests.post('https://your-webapp-hostname:your-webapp-port/v1/groups', headers=headers)

    return r.json()

   except Exception as e:
    print('unable to get groups.')
    print(e.strerror)

if __name__ == '__main__':
   app.run(debug = True)

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 –

{
    "groups": [
       {"id": "group-id1"},
       {"id": "group-id2"}
    ]
}

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.