How To Use the WhatsApp API with Python

Developerking
7 min readJan 22, 2022

Overview of the WhatsApp API

The Maytapi WhatsApp API is an unofficial WhatsApp API. With this API, you can send messages on behalf of a WhatsApp number. You can also see all the WhatsApp messages received by the number.

Just like the WhatsApp web app, which runs a parallel WhatsApp session that mirrors the WhatsApp app running on a phone, the Maytapi WhatsApp API also runs a parallel session. This allows you to build programmable conversations workflows for applications such as bots. However, with this API, you can achieve much more than a typical bot.

To get started with the Maytapi WhatsApp API, follow the steps below to sign up for a RapidAPI account and subscribe to the API.

1. Sign up for RapidAPI Account

To access the Maytapi WhatsApp API, you’ll first have to sign up for a free RapidAPI developer account. With this account, you get a universal API Key to access all APIs hosted in RapidAPI.

RapidAPI is the world’s largest API marketplace, with over 10,000 APIs and a community of over 1,000,000 developers. Our goal is to help developers find and connect to APIs to help them build amazing apps.

2. Access the WhatsApp API

Once signed in, log in to your RapidAPI account.

To access the API console of Maytapi WhatsApp API, you can either search with the query “Maytapi whatsapp api,” or alternatively, you can directly access the API console here.

3. Subscribe to the WhatsApp API

Once inside the API console, click on the “Pricing” tab to access the pricing plan.

Maytapi WhatsApp API is a paid API. It has a Pro plan which gives you unlimited API usage for USD 29.00 per month.

Make sure that you enter your billing details correctly and subscribe to this API. You will be billed immediately for the API subscription and taken back to the API console.

Connect to the WhatsApp API

How to use the WhatsApp API

To use Maytapi WhatsApp API, you first have to set it up to mirror a WhatsApp account associated with a phone number. This phone number is the designated phone number whose WhatsApp account is controlled by the API. To test the message sending capabilities of the API, you should also have a second phone with an active WhatsApp account.

Follow these steps to set up the designated phone number and test the API.

Prerequisites

Before you proceed, make sure that

  1. Both the designated phone and the second phone have an active WhatsApp account associated with the respective phone numbers, and the numbers are known to you.
  2. The WhatsApp account of the designated phone number is accessible and active on the WhatsApp web.
  3. You have access to both the phones and can check the WhatsApp messages on them.
  4. Both phones are connected to the Internet.

Step 1: Add Phone Number

The very first step is to add the phone number to your Maytapi WhatsApp API. This is achieved by the “POST addPhone” API endpoint under the Account Information Retrieval & Edit category of endpoints.

The request body accepts a JSON object with the key “number”. Set it’s value to the phone number of the designated phone number. Make sure to add the country code, without the ‘+’ sign.

Trigger the API, and you should get a success response as follows.

This response contains a phone id assigned to the new phone number. Take note of this phone id as it will be used for other API calls.

Note: If you have already added a phone number then you must remove it through the “POST deletePhone” endpoint. The Maytapi WhatsApp API allows you to add only one phone number at a time.

Step 2: Get Phone Id Status

After the phone number is added, you can check the status of the newly generated phone id. To do this, trigger the “GET getStatus” endpoint under the Session Information Getters category of endpoints.

Set the value of the ‘phone_id’ parameter with the phone id generated in the previous step and trigger the API.

The API returns a response containing several status flags for various operations of the API, that is related to the WhatsApp account of the phone number.

As you can see, the value of ‘isQR’ is set to true. It means that the API is expecting you to scan the QR code from the WhatsApp app to activate the WhatsApp session through the API.

Related: How to add QR Code Functionality to your App

Step 3: Scan QR Code to Activate Phone Number

With the phone number added to the API, you must now activate it via the API. This process is essential because it establishes the association between the API and the phone number through the phone device.

It is similar to how a WhatsApp web session starts by scanning the QR code of the WhatsApp web app on the WhatsApp phone app. In the case of the Maytapi WhatsApp API, you generate a QR code using the “GET getQRCode” endpoint under the Session Information Getters category of endpoints.

Set the value of the ‘phone_id’ parameter with the phone id generated in the first step and trigger the API.

The API response returns a QR code.

Scan this code with the WhatsApp app of the designated phone. Now the API is linked with the WhatsApp account of the phone.

Note: The QR code is valid for a limited time. Hence it should be scanned as soon as it is generated. In case the app displays an error, make sure that the phone is connected to the Internet, and the WhatsApp web session of this phone is already active on a browser.

Step 4: Verify Linkage with Phone Number

This step is the same as step 2. Here you invoke the “GET getStatus” endpoint again to get the status.

Since the WhatsApp app on the phone is now associated with the API, you get a different status now.

You get the value of “loggedIn” as true, and you should also see the phone number of the designated phone. This means that the phone number, ending with the digits 177 is now associated with the Maytapi WhatsApp API and is logged in.

You are now ready to initiate WhatsApp conversation for this number via the API.

Step 5: Sending WhatsApp Message

At this point, your designated phone number is linked to the Maytapi WhatsApp API. Now you can send a message with another WhatsApp number from the API itself, without using the WhatsApp app on the phone or through the WhatsApp web session.

Go ahead and select the “POST sendMessage” endpoint under the Message Sending Operations category of endpoints.

Enter the phone id as the value for ‘phone_id’ parameter. In the JSON structure for the Request Body, set the ‘to_number’ as the phone number of the second phone that you have. Trigger the API.

The API response shows a success message with the message id.

The real proof of this operation lies in seeing the message “Hello” on the WhatsApp app of the second phone. This message should appear within a few seconds.

Step 6: Receiving WhatsApp Message

Through the Maytapi WhatsApp API, you can get a log of all the messages that are sent to the API designated phone number. However, the log also includes the API calls that are triggered, including the ones used for adding the phone number and scanning the QR code to connect the API with the phone number.

Select the ”GET logs” API endpoint under the Session Information Getters category on endpoints.

It accepts only one parameter, which is ‘page’. The value of this parameter signifies the page number of the logs. This is done to ensure that the logs are arranged and returned by the API response in a paginated manner.

Set the value of ‘page’ as 1 and trigger the API.

--

--

Developerking

We are creating the world's largest community of developers & Share knowledgeable contents , So Stay Tuned & Connect with us !! Become a Dev King Member.