Chat API Android SDK for WhatsApp API

Download SDK

You can customize this SDK to your needs and make it more convenient by clicking here:

WhatsApp Android SDK Generator

The SDK allows you to receive and send messages through your WhatsApp account. Sign up now

Requirements

Building the API client library requires Maven to be installed.

Installation

To install the API client library to your local Maven repository, simply execute:

mvn install

To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:

mvn deploy

Refer to the official documentation for more information.

Maven users

Add this dependency to your project's POM:

<dependency>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-android-client</artifactId>
    <version>1.0.0</version>
    <scope>compile</scope>
</dependency>

Gradle users

Add this dependency to your project's build file:

compile "org.openapitools:openapi-android-client:1.0.0"

Others

At first generate the JAR by executing:

mvn package

Then manually install the following JARs:

  • target/openapi-android-client-1.0.0.jar
  • target/lib/*.jar

Getting Started

Please follow the installation instruction and execute the following Java code:

import org.openapitools.client.api.Class1InstanceApi;

public class Class1InstanceApiExample {

    public static void main(String[] args) {
        Class1InstanceApi apiInstance = new Class1InstanceApi();
        try {
            InlineResponse2002 result = apiInstance.expiry();
            System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling Class1InstanceApi#expiry");
            e.printStackTrace();
        }
    }
}

Documentation for API Endpoints

All URIs are relative to https://api.chat-api.com

Documentation for Authorization

Authentication schemes defined for the API:

instanceId

  • Type: API key

  • API key parameter name: instanceId

  • Location: URL query string

token

  • Type: API key

  • API key parameter name: token

  • Location: URL query string

Recommendation

It's recommended to create an instance of ApiClient per thread in a multithreaded environment to avoid any potential issues.


Preparatory work

At the very beginning, we need to connect whatsapp with our script, so as we write the code, we check its operation. To do this, go to your personal account and get a QR code there. Next, open WhatsApp on your smartphone, go to Settings -> WhatsApp Web -> Scan a QR code.

Now we need to indicate a WebHook URL so the server can run the scrip when new messages arrive. Indicate a direct link to your script. You can’t indicate server IP only, but you can indicate the port.


The first steps for beginners

The generated code uses a few Gradle dependencies e.g., Jackson, Volley, and Apache HttpClient. The reference to these dependencies is already added in the build.gradle file will be installed automatically. Therefore, you will need internet access for a successful build.

  • In order to open the client library in Android Studio click on Open an Existing Android Project.
  • Browse to locate the folder containing the source code. Select the location of the WhatsappAPI gradle project and click Ok.
  • Upon successful import, the project can be built by clicking on Build > Make Project or pressing Ctrl + F9.

How to Use

The following section explains how to use the WhatsappAPI library in a new project.

1. Starting a new project

For starting a new project, click on Create New Android Studio Project.

Here, configure the new project by adding the name, domain and location of the sample application followed by clicking Next.

Following this, select the Phone and Tablet option as shown in the illustration below and click Next.

In the following step, choose Empty Activity as the activity type and click Next.

In this step, provide an Activity Name and Layout Name and click Finish. This would take you to the newly created project.

2. Add reference of the library project

In order to add a dependency to this sample application, click on the android button shown in the project explorer on the left side as shown in the picture. Click on Project in the drop down that emerges.

Right click the sample application in the project explorer and click on New > Module as shown in the picture.

Choose Import Gradle Project and click Next.

Click on Finish which would take you back to the sample application with the refernced SDK.

In the following step first navigate to the SampleApplication > settings.gradle file and add the line

Then navigate to the ``` SampleApplication >  app > build.gradle ``` file and add the following line

implementation project(path: ':WhatsappAPILib')``` to the dependencies section as shown in the illustration below. Also add the following packagingOptions.

packagingOptions exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE' exclude 'META-INF/DEPENDENCIES' }

Finally, press Sync Now in the warning visible as shown in the picture below.

3. Write sample code

Once the SampleApplication is created, a file named SampleApplication > app > src > main > java > MainActivity will be visible in the Project Explorer with an onCreate method. This is the entry point for the execution of the created project. Here, you can add code to initialize the client library and instantiate a Controller class. Sample code to initialize the client library and using controller methods is given in the subsequent sections.

How to Test

The generated code and the server can be tested using automatically generated test cases. JUnit is used as the testing framework and test runner.

In Android Studio, for running the tests do the following:

  1. Right click on SampleApplication > WhatsappAPILib > androidTest > java) from the project explorer.
  2. Select "Run All Tests" or use "Ctrl + Shift + F10" to run the Tests.

Other WhatsApp API SDKs

You can configure the SDK or generate other SDKs in 30+ programming languages: Chat API SDK generator.