WABridges
← Docs INSTALL PROVISION PAIR SEND RECEIVE
API Reference
Docs / Quickstarts / Java

JAVA QUICKSTART

Send your first WhatsApp message in under 5 minutes. You need a WABridges API key - get one from the dashboard.

1. INSTALL

No dependencies required. The examples use java.net.http.HttpClient (Java 11+) and org.json for JSON. Add to pom.xml:

XML
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20240303</version>
</dependency>
<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-core</artifactId>
    <version>2.9.4</version>
</dependency>
ENV
export WA_API_KEY="sk_..."

2. PROVISION A BRIDGE

One bridge = one WhatsApp number. customer_ref is your internal identifier (user ID, slug, etc.).

JAVA
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;

public class WaBridges {
    static final String API_KEY = System.getenv("WA_API_KEY");
    static final String BASE    = "https://wabridges.com/api";
    static final HttpClient HTTP = HttpClient.newHttpClient();

    static JSONObject waPost(String path, JSONObject body) throws Exception {
        HttpRequest req = HttpRequest.newBuilder()
            .uri(URI.create(BASE + path))
            .header("Authorization", "Bearer " + API_KEY)
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(body.toString()))
            .build();
        HttpResponse<String> res = HTTP.send(req, HttpResponse.BodyHandlers.ofString());
        return new JSONObject(res.body());
    }

    public static void main(String[] args) throws Exception {
        JSONObject bridge = waPost("/instances", new JSONObject()
            .put("customer_ref", "user-123")
            .put("webhook_url",  "https://yourbackend.com/hook"));
        System.out.println(bridge);
    }
}
RESPONSE
{"id": "7d09aa9c-…", "customer_ref": "user-123", "state": "running", "created_at": 1777180605, "webhook_secret": "whs_…"}

3. PAIR A PHONE

Request a pairing code. Open WhatsApp on the phone, go to Linked Devices → Link a device → Link with phone number, then enter the code.

JAVA
JSONObject result = waPost("/instances/user-123/proxy/pair",
    new JSONObject().put("phone", "15550001234"));
System.out.println("Enter this code on the phone: " + result.getString("code"));
RESPONSE
{"code": "ABCD-EFGH"}

Once paired, the bridge fires a connected webhook event. Poll /proxy/status or wait for the event.

4. SEND A MESSAGE

JAVA
JSONObject msg = waPost("/instances/user-123/proxy/send/text",
    new JSONObject()
        .put("chat", "15559876543")
        .put("body", "Hello from the API!"));
System.out.println(msg.getString("message_id"));
RESPONSE
{"message_id": "ACE41E...", "timestamp": 1777180605}

Production tip: add an Idempotency-Key: <unique-per-message> header so a timeout + retry can never double-send — see Idempotency.

5. RECEIVE EVENTS

Inbound messages and events are delivered via POST to your webhook_url. Return 200 immediately.

JAVA
import static spark.Spark.*;
import org.json.JSONObject;

public class WebhookServer {
    public static void main(String[] args) {
        port(3000);
        post("/hook", (req, res) -> {
            res.status(200);
            JSONObject payload = new JSONObject(req.body());
            if ("message".equals(payload.optString("event")) && !payload.optBoolean("from_me")) {
                System.out.printf("%s: %s%n",
                    payload.optString("name"),
                    payload.optString("body"));
            }
            return "";
        });
    }
}

See the webhook guide for all event types and fields.