The official Java SDK for TrustedRouter. It works from Java, Kotlin, Android, and any JVM application.
The SDK provides:
- OpenAI compatible chat completions and streaming
- OpenAI Responses API and typed Responses SSE events
- Anthropic Messages and embeddings
- Synth, Advisor, Selector, MapReduce, and Subagent orchestration tools
- Explicit ZDR, confidential/E2E, US, EU-alias, provider-order, price, latency, and throughput routing
- Typed errors that distinguish router failures from provider failures
- Billing, credits, activity, models, providers, and Broadcast destinations
- OAuth credit delegation with PKCE and Android deep link validation
- Google Confidential Space attestation verification
- Java 17 bytecode,
java.securityEd25519 receipt verification, and aCompletableFutureasync facade
Inference uses https://api.trustedrouter.com/v1. Control operations use
https://trustedrouter.com/v1. The SDK keeps those planes separate so prompt traffic never
silently falls back to the control plane.
Publication status: v0.4.0 is a source release. It is not yet available from Maven Central. Publication is blocked on the repository's Maven Central credentials and signing key. Until those are configured, install the tagged source into your local Maven repository:
git clone --branch v0.4.0 --depth 1 https://github.com/Lore-Hex/trusted-router-java.git
cd trusted-router-java
./gradlew check publishToMavenLocal -PlocalPublicationThe dependency coordinates below work with that local installation. Gradle consumers must
include mavenLocal() in their repositories.
Gradle:
dependencies {
implementation("com.trustedrouter:trusted-router:0.4.0")
}Maven:
<dependency>
<groupId>com.trustedrouter</groupId>
<artifactId>trusted-router</artifactId>
<version>0.4.0</version>
</dependency>Generate the API reference locally with ./gradlew javadoc -PlocalPublication.
The publication includes source and Javadoc JARs for IDE navigation and hover documentation.
The JPMS automatic module name is com.trustedrouter.sdk.
The JVM artifact requires JDK 17. Ed25519 entered the standard JDK provider in JDK 15; the project
uses its existing JDK 17 build toolchain as the published runtime floor instead of adding a second
cryptography provider. Android consumers need a platform/toolchain that supplies the same
java.security Ed25519 algorithms. The artifact includes consumer R8/ProGuard rules that preserve
its Gson wire models in minified release builds.
Set TRUSTEDROUTER_API_KEY in the environment. Do not put a production API key in source code.
import com.trustedrouter.TrustedRouterClient;
import com.trustedrouter.models.ChatCompletion;
import com.trustedrouter.requests.ChatRequest;
TrustedRouterClient client = new TrustedRouterClient(
System.getenv("TRUSTEDROUTER_API_KEY"));
ChatCompletion completion = client.chatCompletions(ChatRequest.builder()
.model("trustedrouter/auto")
.message("user", "Reply with PONG")
.maxTokens(64)
.build());
System.out.println(completion.firstText());
System.out.println(completion.getUsage().getCostMicrodollars());The complete compiling example is in examples/java/Quickstart.java.
The SDK has a normal Java API, so Kotlin needs no wrapper. This coroutine example
also uses org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2 in the application:
import com.trustedrouter.TrustedRouterClient
import com.trustedrouter.requests.ChatRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
val client = TrustedRouterClient(System.getenv("TRUSTEDROUTER_API_KEY"))
val completion = withContext(Dispatchers.IO) {
client.chatCompletions(
ChatRequest.builder()
.model("trustedrouter/fast")
.message("user", "Reply with PONG")
.maxTokens(64)
.build()
)
}
println(completion.firstText())Alternatively, use client.async().chatCompletions(request) to receive a
CompletableFuture<ChatCompletion>.
Never embed a reusable server API key in an APK. It can be extracted. For a user-facing app, use TrustedRouter OAuth credit delegation so each user authorizes a limited key.
OAuthAuthorizeOptions options = OAuthAuthorizeOptions
.builder("myapp://oauth/trustedrouter")
.keyLabel("My Android app")
.limit("5.00")
.usageLimitType("monthly")
.build();
TrustedRouterClient publicClient = new TrustedRouterClient(
TrustedRouterOptions.builder().build());
OAuthAuthorization authorization = publicClient.createOAuthAuthorization(options);
// Open authorization.getUrl() in a Custom Tab and retain authorization securely.When Android returns to the app, convert the Intent data to a java.net.URI
named callbackUri:
OAuthCallback callback = OAuth.parseCallback(
callbackUri.toString(), authorization.getState());
OAuthToken token = publicClient.exchangeOAuthKey(
callback.getCode(),
authorization.getCodeVerifier(),
authorization.getCodeChallengeMethod());
TrustedRouterClient userClient = new TrustedRouterClient(token.getKey());OAuth.parseCallback rejects state mismatch, denial, malformed callbacks, and missing codes.
Store the delegated key with Android Keystore backed storage. Do not log it.
Streams are blocking and closeable. Read them on a worker thread and always use try-with-resources.
ChatRequest request = ChatRequest.builder()
.model("z-ai/glm-5.2")
.message("user", "Explain TLS exporters in three paragraphs")
.build();
try (TextStream stream = client.chatCompletionsText(request)) {
for (String text = stream.read(); text != null; text = stream.read()) {
System.out.print(text);
}
}For content, thinking, tools, and usage, consume typed chunks:
try (EventStream<ChatCompletionChunk> stream = client.chatCompletionsChunks(request)) {
for (ChatCompletionChunk chunk = stream.read(); chunk != null; chunk = stream.read()) {
for (ChatCompletionChunk.Choice choice : chunk.getChoices()) {
String thinking = choice.getDelta().getReasoning();
String content = choice.getDelta().getContent();
if (thinking != null) System.out.print(thinking);
if (content != null) System.out.print(content);
}
if (chunk.getUsage() != null) {
System.out.println("cost microdollars="
+ chunk.getUsage().getCostMicrodollars());
}
}
}The parser requires the final [DONE] marker. A truncated or empty SSE connection raises an
InternalException instead of looking like a successful empty answer. Each SSE line and complete
frame is capped at 1 MiB before a Java String is allocated.
ResponseObject response = client.responses(ResponsesRequest.builder()
.model("moonshotai/kimi-k3")
.instructions("Be concise")
.input("Give me three names for a database migration tool")
.parameter("store", false)
.parameter("max_output_tokens", 300)
.build());Streaming:
ResponsesRequest request = ResponsesRequest.builder()
.model("moonshotai/kimi-k3")
.input("Give me three names for a database migration tool")
.build();
try (EventStream<ResponseEvent> events = client.responsesEvents(request)) {
for (ResponseEvent event = events.read(); event != null; event = events.read()) {
if ("response.output_text.delta".equals(event.getEvent())) {
System.out.print(event.getData().get("delta").getAsString());
}
}
}Use responsesInputTokens(request) for stateless input token counting.
Use the normal OpenAI content-part shape. Public HTTPS URLs and base64 data URLs are accepted by the TrustedRouter gateway and normalized inside the attested prompt path.
JsonArray content = new JsonArray();
JsonObject text = new JsonObject();
text.addProperty("type", "text");
text.addProperty("text", "What is in this image?");
content.add(text);
JsonObject image = new JsonObject();
image.addProperty("type", "image_url");
JsonObject imageUrl = new JsonObject();
imageUrl.addProperty("url", "https://example.com/photo.jpg");
image.add("image_url", imageUrl);
content.add(image);
JsonObject message = new JsonObject();
message.addProperty("role", "user");
message.add("content", content);
ChatCompletion result = client.chatCompletions(ChatRequest.builder()
.model("google/gemini-3.1-flash-image-preview")
.message(message)
.build());The SDK does not impose an artificial request-size limit and has a regression test for a 1.25 MB multimodal payload. The gateway and selected model still enforce their documented limits.
Tool definitions pass through with the OpenAI shape. Your application executes its own tools.
JsonObject function = new JsonObject();
function.addProperty("name", "get_weather");
function.addProperty("description", "Get weather for a city");
JsonObject parameters = new JsonObject();
parameters.addProperty("type", "object");
JsonObject properties = new JsonObject();
JsonObject city = new JsonObject();
city.addProperty("type", "string");
properties.add("city", city);
parameters.add("properties", properties);
function.add("parameters", parameters);
JsonObject tool = new JsonObject();
tool.addProperty("type", "function");
tool.add("function", function);
ChatCompletion result = client.chatCompletions(ChatRequest.builder()
.model("moonshotai/kimi-k3")
.message("user", "What is the weather in Miami?")
.tool(tool)
.build());Read result.getChoices().get(0).getMessage().getToolCalls() and send the tool result in the next
request.
FusionRequest is the typed Java representation of TrustedRouter Synth. The older Fusion name is
retained for API compatibility.
FusionRequest request = FusionRequest.builder()
.message("user", "Review this migration plan for correctness")
.analysisModels(Arrays.asList(
"minimax/minimax-m3",
"~kimi/latest",
"~zai/glm-latest"))
.judgeModel("minimax/minimax-m3")
.fallbackJudges(Arrays.asList("~kimi/latest"))
.fallbackFinalModels(Arrays.asList("~zai/glm-latest"))
.selectionStrategy(TrustedRouter.SELECTION_SYNTHESIZE_NON_REFUSALS)
.panelPrompt("Identify concrete defects")
.synthesisPrompt("Return one actionable answer")
.build();
ChatCompletion answer = client.synth(request);Synth gets a 10 minute SDK timeout unless the call supplies an explicit timeout.
The atomic orchestration tool helpers are also available:
ChatRequest.builder().advisor(advisorParameters);
ChatRequest.builder().fusion(synthParameters);
TrustedRouter.orchestrationTool("trustedrouter:selector", selectorParameters);
TrustedRouter.orchestrationTool("trustedrouter:mapreduce", mapReduceParameters);Typed filters avoid misspelled privacy promises:
ChatRequest request = ChatRequest.builder()
.model("z-ai/glm-5.2")
.message("user", "Review this contract clause")
.provider(ProviderPreferences.confidential())
.build();Available hard filters:
ProviderPreferences.zeroDataRetention(); // provider.min_privacy = "zdr"
ProviderPreferences.confidential(); // confidential compute plus provider E2EE
ProviderPreferences.unitedStates(); // US-headquartered providerCompose routing controls when needed:
ProviderPreferences preferences = ProviderPreferences.builder()
.only("tinfoil", "phala")
.order("tinfoil", "phala")
.minimumPrivacy("confidential")
.allowFallbacks(true)
.sort("throughput")
.usage("credits")
.build();confidential is stronger than ZDR. It requires provider-side confidential compute and
provider-side end-to-end encryption and fails closed when no eligible route exists.
TrustedRouterClient client = new TrustedRouterClient(TrustedRouterOptions.builder()
.apiKey(System.getenv("TRUSTEDROUTER_API_KEY"))
.workspaceId("workspace-id")
.maxRetries(2)
.build());
CallOptions call = CallOptions.builder()
.workspaceId("different-workspace")
.idempotencyKey("job-42-step-7")
.build();Workspace overrides are sent as X-TrustedRouter-Workspace, never in the prompt body. Mutating
SDK operations receive an automatic idempotency key when the caller does not provide one. Retries
use exponential backoff with jitter and honor numeric Retry-After values.
DEFAULT_API_BASE_URL is one name on one DNS provider, and the domain sits above every cloud
behind it. A zone that stops answering, a registrar lock, or a resolver handing out a stale record
takes the API down no matter how many regions are healthy.
ALIAS_API_BASE_URLS — api.allyrouter.com and api.uptimerouter.com — are exact aliases of the
primary, on separate domains served by separate DNS providers, resolving to the same attested
enclaves. The transport walks them in order after the primary, so a healthy deployment never
touches them. Nothing to configure; it is on by default.
Failover changes host only on connection failures and on 502, 503, or 504. A 500 means a
server received and processed the request. You are not charged twice for it — authorization is
idempotent per Idempotency-Key and settlement happens once — but the work would run a second
time, so the answer could differ and TrustedRouter pays the provider again. A 500 is retried on
the same host.
Aliases are used only for the default baseUrl. A custom one — a private deployment, a test
server, a regional pin — is never rewritten. Call .regionalFailover(false) to keep every attempt
on a single host. The control plane always uses the single configured controlBaseUrl.
The SDK reports content-free reliability facts so TrustedRouter can measure availability from
the client's side (contract v1, docs/client-telemetry.md in Lore-Hex/quill-router). Two
channels, both closed-vocabulary by construction — no prompts, outputs, raw hostnames, keys, or
user identifiers ever leave (request correlation uses the bounded server-issued rlog id):
- Header — every inference attempt carries
x-tr-client(v=1;a=0;s=0, plus the previous attempt's outcome on retries). - Beacon — a daemon thread, started on the first recorded call, posts bounded batches of
exact per-minute counters and sampled request events to
POST https://trustedrouter.com/v1/client-eventsfrom its own single-shot HTTP client. It never rides the engine, the retry loop, or an injectedOkHttpClient, and it never retries a flush. Failures, retried calls, and calls slower than 30 s are always kept; healthy first-attempt successes are sampled attelemetrySampleRate(default 1%).
TrustedRouterClient client = new TrustedRouterClient(TrustedRouterOptions.builder()
.apiKey(System.getenv("TRUSTEDROUTER_API_KEY"))
.telemetry(Boolean.TRUE) // explicit; default follows the rules below
.telemetrySampleRate(0.01) // random sampling of healthy successes
.build());
try {
client.chatCompletions(ChatRequest.builder().message("user", "hi").build());
} finally {
client.close(); // one bounded (2 s) flush, then the worker stops
}Telemetry defaults on only for TrustedRouter's own inference and control hosts; custom base
URLs default off. If telemetry is explicitly enabled with a custom inference URL, its raw
hostname is never emitted (the host field is the closed-vocabulary unknown). Precedence:
telemetry(...) >
TRUSTEDROUTER_TELEMETRY=0|false|off|no > DO_NOT_TRACK=1 > default. Opting out disables the
header and the beacon together. Set TRUSTEDROUTER_TELEMETRY_DEBUG=1 to echo every batch to
stderr before it is sent. close() (or try-with-resources) performs the final flush; a JVM
shutdown hook does the same for clients that were never closed.
try {
client.chatCompletions(request);
} catch (RateLimitException error) {
System.err.println(error.getLayer()); // router, gateway, or provider
System.err.println(error.getSource()); // selected upstream when supplied
System.err.println(error.getRetryAfterSeconds());
} catch (AuthenticationException error) {
// Invalid or disabled TrustedRouter key.
} catch (TrustedRouterException error) {
// Other typed API, gateway, provider, or transport failure.
}Error payloads are preserved. Prompts, outputs, API keys, and BYOK keys are never added to SDK logs because the SDK has no request-body logger.
Costs are integer microdollars in response usage. Checkout amounts are decimal strings. Do not use
double for billing:
Long costMicrodollars = completion.getUsage().getCostMicrodollars();
CheckoutResponse checkout = client.billingCheckout(BillingCheckoutRequest.builder()
.amount("25.000001")
.paymentMethod("stripe")
.build());TrustRelease release = client.trustRelease();
AttestationPolicy policy = AttestationPolicy.fromTrustRelease(release);
GatewayAttestation attestation = client.verifyGatewayAttestation(policy);The verifier checks the Google signature, issuer, audience, expiration, production debug state, Confidential Space software identity, Secure Boot, confidential hardware class, published image digest/reference, a fresh nonce, and the TLS leaf certificate returned by the exact attestation request. Debug Confidential Space workloads are rejected by default.
AttestationVerifier.verify also accepts caller-supplied RFC 9266 TLS exporter bytes through
AttestationVerificationOptions.tlsExporter(...). Java runtimes before JDK 25 do not expose a
standard TLS exporter API, so applications requiring exporter-bound verification should supply
the exporter from their TLS provider or use the TrustedRouter Go verifier. Do not describe a
certificate-only Java verification as exporter-bound.
ReceiptVerifier verifies compact or flattened inference-receipt JWS values in fail-closed order.
The expected issuer is required and pinned after canonical HTTPS-origin normalization (lowercase
host, no default port or trailing slash). Pass the exact bytes held by the caller; JSON and SSE
content are never normalized:
ReceiptVerificationOptions options = ReceiptVerificationOptions.builder(
"https://api.trustedrouter.com")
.requestBody(requestBytes)
.responseBody(responseBytes)
.attestationDocument(attestationBytes)
.expectedNonce(nonce)
.maxAgeSeconds(300)
.build();
ReceiptClaims claims = ReceiptVerifier.verifyReceipt(compactJws, options);Request and response bindings are required by default. For deliberate signature-only inspection,
pass requireBindings(false); otherwise missing traffic throws MissingBindingException. An
invalid or mismatched issuer throws ReceiptIssuerException. Attestation verification is
independently required by default.
Compact receipts carry only an attestation hash, so callers must pass the exact pinned evidence with
attestationDocument(...) or explicitly select verification without attestation with
requireAttestation(false); both escape hatches must be explicit when neither traffic nor
attestation evidence is supplied. The
/receipt-attestation endpoint serves a per-instance document: callers should retry that fetch
until its SHA-256 matches the receipt's att_sha256 claim. A document supplied for a flattened
receipt must exactly match its embedded evidence. For streaming responses, wrap the raw response
InputStream in ReceiptCapture, read through the wrapper, then call verify(...); it retains the
exact SSE wire bytes, discovers the flattened receipt event, and checks its position and hash.
For GCP evidence, the default path reads the public trust release and Google's JWKS. The
gcpAttestationOptions(...) builder method accepts the existing attestation options so callers can
supply release pins and pre-fetched JWKS for a fully offline check. An explicitly declared unknown
attestation kind always fails with UnsupportedAttestationException; it is never silently skipped.
Typed response objects retain the complete top-level JSON through getRaw(). Request builders
accept arbitrary JSON parameters for newly introduced provider fields. Low-level request and
rawRequest methods are also available.
Low-level paths must be relative. This prevents an accidental API-key-bearing request to a foreign absolute URL. Status and trust metadata have dedicated credential-free absolute fetches.
./gradlew clean check javadoc -PlocalPublicationCI compiles with JDK 17 and emits Java 17 bytecode. It runs on Linux, macOS, and Windows, compiles
all Java and Kotlin examples against the built JAR, extracts README/docs code fences,
checks Javadocs with full doclint and zero warnings, and enforces the starting coverage floor.
check also publishes to Maven Local, asserts all publication file lists and metadata,
and runs a temporary Gradle consumer against a loopback fake server.
-PlocalPublication allows unsigned local verification; release publication still requires signing.
The credential-free production trust smoke verifies public status, release metadata, and a fresh gateway attestation:
./gradlew runPublicTrustSmoke./gradlew runAuthenticatedSmoke additionally verifies chat and Responses, streaming and
non-streaming, when TRUSTEDROUTER_API_KEY is set. These live smokes are separate from deterministic
CI tests.
See PARITY.md for endpoint coverage and RELEASING.md for Maven Central release setup.
Apache License 2.0. See LICENSE.