Java SDK

View as Markdown

Java SDK

A highly-performant, typed Java SDK for integrating MeshAPI into your Spring or Java 17+ applications.

Requirements

  • Java 17 or higher
  • Maven 3.6 or higher

Installation

Add MeshAPI as a dependency in your pom.xml:

1<dependency>
2 <groupId>com.meshapi.sdk</groupId>
3 <artifactId>meshapi-java-sdk</artifactId>
4 <version>0.1.0</version>
5</dependency>

Quick Start

Initialize the builder pattern provided by the MeshAPI class.

Standard Chat Completion

1MeshAPI client = MeshAPI.builder()
2 .baseUrl("https://api.meshapi.ai")
3 .token("rsk_...")
4 .build();
5
6ChatCompletionResponse resp = client.chat().completions().create(
7 ChatCompletionRequest.builder()
8 .model("openai/gpt-4o-mini")
9 .addMessage(ChatMessage.user("What is 2+2?"))
10 .build()
11);
12
13System.out.println(resp.choices.get(0).message.content); // "4"

Streaming Chat Completion

Streaming returns a structured Iterator.

1Iterator<ChatCompletionChunk> it = client.chat().completions().stream(params);
2
3while (it.hasNext()) {
4 ChatCompletionChunk chunk = it.next();
5 if (!chunk.choices.isEmpty() && chunk.choices.get(0).delta != null) {
6 System.out.print(chunk.choices.get(0).delta.content);
7 }
8}

Error Handling

Like other MeshAPI SDKs, retries on transient errors are handled for you automatically (up to 3 retries with a maximum 30s timeout and variable jitter, supporting Retry-After).

You can configure error limits manually:

1MeshAPI client = MeshAPI.builder()
2 .baseUrl("https://api.meshapi.ai")
3 .token("rsk_...")
4 .maxRetries(5) // 0 to disable
5 .timeoutMs(30000)
6 .build();

If an error is fatal or limits are breached, MeshAPIError is thrown:

1try {
2 resp = client.chat().completions().create(params);
3} catch (MeshAPIError e) {
4 System.out.println(e.getStatus()); // HTTP status
5 System.out.println(e.getErrorCode()); // e.g. rate_limit_exceeded
6 System.out.println(e.getRequestId()); // Traceable request ID
7}

[!WARNING] Streams do not automatically retry! Ensure your while(it.hasNext()) loop captures MeshAPIErrors representing drops, such as the stream_interrupted error code.