Whatsapp API send message Java

How to Send a Message by WhatsApp API using Java


Given the universality of Java in application development, many developers seek to leverage it for interacting with WhatsApp. In this succinct guide, we'll demonstrate how to employ Java, alongside the WhatsApp API and OkHttpClient, to dispatch WhatsApp messages effortlessly.

Whapi.Cloud is a versatile API provider that facilitates the integration of WhatsApp messaging features into various applications. It caters to the transmission of text, media, and documents, offering a comprehensive suite for developers' needs. For this illustration, we’ll use OkHttpClient, a powerful and efficient HTTP client, to interact with the Whapi.Cloud API.


The first step is to register on the Whapi.Cloud website and create an account. It's free and doesn't require you to enter a credit card.

After registration you will immediately have access to a test channel with a small limitation. Wait for it to start (it usually takes about a minute). You will need to connect your phone for Whatsapp automation. It is from the connected phone that messages will be sent.

The big advantage of the service is that it takes only a couple of minutes to launch and start working.

Open your WhatsApp mobile app, navigate to Settings -> Linked devices -> Link a device -> Scan QR code

Our service enables you to interact with the API without the need for an uninterrupted phone connection. Operate WhatsApp on up to four paired devices and one mobile device concurrently.

Now save the generated API-Key token on channel page:

Get Started Free

How to Send WhatsApp API Message using Java

To start, ensure your Java development environment is primed and ready. We will utilize the OkHttpClient, which requires an additional library to be added to your project. If you are using Maven, you can integrate it by appending the following dependency to your pom.xml file:

    <version>4.9.0</version> <!-- Replace with the version you are using -->

We offer a convenient service for testing requests and using ready-made code fragments in any programming language. You will only need to transfer the saved API-token and fill in the required parameters:

With the OkHttpClient library in place, you can craft a Java class or method to send WhatsApp messages. Here's a snippet demonstrating how to implement this using Java:

import okhttp3.*;

import java.io.IOException;

public class WhatsAppMessageSender {
    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\"typing_time\":0,\"to\":\"1234567891@s.whatsapp.net\",\"body\":\"Hello, World!\"}");
        Request request = new Request.Builder()
                .addHeader("accept", "application/json")
                .addHeader("content-type", "application/json")

        Response response = client.newCall(request).execute();
  • 'to': The recipient’s WhatsApp number with international format e.g., 14409416972 or chatID for contact or group e.g 14409416972@s.whatsapp.net or 1203234567890@g.us

  • 'body' : Message text.

Please note that this is a basic example and doesn't include any error handling. For a production environment, ensure to add appropriate error and exception handling.

Employing Java in tandem with OkHttpClient and the Whapi API offers a streamlined approach to integrating WhatsApp messaging functionalities into your applications. This integration opens up myriad possibilities for enhancing communication and user interaction in your development projects. Always ensure to refer to the official documentation of the tools and libraries you are using for the most accurate and up-to-date information.

Make sure to explore the Whapi.Cloud API documentation for more features and possibilities

In our comprehensive documentation, you'll discover step-by-step instructions and examples for utilizing methods that empower you to send diverse content, ranging from files of any format, locations, and contacts to stickers and in-message buttons.

Moreover, you can interact dynamically with emoji messages by quoting, marking them as read, or simulating real-time typing.

Furthermore, you can efficiently manage your groups by changing avatars, renaming them, inviting new members, or blocking unwanted users.

Send Media Message

Please don't hesitate to reach out to share your experiences or pose any questions you might have.

Get Started Free

Send Document

Send Audio

Send Voice

You can Send a ppt audio recording to the phone number or group, But WhatsApp is sensitive to this extension, you need to be in OGG format and the codecs should be the opus, For more information, you can check out this article.

Send Video

Send GIF

Send Contact

Send Contact List

Send Location

Send Sticker

Last updated