Library for interacting with Clerk
Project description
Clerk Python SDK
A production-ready Python client for the Clerk API. The SDK wraps Clerk's REST endpoints, rich document models, automation helpers, and structured task decorators so that your applications can create, update, and process Clerk documents with minimal boilerplate.
Table of Contents
- Overview
- Key Features
- Requirements
- Installation
- Configuration
- Quick Start
- Automation Utilities
- Error Handling
- Development Workflow
- Contributing
- License
Overview
The Clerk SDK centers around the Clerk client (clerk.client.Clerk), which extends a resilient BaseClerk transport layer with automatic retries and typed responses. Models under clerk.models provide Pydantic-powered validation for documents, files, and API payloads, ensuring type safety across network boundaries. Additional modules cover automated task execution via the clerk.decorator package and UI workflows under clerk.gui_automation.
Key Features
- Document lifecycle management – Create, fetch, list, and update Clerk documents with first-class models.
- File handling – Upload binary files or parsed base64 payloads and attach them to documents.
- Robust networking – Automatic retries for transient HTTP issues, configurable base URLs, and bearer authentication out of the box.
- Structured task execution – Decorators for running Clerk tasks locally or inside worker environments with consistent pickle-based I/O.
- GUI automations – Utilities for orchestrating low-level UI actions, state machines, and operator interactions when human-in-the-loop steps are required.
Requirements
- Python 3.10+
- Dependencies listed in
requirements.txt, includingpydanticandbackoff.
Installation
Install the SDK from PyPI:
pip install clerk-sdk
For local development inside this repository, install the dependencies in editable mode:
pip install -e .[dev]
Configuration
The client reads configuration from keyword arguments or environment variables.
| Setting | Environment Variable | Description |
|---|---|---|
| API key | CLERK_API_KEY |
Required secret used for bearer authentication. |
| Base URL | CLERK_BASE_URL |
Optional override of the default API host (https://api.clerk-app.com). |
export CLERK_API_KEY="sk_live_123"
export CLERK_BASE_URL="https://staging.clerk-app.com" # optional
You can also pass the API key directly when instantiating Clerk:
from clerk import Clerk
client = Clerk(api_key="sk_live_123")
Quick Start
The following snippets demonstrate the core document operations supported by the SDK.
Instantiate a Client
from clerk import Clerk
client = Clerk(api_key="sk_live_123")
Fetch Documents
Retrieve a single document by its identifier or list documents with query filters.
from clerk.models.document import GetDocumentsRequest
# Single document
invoice = client.get_document(document_id="doc_123")
print(invoice.title, invoice.status)
# Query multiple documents
request = GetDocumentsRequest(project_id="proj_456", limit=25)
documents = client.get_documents(request)
for doc in documents:
print(doc.id, doc.status)
Upload a Document
Use UploadDocumentRequest to send metadata and file attachments. Files can be supplied as paths or ParsedFile instances.
from clerk.models.document import UploadDocumentRequest
upload_request = UploadDocumentRequest(
workflow_id="proj_456",
message_subject="Invoice 2024-01",
files=["/path/to/invoice.pdf"],
input_structured_data={"customer_id": "cust_789"},
)
created = client.upload_document(upload_request)
print(f"Created document: {created.id}")
Update Structured Data
Patch a document's structured payload without re-uploading files.
updated = client.update_document_structured_data(
document_id="doc_123",
updated_structured_data={"status": "processed", "processed_by": "automation"},
)
print(updated.structured_data)
Work with Files
Retrieve parsed file metadata or attach additional files to existing documents.
from clerk.models.file import UploadFile
# List associated files
files = client.get_files_document(document_id="doc_123")
for file in files:
print(file.name, file.mimetype)
# Append output files
client.add_files_to_document(
document_id="doc_123",
type="output",
files=[
UploadFile(name="summary.txt", mimetype="text/plain", content=b"Processed")
],
)
Custom Code Utilities
Task Decorator
The @clerk_code decorator standardizes how Clerk tasks load inputs and persist outputs when executed by the Clerk workflow. It automatically reads a pickled ClerkCodePayload from /app/data/input/input.pkl, executes your function, and writes the result (or an ApplicationException) to /app/data/output/output.pkl.
from clerk.decorator import clerk_code
from clerk.decorator.models import ClerkCodePayload, Document
@clerk_code()
def handle_document(payload: ClerkCodePayload) -> ClerkCodePayload:
document: Document = payload.document
payload.structured_data = payload.structured_data or {}
payload.structured_data["status"] = f"Processed {document.id}"
return payload
if __name__ == "__main__":
handle_document() # Auto-loads from pickle files when payload is omitted
For unit testing, you can bypass the pickle integration by passing an explicit payload instance:
from datetime import datetime
from clerk.decorator.models import ClerkCodePayload, Document
from clerk.models.document_statuses import DocumentStatuses
sample_payload = ClerkCodePayload(
document=Document(
id="doc_123",
project_id="proj_456",
title="Sample",
upload_date=datetime.utcnow(),
status=DocumentStatuses.draft,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
),
structured_data={},
)
result = handle_document(sample_payload)
assert "Processed" in result.structured_data["status"]
Note: Refer to
tests/test_task_decorator.pyfor additional usage examples covering error propagation and pickle round-trips.
GUI Automation Toolkit
The clerk.gui_automation package contains models, actions, and state machines for orchestrating UI interactions. Highlights include:
BaseActionand concrete actions for cursor movement, clicks, and keyboard input.ActionModelbuilders that translate payloads into executable UI sequences.- State machine primitives (
ui_state_machine) to coordinate multi-step automations. - Helpers for safely reading files, validating anchors, and converting payload flags.
These utilities are designed to be composed with your own automation runners or integrated into Clerk tasks. Review the tests in tests/test_gui_automation.py for patterns on stubbing operator clients and verifying payload transformations.
Error Handling
All network helpers raise requests exceptions for HTTP errors. When using the task decorator, runtime failures are wrapped in ApplicationException objects that capture the exception type, message, and traceback for easier debugging. Deserialize the returned payload or inspect the pickle output to handle errors gracefully.
Development Workflow
- Clone the repository and install dependencies with
pip install -e .[dev]. - Run the test suite using
pytest. The CI workflow executes these tests before packaging releases. - Add type-safe models or extend the client in
clerk/client.pyandclerk/modelsas needed. - Contribute automations under
clerk/gui_automationby following the established action/state machine patterns. - Commit and open a pull request once tests pass and documentation is updated.
Contributing
Contributions are welcome! Please open an issue to discuss substantial changes, follow the existing code style (Pydantic models, typed functions, and pytest fixtures), and ensure the test suite passes before submitting a pull request.
License
This project is licensed under the MIT License. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file clerk_sdk-0.5.3.tar.gz.
File metadata
- Download URL: clerk_sdk-0.5.3.tar.gz
- Upload date:
- Size: 87.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a0099ea17529214d8918b526468e365fa6b80d45080a7419ab9e8bea0515c2b
|
|
| MD5 |
db8862b1df2cea0fdc2155a224b869d2
|
|
| BLAKE2b-256 |
710da6f4f722fba523fd60ed28134cd80dd18f623a03ef9714bafc7c74e998df
|
File details
Details for the file clerk_sdk-0.5.3-py3-none-any.whl.
File metadata
- Download URL: clerk_sdk-0.5.3-py3-none-any.whl
- Upload date:
- Size: 78.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6cab4805fac21da461868f28cc4d97bf0cc890a933b92d7144dfd2f4e34b9c5b
|
|
| MD5 |
2dbb59b2ed27ce3439ef17278dd7ef1b
|
|
| BLAKE2b-256 |
3091205f3b6415ca1d017be812747d123b5c163bae2596a902c5c805456a94e5
|