Available now. The Java SDK is published on Maven Central and works with the install command below. See the artifact page on Maven Central, which shows the latest version. If you prefer not to use it, you can also call the REST API from Java with any HTTP client (Call a tool over REST).
The Java SDK is the official Java client. Its methods, field names, allowed values and error codes are generated from the same API contract as the server, so they match the API.
Requirements and install
Java 11 or newer. It needs no other libraries.
<dependency>
<groupId>io.davixlabs</groupId>
<artifactId>h2i-sdk</artifactId>
<version>THE_LATEST_VERSION</version>
</dependency>
Create a client
Create an API key first (Create and rotate API keys) and keep it in an environment variable or a secrets manager, never in the code.
import io.davixlabs.h2i.*;
H2IClient h2i = new H2IClient(System.getenv("DAVIX_H2I_API_KEY"));
The address defaults to https://app.h2i.davixlabs.io. To change an option, build the client with H2IClient.builder(key)...build().
Run a tool
Response result = h2i.images().resize(Params.of(
"images", "photo.jpg",
"width", 800,
"resizeMode", Enums.ResizeFit.COVER));
System.out.println(result.status() + " " + result.requestId());
Tools are grouped like the API: h2i.images().resize(...), h2i.pdfs().merge(...), h2i.ai().documents().summarize(...). You pass the parameters as Params, using the names of the API reference.
Find every tool and its fields in the tool list; each tool page has an API reference.
The result
| What | Meaning |
|---|---|
result.data(), result.json() |
The answer: parsed JSON, text or bytes, and the JSON object |
result.status(), result.requestId() |
HTTP status and the id to quote to support |
result.files() |
Every result file in the answer, each with a signed url |
result.accepted(), result.jobId() |
True when the server accepted a background job (HTTP 202), and its id |
result.attempts(), result.idempotencyKey() |
How many tries it took and the key used |
Send files and download results
A file parameter accepts a path string, or new H2IFile(bytes, "a.png") for bytes you already have. A path is read from disk only for fields the API defines as files.
Results are files with a signed address. Download them with the client; the API key is not sent with the download, and only addresses of your Davix H2I host are accepted.
h2i.downloadTo((String) result.files().get(0).get("url"), java.nio.file.Path.of("photo-800.jpg")); // saves the file
byte[] data = h2i.download(address); // or returns the bytes
Options
Client options (on the builder)
| Option | What it does |
|---|---|
baseUrl(url) |
Another Davix H2I address. It must be https (http is allowed only for localhost) |
timeout(Duration) |
Time for one attempt, 60 seconds by default |
maxRetries(n), maxRetryDelay(Duration) |
2 retries and a longest wait of 30 seconds by default |
autoIdempotencyKey(boolean) |
On by default, see Retries |
validate(boolean) |
Check parameters before sending, off by default |
bearerAuth() |
Send the key as a Bearer token instead of X-Api-Key |
Options of one call
Options of one call are a CallOptions after the parameters: idempotencyKey, header, timeout, maxRetries, noRetry, validate and strict.
h2i.images().resize(Params.of("images", "photo.jpg", "width", 800),
new CallOptions().idempotencyKey("order-42").timeout(java.time.Duration.ofMinutes(2)));
Retries and idempotency
The client retries only what is safe to repeat: a rate limit answer (429, it waits as long as the server says), and 503 or 504 answers that are marked retryable, for calls that cannot do the work twice. By default it makes one idempotency key for each call and reuses it on every retry, so a retry never charges you twice. You can give your own key, which must be 8 to 128 characters from letters, digits and . _ : -, and you can turn retries or the automatic key off. See Error codes and rate limits.
Errors
try {
Response result = h2i.images().resize(Params.of("images", "photo.jpg", "width", 800));
} catch (H2IException error) {
if (ErrorCodes.MONTHLY_QUOTA_EXCEEDED.equals(error.getCode())) {
System.out.println("Allowance used up: " + error.getHint());
} else {
System.out.println(error.getStatus() + " " + error.getCode() + " " + error.getHint() + " " + error.getRequestId());
}
}
Every failure is an H2IException (unchecked). getKind() says what went wrong: VALIDATION, UPLOAD, AUTH, PLAN, QUOTA, RATE_LIMIT, PROCESSING, SERVER, NOT_FOUND, CONFLICT, AI, KEY, JOB, SIGNED_URL, API, NETWORK, TIMEOUT or CONFIG (the SDK was used wrongly). It also has getCode(), getStatus(), getHint(), getDetails(), getRequestId(), getRenewalUrl(), isRetryable(), getRetryAfter() and getBody(). ErrorCodes holds every code as a constant.
Quote the request id when you contact support. The meaning of each code is in Error codes and rate limits.
Look up fields, limits and your plan
h2i.schema("images.resize"); // type, limits, default and allowed values of each field
h2i.capabilities().runtime(); // what the server offers
h2i.capabilities().account(); // your plan, limits and usage
h2i.contract().refresh(); // use the live contract instead of the bundled one
The schema of a tool gives each field’s type, limits, default and allowed values. The account information shows what your plan allows and what you have used.
Reach a new endpoint
If the API gets an endpoint this version does not know yet, use the generic request method. It only reaches the public /v1/ API.
H2IClient.RequestInput input = new H2IClient.RequestInput();
input.query = java.util.Map.of("limit", 10);
h2i.request("GET", "/v1/jobs", input, null);
Name your app in the History
Every processing call accepts an optional app parameter. Its text is shown under each result in your History, for example My shop importer.
Safe by design
- The API key is sent only in a header. It is never put in an address or a body, never printed (showing the client does not reveal it), and never sent to a redirect or to a download address.
- The client refuses plain http for any host except your own computer, and refuses an address with a password in it.
- Redirects are not followed.
Other clients: Node SDK, Python SDK, PHP SDK, Go SDK, .NET SDK, Ruby SDK.
Checked against the Java SDK source code and API contract 3.0.0.
