Java Guide

Java integration

Java 11+ using java.net.http.HttpClient and the built-in JSON encoder — no third-party dependencies.

1. Set up the client

import java.net.URI;
import java.net.http.*;
import java.util.Map;

public class EthioLink {
    private static final String BASE = "https://api.ethiolink-consent.et";
    private final HttpClient http = HttpClient.newHttpClient();
    private final String apiKey;

    public EthioLink(String apiKey) { this.apiKey = apiKey; }

    public String call(String method, String path, Map<String,Object> body) throws Exception {
        HttpRequest.Builder b = HttpRequest.newBuilder(URI.create(BASE + path))
            .header("Authorization", "Bearer " + apiKey)
            .header("Content-Type", "application/json");
        if (body != null) b.method(method, HttpRequest.BodyPublishers.ofString(encode(body)));
        else b.method(method, HttpRequest.BodyPublishers.noBody());
        return http.send(b.build(), HttpResponse.BodyHandlers.ofString()).body();
    }

    private static String encode(Map<String,Object> m) {
        // JSON: {"key":"value"} — use any tiny helper or a library of your choice
        StringBuilder s = new StringBuilder("{");
        m.forEach((k, v) -> s.append('"').append(k).append("\":\"")
                 .append(v).append("\","));
        return s.substring(0, s.length() - 1) + "}";
    }
}

2. Request consent

EthioLink el = new EthioLink(System.getenv("ETHIOLINK_API_KEY"));
String res = el.call("POST", "/api/consent/request", Map.of(
    "user_fayda_id", "FAYDA-847291",
    "purpose", "Loan eligibility assessment",
    "duration_days", "30"
    // categories are an array - use a real JSON library for production
));
// res -> {"ok":true,"request":{"public_id":"cr_..."}}

3. Verify a token before touching data

String q = java.net.URLEncoder.encode(token, java.nio.charset.StandardCharsets.UTF_8);
String verdict = el.call("GET",
    "/api/consent/active?token=" + q + "&reason=loan_application", null);
// Parse JSON: active:true/false, status, automation.objection

4. Retrieve encrypted data

String data = el.call("GET",
    "/api/data/retrieve?token=" + q + "&reason=loan_application", null);
// data.transfers[].encrypted_data -> decrypt with your envelope key

5. Webhook signature verification

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

boolean verify(String payload, String sig, String ts, String secret) throws Exception {
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(new SecretKeySpec(secret.getBytes(), "HmacSHA256"));
    byte[] expected = mac.doFinal((ts + "." + payload).getBytes());
    return java.security.MessageDigest.isEqual(expected,
        sig.getBytes(java.nio.charset.StandardCharsets.ISO_8859_1));
}

← All guides