Python
quantumbpm-sdk - async-first, built on asyncio. Requires Python 3.10+.
Installation
pip install quantumbpm-sdk
Quick start
import asyncio
import os
from quantumbpm import ClientCredentialsTokenProvider, QuantumBPM, Vars
async def main():
provider = ClientCredentialsTokenProvider(
token_url="https://idp.example.com/realms/acme/protocol/openid-connect/token",
client_id="billing-svc",
client_secret=os.environ["QUANTUMBPM_CLIENT_SECRET"],
)
async with QuantumBPM(
base_url="https://quantumbpm.example.com",
project_id="YOUR_PROJECT_ID",
token_provider=provider,
) as client:
result = await client.dmn.evaluate(
"loan-eligibility",
Vars().set("requestedAmt", 1000).set("creditScore", 720),
)
print(result)
asyncio.run(main())
The SDK asks the token provider for a token before every API call, inside or outside async with. ClientCredentialsTokenProvider caches its token until less than 60 seconds of its lifetime remain, so a custom provider should cache too.
Authentication
ClientCredentialsTokenProvider fetches tokens from your OIDC identity provider with the client credentials grant. token_url, client_id and client_secret are required. scopes and audience are optional:
provider = ClientCredentialsTokenProvider(
token_url="https://idp.example.com/realms/acme/protocol/openid-connect/token",
client_id="billing-svc",
client_secret=os.environ["QUANTUMBPM_CLIENT_SECRET"],
scopes=["openid"],
audience="quantumbpm",
)
The exact token endpoint depends on the identity provider, see Authentication. For an identity provider behind a private CA, pass ssl_ca_cert="/path/to/ca.crt". A failed token request raises quantumbpm.exceptions.ApiException with the HTTP status and response body. For a token you already hold, use StaticTokenProvider(token).
DMN
draft = await client.dmn.create_definition("loan-eligibility", dmn_xml)
deployed = await client.dmn.deploy_definition(draft.id)
result = await client.dmn.evaluate(
deployed.definitions_id,
Vars().set("requestedAmt", 5000),
)
A draft is not evaluable until it is deployed, and the deploy gives it the next version of its <definitions id>. Saving XML identical to the latest deployed version raises quantumbpm.exceptions.ApiException with status 409, and nothing is saved.
BPMN
draft = await client.bpmn.create_resource("loan-process", bpmn_xml)
await client.bpmn.deploy_resource(draft.id)
deployed = await client.bpmn.get_resource(draft.id)
process_def = deployed.processes[0]
# The latest deployed version, or version=n for one version.
instance_id = await client.bpmn.start_instance(
process_def.process_id,
Vars().set("applicantID", "u-123"),
)
Correlation keys (businessId)
Stamp a caller-supplied correlation key (order number, ticket ID, etc.) on a new instance by passing business_id=. The key is inherited by every child instance, external job, user task, and DMN execution emitted by the process - workers read it as job.business_id.
await client.bpmn.start_instance(process_def.process_id, vars, business_id="ORDER-42")
# Filter listings by the same key.
page = await client.bpmn.list_instances(business_id="ORDER-42")
tasks = await client.bpmn.list_user_tasks(business_id="ORDER-42")
# DMN evaluations stamp it on the resulting execution row.
result = await client.dmn.evaluate("loan-eligibility", vars, business_id="ORDER-42")
External job workers
Decorator-based registration. Each task type runs its own poll loop, and its handlers run as asyncio tasks, at most max_jobs at a time.
from quantumbpm import Vars, BpmnError
from quantumbpm.workers import Job
worker = client.new_worker(client_id="billing-svc")
stop = asyncio.Event()
@worker.handler("send-email", max_jobs=10, lock_duration="1m")
async def handle(job: Job) -> Vars:
recipient = job.vars.lookup("recipient")
await emailer.send(recipient)
return Vars().set("messageID", "msg-123") # → Complete
asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, stop.set)
await worker.run(stop) # resolves once running handlers have finished
Heartbeats keep each job locked to the worker while its handler runs. The server clamps lock_duration to 10s..5m by default, and the worker heartbeats at half of the lock it was granted. Durations are Go duration strings such as 500ms, 30s, 12.5s or 1m30s, and registering a handler with an invalid one raises ValueError. When a heartbeat finds the lock gone, the handler task is cancelled and nothing is reported for that job. Each poll waits at most 5 seconds for a job. Once stop is set, the worker starts no new poll, an in-flight poll returns within those 5 seconds, and running handlers keep heartbeating and report their results for up to shutdown_timeout (a new_worker argument in seconds, 20 by default). Handlers still running then are cancelled and not reported, so let CancelledError propagate. A job abandoned that way costs a retry, see When a lock runs out. On Kubernetes, keep terminationGracePeriodSeconds above the shutdown timeout.
Raise a BpmnError to fail with a code boundary error events can catch:
raise BpmnError("INSUFFICIENT_FUNDS", Vars().set("balance", 12.0))
Any other raised exception is reported with the code WORKER_ERROR and the message attached as the error variable (truncated to 2048 bytes by default, configurable via the max_error_message_bytes worker option). A boundary error event whose error definition has errorCode="WORKER_ERROR" catches it once the retry budget is exhausted - see Failure handling.
Type-annotate Job[T] with a Pydantic model for opt-in typed dispatch - the runtime decodes job variables into T before invoking the handler:
from pydantic import BaseModel
class EmailJob(BaseModel):
recipient: str
subject: str
@worker.handler("send-email")
async def handle(job: Job[EmailJob]) -> Vars:
await emailer.send(job.typed.recipient, job.typed.subject)
return Vars()
Full reference
See the SDK README for the complete API surface and the Vars typed-access helpers.