Skip to main content

Java HTTP Example

This complete program uses the standard library; it needs no third-party SDK dependency. Java 21. Save as Example.java; run java Example.java (or javac Example.java then java Example).

Set MAKAS_API_KEY to your full regional token and OPENAI_API_KEY to your provider key in the environment. Select your region above before copying. Run this only on your server/local machine, never a public browser bundle.

Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class Example {
    public static void main(String[] args) throws Exception {
        String body = """
            {"model":"gpt-4.1-mini","messages":[{"role":"user","content":"Say hello"}],"max_tokens":64}
            """;
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://eu.makas.ai/proxy/openai/v1/chat/completions"))
            .timeout(Duration.ofSeconds(30))
            .header("Content-Type", "application/json")
            .header("Makas-Api-Key", System.getenv("MAKAS_API_KEY"))
            .header("Authorization", "Bearer " + System.getenv("OPENAI_API_KEY"))
            .POST(HttpRequest.BodyPublishers.ofString(body)).build();
        try (HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build()) {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            if (response.statusCode() >= 400) throw new IllegalStateException("HTTP " + response.statusCode());
            // Print bounded provider JSON, including choices[0].message.content.
            System.out.println(response.body());
            System.out.println("Routed: " + response.headers().firstValue("Makas-Was-Routed").orElse("absent"));
        }
    }
}

The request is bounded to 64 output tokens; provider text varies. Header metadata is conditional. Check Request Logs and the header reference. For Anthropic or Google payload/header differences, use their complete quickstarts.