Available now. The Python SDK is published on PyPI and works with the install command below. See the package page on PyPI. If you prefer not to use it, you can also call the REST API from Python with any HTTP client (Call a tool over REST).
The Python SDK is the official Python client. Its methods, field names, allowed values and error codes are generated from the same API contract as the server, so they match the API.
Requirements and install
Python 3.8 or newer. It needs no other packages.
pip install davixlabs-h2i
Create a client
Create an API key first (Create and rotate API keys) and keep it in an environment variable or a secrets manager, never in the code.
import os
from davix_h2i import H2I, DavixH2IError, ErrorCodes, enums
h2i = H2I(os.environ["DAVIX_H2I_API_KEY"])
The address defaults to https://app.h2i.davixlabs.io.
Run a tool
result = h2i.images.resize({
"images": "photo.jpg",
"width": 800,
"resizeMode": enums.ResizeFit.COVER,
})
print(result.status, result.request_id)
Tools are grouped like the API: h2i.images.resize(...), h2i.pdfs.merge(...), h2i.ai.documents.summarize(...). You pass the parameters as a dict, using the names of the API reference.
Find every tool and its fields in the tool list; each tool page has an API reference.
The result
| What | Meaning |
|---|---|
result.data |
The answer: parsed JSON, text or bytes |
result.status, result.request_id |
HTTP status and the id to quote to support |
result.files() |
Every result file in the answer, each with a signed url |
result.accepted, result.job_id |
True when the server accepted a background job (HTTP 202), and its id |
result.attempts, result.idempotency_key |
How many tries it took and the key used |
Send files and download results
A file parameter accepts a path string, bytes, an open file, FileInput(data=..., filename="a.png"), or a dict such as {"path": "a.png"} or {"data": b"...", "filename": "a.png"}. A path is read from disk only for fields the API defines as files.
Results are files with a signed address. Download them with the client; the API key is not sent with the download, and only addresses of your Davix H2I host are accepted.
h2i.download(result.files()[0], dest="photo-800.jpg") # saves the file
data = h2i.download(result.files()[0]) # or returns the bytes
Options
Client options
| Option | What it does |
|---|---|
base_url |
Another Davix H2I address. It must be https (http is allowed only for localhost) |
timeout |
Seconds for one attempt, 60 by default |
max_retries, max_retry_delay |
2 retries and a longest wait of 30 seconds by default |
auto_idempotency_key |
On by default, see Retries |
validate |
Check parameters before sending, off by default |
auth_scheme |
"x-api-key" (default) or "bearer" |
user_agent_suffix |
Text added to the User-Agent, to name your program |
Options of one call
Options of one call are keyword arguments after the parameters: idempotency_key, headers, timeout, max_retries, retry (False turns retries off), validate and strict.
h2i.images.resize({"images": "photo.jpg", "width": 800}, idempotency_key="order-42", timeout=120)
Retries and idempotency
The client retries only what is safe to repeat: a rate limit answer (429, it waits as long as the server says), and 503 or 504 answers that are marked retryable, for calls that cannot do the work twice. By default it makes one idempotency key for each call and reuses it on every retry, so a retry never charges you twice. You can give your own key, which must be 8 to 128 characters from letters, digits and . _ : -, and you can turn retries or the automatic key off. See Error codes and rate limits.
Errors
try:
result = h2i.images.resize({"images": "photo.jpg", "width": 800})
except DavixH2IError as error:
if error.code == ErrorCodes.MONTHLY_QUOTA_EXCEEDED:
print("Allowance used up:", error.hint)
else:
print(error.status, error.code, error.hint, error.request_id)
Every failure is a DavixH2IError. The subclasses are ValidationError, UploadError, AuthenticationError, PlanError, QuotaError, RateLimitError, ProcessingError, ServerError, NotFoundError, ConflictError, AiError, KeyManagementError, JobError and SignedUrlError for answers of the API, NetworkError and TimeoutError for connection problems, and ConfigError when the SDK is used wrongly. An error has code, status, hint, details, request_id, renewal_url, retryable, retry_after and body. ErrorCodes holds every code as a constant.
Quote the request id when you contact support. The meaning of each code is in Error codes and rate limits.
Look up fields, limits and your plan
h2i.schema("images.resize").fields # type, limits, default and allowed values of each field
h2i.capabilities() # what the server offers
h2i.capabilities.account() # your plan, limits and usage
h2i.contract.refresh() # use the live contract instead of the bundled one
The schema of a tool gives each field’s type, limits, default and allowed values. The account information shows what your plan allows and what you have used.
Reach a new endpoint
If the API gets an endpoint this version does not know yet, use the generic request method. It only reaches the public /v1/ API.
h2i.request("GET", "/v1/usage")
h2i.request("GET", "/v1/jobs", query={"limit": 10})
Name your app in the History
Every processing call accepts an optional app parameter. Its text is shown under each result in your History, for example My shop importer.
Safe by design
- The API key is sent only in a header. It is never put in an address or a body, never printed (showing the client does not reveal it), and never sent to a redirect or to a download address.
- The client refuses plain http for any host except your own computer, and refuses an address with a password in it.
- Redirects are not followed.
Other clients: Node SDK, PHP SDK, Go SDK, Java SDK, .NET SDK, Ruby SDK.
Checked against the Python SDK source code and API contract 3.0.0.
