Async support
If your code relies on asynchronous operations via CompletableFuture
or
Future
, you can use the ai.fal.client:fal-client-async
artifact instead,
which contains the necessary APIs for asynchronous programming.
The client for Java provides a seamless interface to interact with fal.
First, add the client as a dependency in your project:
implementation 'ai.fal.client:fal-client:0.7.1'
If your code relies on asynchronous operations via CompletableFuture
or
Future
, you can use the ai.fal.client:fal-client-async
artifact instead,
which contains the necessary APIs for asynchronous programming.
Endpoints requests are managed by a queue system. This allows fal to provide a reliable and scalable service.
The subscribe
method allows you to submit a request to the queue and wait for the result.
import ai.fal.client.*;
import ai.fal.client.queue.*;
var fal = FalClient.withEnvCredentials();
var input = Map.of(
"prompt", "a cat",
"seed", 6252023,
"image_size", "landscape_4_3",
"num_images", 4
);
var result = fal.subscribe("fal-ai/flux/dev",
SubscribeOptions.<JsonObject>builder()
.input(input)
.logs(true)
.resultType(JsonObject.class)
.onQueueUpdate(update -> {
if (update instanceof QueueStatus.InProgress) {
System.out.println(((QueueStatus.InProgress) update).getLogs());
}
})
.build()
);
You can manage the queue using the following methods:
Submit a request to the queue using the queue.submit
method.
import ai.fal.client.*;
import ai.fal.client.queue.*;
var fal = FalClient.withEnvCredentials();
var input = Map.of(
"prompt", "a cat",
"seed", 6252023,
"image_size", "landscape_4_3",
"num_images", 4
);
var job = fal.queue().submit("fal-ai/flux/dev",
SubmitOptions.<JsonObject>builder()
.input(input)
.webhookUrl("https://optional.webhook.url/for/results")
.resultType(JsonObject.class)
.build()
);
This is useful when you want to submit a request to the queue and retrieve the result later. You can save the request_id
and use it to retrieve the result later.
For long-running requests, such as training jobs, you can use webhooks to receive the result asynchronously. You can specify the webhook URL when submitting a request.
Retrieve the status of a specific request in the queue:
import ai.fal.client.*;
import ai.fal.client.queue.*;
var fal = FalClient.withEnvCredentials();
var job = fal.queue().status("fal-ai/flux/dev", QueueStatusOptions
.withRequestId("764cabcf-b745-4b3e-ae38-1200304cf45b"));
Get the result of a specific request from the queue:
import ai.fal.client.*;
import ai.fal.client.queue.*;
var fal = FalClient.withEnvCredentials();
var result = fal.queue().result("fal-ai/flux/dev", QueueResultOptions
.withRequestId("764cabcf-b745-4b3e-ae38-1200304cf45b"));
Some endpoints require files as input. However, since the endpoints run asynchronously, processed by the queue, you will need to provide URLs to the files instead of the actual file content.
Luckily, the client library provides a way to upload files to the server and get a URL to use in the request.
This functionality is not available on this client yet.
Some endpoints support streaming:
This functionality is not available on this client yet.
For the endpoints that support real-time inference via WebSockets, you can use the realtime client that abstracts the WebSocket connection, re-connection, serialization, and provides a simple interface to interact with the endpoint:
This functionality is not available on this client yet.
The endpoints can also be called directly instead of using the queue system.
We do not recommend this use most use cases as it will block the client until the response is received. Moreover, if the connection is closed before the response is received, the request will be lost.
import ai.fal.client.*;
var fal = FalClient.withEnvCredentials();
var input = Map.of(
"prompt", "a cat",
"seed", 6252023,
"image_size", "landscape_4_3",
"num_images", 4
);
var result = fal.run("fal-ai/flux/dev", RunOptions.withInput(input));
For a complete list of available methods and their parameters, please refer to Java API Reference documentation (opens in a new tab).
If you encounter any issues or have questions, please visit the GitHub repository (opens in a new tab) or join our Discord Community (opens in a new tab).