Many checks do not need their answer right away: a comment held for review, a product photo, an upload a moderator looks at later. Send them with "async": true. The API answers at once with the request's id, checks it shortly after, and tells you the result by webhook. An async check costs the price table; an answer given at once costs twice that.
Async requests also smooth out a burst: up to 50 of your checks wait their turn in the queue, and the API tells you when to send more. See How many can wait.
Send one#
Add "async": true to a normal moderation request. Text, URLs, files and base64 images work as usual (a file in a form: the field async set to true, see Files); live streams have their own sessions.
curl https://api.detectivision.ai/api/v1/moderation \
-H "Api-Key: $DETECTIVISION_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "text_moderation",
"input": "Great product, but the seller is an idiot",
"async": true,
"metadata": { "commentId": "c_981", "postId": 42 }
}'import os, requests
r = requests.post(
"https://api.detectivision.ai/api/v1/moderation",
headers={"Api-Key": os.environ["DETECTIVISION_API_KEY"]},
json={
"type": "text_moderation",
"input": "Great product, but the seller is an idiot",
"async": True,
"metadata": {"commentId": "c_981", "postId": 42},
},
timeout=30,
)
job = r.json() # {"id": 1043, "type": "text_moderation", "status": "queued", "metadata": {...}}const res = await fetch("https://api.detectivision.ai/api/v1/moderation", {
method: "POST",
headers: { "Api-Key": process.env.DETECTIVISION_API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({
type: "text_moderation",
input: "Great product, but the seller is an idiot",
async: true,
metadata: { commentId: "c_981", postId: 42 },
}),
});
const job = await res.json(); // { id: 1043, type: "text_moderation", status: "queued", metadata: {...} }The answer is 202 Accepted, with a Location header pointing at the request:
{ "id": 1043, "type": "text_moderation", "status": "queued", "metadata": { "commentId": "c_981", "postId": 42 } }Put your own ids in metadata: they come back with the result, so you know which comment it is about.
Files#
An async request can carry the image or the video itself: a form with the file and async set to true, or an image in base64.
curl https://api.detectivision.ai/api/v1/moderation \
-H "Api-Key: $DETECTIVISION_API_KEY" \
-F "type=image_moderation" \
-F "async=true" \
-F 'metadata={"photoId": "p_311"}' \
-F "file=@photo.jpg"import json, os, requests
with open("photo.jpg", "rb") as f:
r = requests.post(
"https://api.detectivision.ai/api/v1/moderation",
headers={"Api-Key": os.environ["DETECTIVISION_API_KEY"]},
data={"type": "image_moderation", "async": "true", "metadata": json.dumps({"photoId": "p_311"})},
files={"file": f},
timeout=30,
)
job = r.json() # {"id": 1044, "type": "image_moderation", "status": "queued", "metadata": {...}}import { readFile } from "node:fs/promises";
const form = new FormData();
form.append("type", "image_moderation");
form.append("async", "true");
form.append("metadata", JSON.stringify({ photoId: "p_311" }));
form.append("file", new Blob([await readFile("photo.jpg")], { type: "image/jpeg" }), "photo.jpg");
const res = await fetch("https://api.detectivision.ai/api/v1/moderation", {
method: "POST",
headers: { "Api-Key": process.env.DETECTIVISION_API_KEY },
body: form,
});
const job = await res.json(); // { id: 1044, type: "image_moderation", status: "queued", metadata: {...} }The check needs the file when it runs, so the file waits in storage until then, and only until then: it is deleted as soon as its check has run, whether the check succeeded or failed, and nothing is kept longer than 24 hours. The request keeps only the file's name (upload: photo.jpg). An immediate request stores no file at all. Files have the same limits as in an immediate request: images up to 10 MB, videos up to 20 MB.
Get the result#
By webhook (recommended). Add an endpoint under Dashboard → Webhooks and subscribe to:
moderation.completed: the check finished.datais the same answer an immediate request gets (see Responses), with yourmetadata.moderation.flagged: the check finished and raised a flag.moderation.failed: the check could not be done (see When it fails).datahas the request'sid,type, yourmetadata, and an error (error,message).
By polling. GET /api/v1/moderation/{id} answers status: "queued" while the request waits and "processing" while it is checked, then the answer. A request that failed has an error (and its message) instead of a result. Poll every few seconds at most; webhooks are better.
Price#
The price table is the price of an async check. An answer given at once (the default) costs twice the table. Credits are taken when the check runs, not when you send it: the request is refused at once only if your balance is already short of its price (a text's by its length; for a video, one unit).
When it fails#
Checked at once, before the 202 (nothing is queued):
- an invalid request or input shape (
invalid_request,invalid_input: an empty text, something that is not a URL), - flags the type does not check (
invalid_flags), invalidmetadata(invalid_metadata), - a balance short of the price (
insufficient_quota), - 50 of your checks already waiting (
429too_many_requests, see How many can wait), - a type that cannot be served right now (
503not_available_yet).
Later, the request fails (its read-back has an error) and you get moderation.failed when:
- the input cannot be used when it runs (
invalid_input: the URL no longer works, or is not an image; the file cannot be read), - the credits ran out before it ran (
insufficient_quota), - the API key was deleted or expired, the account was closed (
unauthorized), or the key can no longer write (forbidden), - a flag you asked for, or the type itself, can no longer be checked (
invalid_flags,not_available_yet), - checks stayed unavailable for about a day (
temporarily_unavailable).
When checks are briefly unavailable, an async request just waits and is tried again: you do nothing. A failed request costs nothing.
How many can wait#
The queue is shared by every account, so an account can have at most 50 async checks waiting in it at once: queued or being checked. One more is refused at once with 429. Nothing is queued and nothing is charged:
HTTP/1.1 429 Too Many Requests
Retry-After: 60
Content-Type: application/json
{
"error": "too_many_requests",
"message": "Too many checks are waiting: at most 50 per account. Send more once some have run."
}Send more once some have run. Wait the seconds in Retry-After, or until your webhooks (or GET /api/v1/moderation/{id}) show checks finishing, then send the next ones. To send a large batch, keep at most 50 waiting at a time.
Timing#
Async requests run as soon as there is capacity, usually within seconds. Immediate requests always go first, so under heavy load an async request can wait longer; one that cannot run within about a day fails with moderation.failed. Keep URLs reachable until the result arrives; a file sent with the request is kept for its check, so it needs nothing from you.