Skip to content

Quickstart

You need an API key with the documents:write and documents:read scopes. There is no public signup yet: first keys are provisioned by an operator. Set the key in your environment:

Terminal window
export SONIC_API_KEY="your-api-key"

The examples default to https://api.sonicdocs.ai/v1, the intended production base URL. Set SONIC_API_BASE to point them somewhere else. The curl example needs curl 7.76 or later, jq and openssl. The JavaScript example needs Node.js 22 or later. The Python example needs Python 3.10 or later and requests.

  1. Upload the file. Send the raw bytes to POST /documents with the file’s media type and a new Idempotency-Key. Add export=txt to ask for plain text, which is also the default. The service answers 202 Accepted with a receipt only after the file is stored durably.

  2. Wait for a terminal state. Poll the receipt’s links.status URL until state is succeeded or failed. For live progress without polling, see live events and webhooks.

  3. Download the outputs. Read the result manifest at links.result and download each listed file from its url.

docs/public/examples/curl/quickstart.sh
#!/usr/bin/env bash
# Sonic Docs contract example: upload a document, wait for it, download the result.
# Written against the planned v1 contract (api/specs/openapi.yaml). It has not yet
# been run against a live Sonic Docs API. Requires bash, curl 7.76+, jq and openssl.
set -euo pipefail
API_BASE="${SONIC_API_BASE:-https://api.sonicdocs.ai/v1}"
: "${SONIC_API_KEY:?Set SONIC_API_KEY to a key with documents:read and documents:write}"
FILE="${1:?Usage: quickstart.sh path/to/document.pdf}"
OUT="${SONIC_OUTPUT_DIR:-sonic-output}"
AUTH="Authorization: Bearer $SONIC_API_KEY"
case "$(tr "[:upper:]" "[:lower:]" <<<"$FILE")" in
*.pdf) TYPE=application/pdf ;;
*.png) TYPE=image/png ;;
*.jpg | *.jpeg) TYPE=image/jpeg ;;
*) echo "Only PDF, PNG and JPEG files are accepted" >&2; exit 2 ;;
esac
# 1. Upload. Reuse the same key if you retry, so the upload is admitted only once.
KEY="upload-$(openssl rand -hex 16)"
RECEIPT=$(curl -sS --fail-with-body -X POST "$API_BASE/documents?export=txt" \
-H "$AUTH" -H "Idempotency-Key: $KEY" -H "Content-Type: $TYPE" \
--data-binary "@$FILE")
STATUS_URL=$(jq -r .links.status <<<"$RECEIPT")
RESULT_URL=$(jq -r .links.result <<<"$RECEIPT")
echo "Accepted $(jq -r .document_id <<<"$RECEIPT")"
# 2. Poll the status URL until the document succeeds or fails.
while :; do
SNAPSHOT=$(curl -sS --fail-with-body -H "$AUTH" "$STATUS_URL")
case "$(jq -r .state <<<"$SNAPSHOT")" in
succeeded) break ;;
failed) jq .failure <<<"$SNAPSHOT" >&2; exit 1 ;;
*) sleep 2 ;;
esac
done
# 3. Read the result manifest and download every published file.
mkdir -p "$OUT"
curl -sS --fail-with-body -H "$AUTH" "$RESULT_URL" |
jq -r '.files | to_entries[] | "\(.key) \(.value.url)"' |
while read -r NAME URL; do
curl -sS --fail-with-body -H "$AUTH" -o "$OUT/$NAME" "$URL"
echo "Saved $OUT/$NAME"
done

The receipt identifies the document and gives you the URLs to follow. Treat the document ID as opaque and use the links rather than building URLs yourself.

api/specs/examples/receipt.json (contract fixture)
{
"document_id": "a~d17",
"trace_id": "86bc31a843374ae6b6a3def26d08a011",
"state": "accepted",
"chunk_pages": 400,
"output_plan": {
"formats": [
"txt"
],
"transformations": []
},
"links": {
"status": "https://docs.example/documents/a~d17",
"history": "https://docs.example/documents/a~d17/events",
"result": "https://docs.example/documents/a~d17/result",
"event_tickets": "https://docs.example/documents/a~d17/event-tickets",
"events": "wss://docs.example/documents/a~d17/events"
},
"expires_at": "2026-12-07T20:00:00Z"
}

When the document has succeeded, the result manifest lists every published file:

api/specs/examples/result.json (contract fixture)
{
"document_id": "a~d17",
"page_count": 2,
"transformations": [],
"files": {
"document.txt": {
"media_type": "text/plain",
"url": "https://docs.example/documents/a~d17/artifacts/document.txt"
}
}
}

document.txt is UTF-8 text in reading order, with a form feed (U+000C) between pages.