Piloterr

Java

Install the Piloterr Java SDK and make your first API call.

The official Java SDK for Piloterr, with one typed method per endpoint and a generic request() for anything else.

Requirements

  • Java 17+

Install

The Java SDK is not on Maven Central. Build it from source to install it in your local Maven repository, then add it to your project.

git clone https://github.com/piloterr/piloterr-java.git
cd piloterr-java
mvn -q install -DskipTests
Maven
<dependency>
  <groupId>com.piloterr</groupId>
  <artifactId>piloterr</artifactId>
  <version>0.1.0</version>
</dependency>
Gradle
implementation("com.piloterr:piloterr:0.1.0")

Get your API key

Copy your API key from the Piloterr dashboard and set it in the PILOTERR_API_KEY environment variable, which the SDK reads by default. Keep it out of source control.

Quick start

import com.piloterr.ApiResponse;
import com.piloterr.PiloterrClient;
import com.piloterr.params.AmazonProductParams;

PiloterrClient client = PiloterrClient.fromEnv();
ApiResponse response = client.amazonProduct(AmazonProductParams.builder().query("B09DFCB66S").build());
System.out.println(response.json().toPrettyString());

Async jobs

Any call accepts is_async, which is useful past the 60-second server limit. The response is then a job_id, and the SDK can wait for the job to finish.

ApiResponse submitted = client.amazonProduct(AmazonProductParams.builder().query("B09DFCB66S").isAsync(true).build());
ApiResponse job = client.waitForJob(
    submitted.json().get("job_id").asText(),
    Duration.ofSeconds(3),
    Duration.ofHours(12),
    progress -> System.out.println(progress.path("status").asText() + " " + progress.path("progress").asInt()));

Errors

A non-2xx response raises an API error with its status and message. See Status codes for the full list.

try {
  client.amazonProduct(AmazonProductParams.builder().query("B09DFCB66S").build());
} catch (PiloterrApiException e) {
  System.err.println(e.status() + ": " + e.getMessage());
}

Retries

Requests are retried up to 3 times after a network error, a 408, 500, 502, 503 or 504, or a plan rate limit. Per-key quota errors and client-side timeouts are not retried. The delay starts at 1 second and grows 1.5× per retry, and a Retry-After header wins.

PiloterrClient client = PiloterrClient.builder().maxRetries(5).retryDelay(Duration.ofMillis(500)).build();
client.amazonProduct(AmazonProductParams.builder().query("B09DFCB66S").build(), RequestOptions.builder().maxRetries(0).build()); // one request without retries

Batch requests

Run many calls at once, 10 at a time by default. One failure does not stop the others.

List<String> inputs = List.of("B09DFCB66S", "B0BSHF7WHW", "B08N5WRWNW");
List<BatchResult<ApiResponse>> results = client.batch(inputs, 5, value ->
    client.amazonProduct(AmazonProductParams.builder().query(value).build()));
for (BatchResult<ApiResponse> result : results) {
  if (result.ok()) {
    System.out.println(result.value().json());
  } else {
    System.err.println(inputs.get(result.index()) + ": " + result.error().getMessage());
  }
}

Any endpoint

request() reaches any path, including endpoints released after your SDK version.

ApiResponse response = client.request("GET", "/v1/usage", Map.of());

Common mistakes

  • Putting the API key in front-end code. Anyone could read it. Call the API from your server.
  • Treating every 401 as a bad key. A 401 can also be a rate limit: read the message. The SDK retries plan rate limits for you.
  • Retrying a 402. It means there are no credits or quota left, so a retry cannot succeed. See Credits.
  • Waiting on long requests. Past 60 seconds, use async mode as shown above rather than raising your timeout. See Timeout.

On this page