Skip to content

Hello world: Java

Preview — installs from source. Not yet tested end to end.

Runs today on Linux x86-64. On macOS, Windows and ARM the on-device engine is not available yet; the SDK uses basic detection and shows a warning.

1. Get an API key

Create a key at app.cloakapi.io/api-keys. It starts with cloak_. Verify your email to unlock your trial balance.

2. Install

Set up the on-device engine and pin: Engine setup.

Terminal window
cd repo/sdk/java
mvn -q package -DskipTests
mvn -q dependency:build-classpath -Dmdep.outputFile=cp.txt

3. Set your key

Terminal window
export CLOAKAPI_API_KEY="cloak_your_key_here"

4. Run hello world

HelloCloak.java
import io.cloakapi.CloakAPIClient;
import io.cloakapi.CloakAPIClient.PreparedRequest;
import io.cloakapi.errors.ApiException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public final class HelloCloak {
private static final String PROMPT = "Say hello to jane.doe@example.com in one short sentence.";
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String apiKey = System.getenv("CLOAKAPI_API_KEY");
if (apiKey == null || apiKey.isBlank()) {
System.err.println("CLOAKAPI_API_KEY is not set. Create a key at https://app.cloakapi.io/api-keys");
System.exit(1);
}
try {
CloakAPIClient client = new CloakAPIClient(apiKey);
Map<String, Object> body = Map.of(
"model", "claude-sonnet-4-6",
"max_tokens", 100,
"messages", List.of(Map.of("role", "user", "content", PROMPT)));
PreparedRequest review = client.prepareReview("/api/v1/messages", body);
System.out.println("This exact request will leave your machine:\n\n" + review.preview());
System.out.print("\nSend it? [y/N] ");
if (!new Scanner(System.in).nextLine().trim().equalsIgnoreCase("y")) {
System.out.println("Not sent. You declined the request.");
return;
}
Map<String, Object> resp = client.messages().create(review.approve());
List<Map<String, Object>> content = (List<Map<String, Object>>) resp.get("content");
System.out.println("\nReply (restored on your device):");
System.out.println(content.get(0).get("text"));
} catch (ApiException | RuntimeException e) {
System.err.println("Request failed: " + e.getMessage());
System.exit(1);
}
}
}
Terminal window
cd repo/sdk/java
java -cp "target/cloakapi-sdk-1.2.0.jar:$(cat cp.txt)" examples/hello/HelloCloak.java

5. What you should see

You’ll see the exact request before it is sent. Your email address is replaced with a token like <<CLOAK:CONTACT_EM:3f9a1c>>. Type y. The reply comes back with the real address restored on your device. The reply text varies.

If something goes wrong

See the Java SDK README for common messages and fixes.

What this does and does not do
  • Detection is best-effort. Emails, phone numbers, cards and similar are matched by rules; names use an on-device list and an optional on-device model, and may be missed.
  • You see the exact request before it is sent.
  • CloakAPI does not refuse a request because of what it contains. Errors come from your key, balance, usage limits, request format or size, or the model provider.
  • The gateway does not run detection on your content. It checks the request proof, then relays it to the model provider.

Last updated: