How to send a WhatsApp message with Java
Send text and media messages from Java with a single HTTP request, then receive replies on your own webhook. Plain REST and JSON — no SDK, no template approval.
Last updated:
- Java
- REST + JSON
- no SDK needed
- webhook replies
// Java 15+ (java.net.http, text blocks)
import java.net.URI;
import java.net.http.*;
String pid = "your-pid";
String phoneId = "your-phone-id";
String token = "your-token";
String body = """
{"to_number": "1415xxxxx67", "type": "text", "message": "Hello from Java!"}
""";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.maytapi.com/api/" + pid + "/" + phoneId + "/sendMessage"))
.header("x-maytapi-key", token)
.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());What you need
Four things, all available in the console within a few minutes.
-
account
A Maytapi account
Sign up and start the free trial — no card required.
-
phone
A paired number
Scan the QR code with WhatsApp on your phone. Your number keeps working as usual.
-
token
pid and token
Copy them from the token page of the console; phone_id is shown next to your number.
-
Java
A Java environment
Any recent Java version with its standard HTTP client is enough.
Send a text message with Java
POST a JSON body to /sendMessage with your token in the x-maytapi-key header. The message is queued on your phone and the response returns immediately.
- ✓to_number in international format, e.g. 1415xxxxx67
- ✓type: text and the message body
- ✓The response contains the message id
curl -X POST "https://api.maytapi.com/api/$PID/$PHONE_ID/sendMessage" \
-H "x-maytapi-key: $TOKEN" \
-H "Content-Type: application/json" \
-d '{"to_number": "1415xxxxx67", "type": "text", "message": "Hello from Java!"}'{
"success": true,
"data": {
"chatId": "1415xxxxx67@c.us",
"msgId": "3EB065B353B9493D00C9"
}
}Send an image, video or document
Use the same endpoint with type media and a public file URL or a base64 data URI. Add an optional caption and file name.
- ✓Images, video, audio, PDF and other documents
- ✓Caption in the text field
- ✓Base64 data URIs work as well as URLs
String body = """
{
"to_number": "1415xxxxx67",
"type": "media",
"message": "https://example.com/invoice-10482.pdf",
"text": "Your invoice for order #10482",
"filename": "invoice-10482.pdf"
}
""";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.maytapi.com/api/" + pid + "/" + phoneId + "/sendMessage"))
.header("x-maytapi-key", token)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());Receive replies with a Java webhook
Register a webhook URL once. Incoming messages, delivery and read receipts arrive as JSON; answer through the reply URL included in each event.
- ✓Return 200 quickly, then process the event
- ✓Skip your own messages with fromMe
- ✓Use event.reply to answer in the same chat
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.maytapi.com/api/" + pid + "/setWebhook"))
.header("x-maytapi-key", token)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"webhook\": \"https://example.com/maytapi/webhook\"}"))
.build();
HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());@RestController
public class WebhookController {
private final RestClient client = RestClient.create();
@PostMapping("/maytapi/webhook")
public ResponseEntity<Void> webhook(@RequestBody Map<String, Object> event) {
Map<String, Object> message = (Map<String, Object>) event.get("message");
if ("message".equals(event.get("type")) && !Boolean.TRUE.equals(message.get("fromMe"))) {
client.post()
.uri((String) event.get("reply"))
.header("x-maytapi-key", token)
.contentType(MediaType.APPLICATION_JSON)
.body(Map.of(
"to_number", event.get("conversation"),
"type", "text",
"message", "Thanks! We received your message."))
.retrieve()
.toBodilessEntity();
}
return ResponseEntity.ok().build();
}
}Java WhatsApp API FAQ
Ask our support team anything over WhatsApp. Average response time is under 24 hours.
Help center →Is there an official Java SDK?
You do not need one. The API is plain REST with JSON, so the standard Java HTTP client is enough, as the examples on this page show.
Can I send messages to groups from Java?
Yes. Use the group id (ending in @g.us) as to_number; group management endpoints are available on every plan.
Why did my message not arrive?
Check that the phone status is active, then look at the queue and logs in the console. New numbers should be warmed up before sending in volume.
More WhatsApp API features

Get your key, send your first message in ten minutes
No approval, no templates, no card. Every endpoint is open for three days.