Send WhatsApp messages with Java
Java 11's built-in HttpClient is enough to send WhatsApp messages — no extra dependencies. Drop it into a Spring Boot service or any JVM application.
Before you start
- Create a free account and copy your API key from Settings.
- Add a WhatsApp number and scan the QR code; copy its instance id.
- Replace
YOUR_API_KEYandYOUR_INSTANCE_IDin the examples below.
1. Setup
// Java 11+ — java.net.http is built in
2. Send a WhatsApp text message with Java
The recipient goes in to with the country code (for India, 91 followed by the 10-digit number). The response contains the message id and status.
send_text
import java.net.URI;
import java.net.http.*;
public class SendWhatsApp {
public static void main(String[] args) throws Exception {
String apiKey = "YOUR_API_KEY";
String instanceId = "YOUR_INSTANCE_ID";
String body = "{\"to\":\"919876543210\",\"type\":\"text\",\"message\":\"Hello from Java\"}";
HttpRequest request = HttpRequest.newBuilder(
URI.create("https://powerapiwa.com/api/v1/instances/" + instanceId + "/send"))
.header("x-api-key", apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
Response and errors
Successful calls return {"success": true, ...}. Errors return {"success": false, "error": "..."} with a status code: 401 invalid key, 402 plan expired, 422 invalid payload, 429 daily limit reached. See the full API documentation.