We currently don’t have an official SDK for Java, but you can use the OpenAPI Generator to generate a Java client or simply call the API directly using Java’s built-in HttpClient.
Grab the API key from your project and begin submitting text, images, or other media to your project for moderation.
import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import org.json.JSONObject;public class TextModeration { public static void main(String[] args) { String API_KEY = "your-api-key"; // Replace with your API key String BASE_URL = "https://api.moderationapi.com/v1"; try { HttpClient client = HttpClient.newHttpClient(); // Create request body JSONObject content = new JSONObject() .put("type", "text") .put("text", "Hello world!"); JSONObject requestBody = new JSONObject() .put("content", content) .put("authorId", "123") .put("conversationId", "456") .put("metadata", new JSONObject() .put("customField", "value") ); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(BASE_URL + "/moderate")) .header("Authorization", "Bearer " + API_KEY) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(requestBody.toString())) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); JSONObject textAnalysis = new JSONObject(response.body()); if (textAnalysis.getBoolean("flagged")) { System.out.println("Text content flagged"); // Block the content, show an error, etc... } else { System.out.println("Text content is safe."); // Save to database or proceed... } } catch (Exception e) { e.printStackTrace(); } }}
Dry-run mode: If you want to analyze production data but don't want to block content, enable "dry-run" in your project settings.
With dry-run enabled, the API still analyzes content but it always returns flagged: false - yet content still shows in the review queue. This way you can implement your moderation workflows and start testing your project configuration without actually blocking content.