Java AI detector quickstart
Send at least 150 characters for a more reliable signal. The response score ranges from 0 (more likely human-written) to 1 (more likely AI-generated); use it as evidence, not proof.
This example uses the HTTP client included with Java 11 and newer.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String apiKey = System.getenv("SAPLING_API_KEY");
if (apiKey == null || apiKey.isBlank()) {
throw new IllegalStateException("Set SAPLING_API_KEY first.");
}
String payload = "{"
+ "\"text\":\"I am an artificial intelligence system designed to help people solve complex problems. "
+ "My capabilities include natural language processing, machine learning, and predictive analytics.\","
+ "\"sent_scores\":true"
+ "}";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.sapling.ai/api/v1/aidetect"))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(
request,
HttpResponse.BodyHandlers.ofString());
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new IllegalStateException(
"Sapling API error " + response.statusCode() + ": " + response.body());
}
System.out.println(response.body());
}
}
{
"score": 0.9989,
"sentence_scores": [
{
"score": 0.9764,
"sentence": "I am an artificial intelligence system designed to help people solve complex problems."
},
{
"score": 0.9812,
"sentence": "My capabilities include natural language processing, machine learning, and predictive analytics."
}
],
"text": "I am an artificial intelligence system designed to help people solve complex problems. My capabilities include natural language processing, machine learning, and predictive analytics."
}
The overall score and sentence scores use different methods and may not match exactly. Choose thresholds based on the false-positive and false-negative costs of your use case.
About Java
Java is a popular object-oriented programming language that can run across many different platforms through the Java Virtual Machine (JVM). Java has C/C++ style syntax but comes with automatic memory management and less low-level primitives.