Build

Python SDK guide

Install the Python SDK, compress a PDF in three lines, then run tools and pipelines, handle errors and verify webhooks.

Install

Python package wellfriend-document-sdk, for Python 3.10 or later. Import it with from wellfriend import Wellfriend What the registry serves now: 1.0.0 on PyPI.

Install the Python SDK
pip install -U wellfriend-document-sdk

Quickstart

Create a key in the dashboard (/dashboard/developer-api/keys) and set it as WELLFRIEND_API_KEY on your server; the client reads it, and https://wellfriend.dev/api/v2 is the default address.

Python: compress a PDF to a file
from wellfriend import Wellfriend
wf = Wellfriend()  # reads WELLFRIEND_API_KEY
wf.tools.compress_pdf("input.pdf").save("output.pdf")
The same job as a complete program
# wellfriend-document-sdk: pip install -U wellfriend-document-sdk
# Your API key: set WELLFRIEND_API_KEY, or replace YOUR_API_KEY (keys: https://wellfriend.dev/dashboard/developer-api/keys).
import os

from wellfriend import Wellfriend

wf = Wellfriend(api_key=os.environ.get("WELLFRIEND_API_KEY", "YOUR_API_KEY"))
result = wf.tools.compress_pdf("input.pdf")
result.save("input-wellfriend.pdf")

Tools

Every tool is one method, wf.tools.<tool>(input, params, options): it uploads the input (a path, bytes, a stream or a stored document; a list for merge), runs the tool, waits and returns the result. The result saves its first output with save(path), or every output into a folder; it also gives the bytes, a stream, the outputs, the operation and the usage. tools.run(toolId, ...) runs any tool by id, wait: false returns the started operation to wait on later, and quote(...) prices a run before you send it.

Settings, any tool by id, and not waiting
result = wf.tools.compress_pdf("input.pdf", params={"level": "strong"})
print(result.outputs, result.usage)
wf.tools.run("merge-pdf", ["a.pdf", "b.pdf"]).save("merged.pdf")
operation = wf.tools.ocr_pdf("scan.pdf", options={"wait": False})  # resume later with operation.wait()

Pipelines

A pipeline runs up to ten tools on one file in one run; each step reads the previous step's output on the server. workflows.define(name, steps) keeps a named pipeline you run again like a tool. The SDK reuses one saved pipeline for the same steps instead of creating one per run.

Compress, then OCR
result = wf.pipeline([{"tool": "compress-pdf"}, {"tool": "ocr-pdf"}]).run("input.pdf")
result.save("output.pdf")
tidy = wf.workflows.define("tidy-scans", [{"tool": "compress-pdf"}, {"tool": "ocr-pdf"}])
tidy.run("next.pdf").save("next-out.pdf")

Errors

Every SDK raises the same errors. A message ends with the reference to quote to Support. Network errors, 408, 429 and 5xx answers are retried twice with backoff (honouring Retry-After) for reads and for writes the SDK sends with an idempotency key; set maxRetries and timeout on the client or per call.

The error types
WellfriendError
├─ APIError (status, code, message, referenceId, details)
│  ├─ BadRequestError (400) / ValidationError (422)
│  ├─ AuthenticationError (401) / PermissionDeniedError (403)
│  ├─ NotFoundError (404) / ConflictError (409)
│  ├─ RateLimitError (429, retryAfter)
│  └─ ServerError (5xx)
├─ ConnectionError
├─ TimeoutError
├─ OperationFailedError (code, message, operation)
└─ WaitTimeoutError (operation id, to resume)
Handle the common cases
from wellfriend import OperationFailedError, RateLimitError, ValidationError

try:
    wf.tools.compress_pdf("input.pdf", params={"level": "extreme"})
except ValidationError as error:
    print(error.message, error.details)
except RateLimitError as error:
    print(f"Try again in {error.retry_after} s")
except OperationFailedError as error:
    print(error.code, error.operation.id)

Webhooks

webhooks.verify(payload, headers, secret), also available on its own as verifyWebhook, checks the signature over the raw body with a five-minute tolerance and returns the typed event; a bad signature raises WebhookVerificationError. Pass a list of secrets while you rotate one. See /docs/webhooks/signatures.

Verify a delivery
# Use the raw request body, exactly as received.
event = wf.webhooks.verify(raw_body, request.headers, os.environ["WELLFRIEND_WEBHOOK_SECRET"])
print(event.type, event.data)

Every other endpoint

Every API operation is a method named after it: document_versions.download, webhooks.endpoints.create, and so on. List methods return one page and can also go through every page. The API reference (/docs/developer-api/snippets) shows each one in this language.

Documents and webhooks
wf.documents.list(limit=20)
wf.document_versions.download("<documentId>", "<versionId>").save("output.pdf")
wf.webhooks.endpoints.create(url="https://example.com/hooks", event_filters=["com.wellfriend.document.*"])