Skip to content

Java

Anyrow for Java: every endpoint, typed, on the JDK's HTTP client, for Java 17 and later.

The ai.anyrow:anyrow library has a typed method, request and response for every endpoint, built on java.net.http.HttpClient and Jackson. Models are records. It needs Java 17 or later.

Install

implementation("ai.anyrow:anyrow:1.0.0")

Create a client

import ai.anyrow.AnyrowClient;import java.time.Duration;AnyrowClient anyrow = AnyrowClient.builder()    .baseUrl("https://api.anyrow.ai")    .apiKey(System.getenv("ANYROW_API_KEY"))    .timeout(Duration.ofSeconds(30))    .build();

The client sends the key as Authorization: ApiKey <key>. Create one client and reuse it.

Make a call

An operation's id is its method in camel case, through one accessor per group (extract.table.once is anyrow.extract().table().once(...)). Path parameters come first, then the body, then a parameters object named after the method, built with a builder, which holds the query parameters:

import ai.anyrow.models.RowListParams;var page = anyrow.row().list("proj_...", "tbl_...", RowListParams.builder().limit(50L).build());System.out.println(page.rows().size() + " rows, more: " + page.hasMore());

Request bodies are records named after the operation's body (CreateProjectsTableRequest for table.create).

Errors

A failed call throws an unchecked AnyrowException with the status and the error body; each status has its own subclass:

import ai.anyrow.errors.AnyrowException;import ai.anyrow.errors.ForbiddenException;try {    var table = anyrow.table().get("proj_...", "tbl_...");} catch (ForbiddenException error) {    System.out.println("The key lacks tables:read");} catch (AnyrowException error) {    System.out.println(error.status() + ": " + error.getMessage());}

Read every row

var rows = new ArrayList<Map<String, Object>>();String cursor = null;do {    var page = anyrow.row().list("proj_...", "tbl_...",        RowListParams.builder().limit(100L).cursor(cursor).build());    rows.addAll(page.rows());    cursor = page.nextCursor();} while (cursor != null);

Extract a document

var batch = anyrow.extract().table().once("proj_...", "tbl_...",    Map.of("text", Files.readString(Path.of("./invoice.txt"))));System.out.println(batch.status());

Stream an extraction

The stream methods return a stream of server-sent events:

anyrow.extract().table()    .stream("proj_...", "tbl_...", Map.of("text", "Invoice 42 from Acme"))    .forEach(event -> System.out.println(event.event() + " " + event.data()));

Verify webhooks

Deliveries follow Standard Webhooks; verify them with the standardwebhooks (opens in a new tab) Java library as the Webhooks guide describes.