1. Create a key
In the dashboard, open Developer API → API keys and create a test key with the documents.write, documents.process and documents.read scopes. The full key is shown once: copy it into your server's secret store as WELLFRIEND_API_KEY.
- Grant only the scopes your service needs.
- Keep the key out of client-side code, logs, screenshots and support tickets.
2. Upload and process
Send the file bytes to POST /api/v2/documents and keep the returned version ID. Then call POST /api/v2/document-operations with that versionId and a toolId. Reuse an idempotency key only to retry the same request.
#!/usr/bin/env bash
set -euo pipefail
: "${WELLFRIEND_API_KEY:?Set WELLFRIEND_API_KEY to a wf_v2_test or wf_v2_live key}"
API="https://wellfriend.dev/api/v2"
AUTH="Authorization: Bearer ${WELLFRIEND_API_KEY}"
RUN_ID="$(date +%s)"
# 1. Ingest: the request body is the file itself. Keep the returned version ID.
DOCUMENT=$(curl --fail-with-body --max-time 120 -X POST "$API/documents" \
-H "$AUTH" \
-H "Content-Type: application/pdf" \
-H "X-Wellfriend-Filename: input.pdf" \
-H "Idempotency-Key: ingest-$RUN_ID" \
--data-binary @./input.pdf)
DOCUMENT_ID=$(jq -r '.document.id' <<<"$DOCUMENT")
VERSION_ID=$(jq -r '.document.currentVersion.id' <<<"$DOCUMENT")
# 2. Process that exact immutable version (HTTP 202 = accepted, not finished).
OPERATION_ID=$(curl --fail-with-body --max-time 30 -X POST "$API/document-operations" \
-H "$AUTH" \
-H "Content-Type: application/json" \
--data "{\"documentId\":\"$DOCUMENT_ID\",\"versionId\":\"$VERSION_ID\",\"toolId\":\"compress-pdf\",\"parameters\":{},\"idempotencyKey\":\"process-$RUN_ID\"}" \
| jq -r '.operation.id')
# 3. Poll until the operation reaches a final state.
DEADLINE=$(( $(date +%s) + 600 ))
while :; do
OPERATION=$(curl --fail-with-body --max-time 30 "$API/document-operations/$OPERATION_ID" -H "$AUTH")
STATE=$(jq -r '.operation.state' <<<"$OPERATION")
case "$STATE" in
queued|running|cancelling) ;;
completed) break ;;
*) jq -c '.operation | {state, errorCode, errorMessage}' <<<"$OPERATION" >&2; exit 1 ;;
esac
[ "$(date +%s)" -lt "$DEADLINE" ] || { echo "Timed out waiting for $OPERATION_ID" >&2; exit 1; }
sleep 2
done
# 4. Download the exact output version.
OUTPUT_DOCUMENT_ID=$(jq -r '.operation.outputDocumentId // .operation.documentId' <<<"$OPERATION")
OUTPUT_VERSION_ID=$(jq -r '.operation.outputVersionId' <<<"$OPERATION")
curl --fail-with-body --max-time 120 "$API/documents/$OUTPUT_DOCUMENT_ID/versions/$OUTPUT_VERSION_ID/content" \
-H "$AUTH" -o ./output.pdf3. Wait and download
- Poll GET
/api/v2/document-operations/{operationId}, waiting the Retry-After seconds between calls, or subscribe to webhooks. - When the state is completed, download the version named by outputVersionId and check the result.
4. Handle errors
- Store the document, version and operation IDs, your idempotency keys and the tool ID with your own record.
- Use the HTTP status, error.code and error.retryable to decide what to do. Show your users your own message and keep the referenceId for Support.
- Never log API keys, full request bodies, document content or download responses.
5. Move from polling to webhooks
Polling is fine for a first integration. In production, subscribe to signed webhooks so an event tells your server when work finishes.
- Stop polling once the operation is completed, failed or cancelled.
- Wait for Retry-After and add jitter to your backoff.
- Verify webhook signatures over the raw body and deduplicate by event ID.
6. Before you go live
- Process a known-good file and check the output yourself.
- Test a wrong file type, an oversized file, a missing scope, a revoked key, an exhausted balance and a repeated idempotency key.
- Use
wf_v2_testkeys in staging andwf_v2_livekeys in production; a test key cannot reach live data. - Monitor latency, failed operations, request-unit use and webhook delivery failures. The production checklist covers the rest.