JavaScript / TypeScript
@quantumbpm/sdk - TypeScript-first, async, runs on Node.js 18+.
Installation
npm install @quantumbpm/sdk
Quick start
import { ClientCredentialsTokenProvider, QuantumBPM, Vars } from '@quantumbpm/sdk';
const provider = new ClientCredentialsTokenProvider({
tokenUrl: 'https://idp.example.com/realms/acme/protocol/openid-connect/token',
clientId: 'billing-svc',
clientSecret: process.env.QUANTUMBPM_CLIENT_SECRET!,
});
const client = new QuantumBPM({
baseUrl: 'https://quantumbpm.example.com',
projectId: 'YOUR_PROJECT_ID',
tokenProvider: provider,
});
const result = await client.dmn.evaluate(
'loan-eligibility',
new Vars().set('requestedAmt', 1000).set('creditScore', 720),
);
console.log(result);
Authentication
ClientCredentialsTokenProvider fetches tokens from your OIDC identity provider with the client credentials grant. tokenUrl, clientId and clientSecret are required. scopes and audience are optional:
const provider = new ClientCredentialsTokenProvider({
tokenUrl: 'https://idp.example.com/realms/acme/protocol/openid-connect/token',
clientId: 'billing-svc',
clientSecret: process.env.QUANTUMBPM_CLIENT_SECRET!,
scopes: ['openid'],
audience: 'quantumbpm',
});
The exact token endpoint depends on the identity provider, see Authentication. Parallel getToken() calls share one in-flight token request. A failed token request rejects with an Error that carries the HTTP status and response body. For a token you already hold, use new StaticTokenProvider(token).
DMN
const draft = await client.dmn.createDefinition('loan-eligibility', dmnXml);
const deployed = await client.dmn.deployDefinition(draft.id);
const result = await client.dmn.evaluate(
deployed.definitionsID,
new 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 rejects with an ApiError of status 409, and nothing is saved.
BPMN
const draft = await client.bpmn.createResource('loan-process', bpmnXml);
await client.bpmn.deployResource(draft.id);
const deployed = await client.bpmn.getResource(draft.id);
const processDef = deployed.processes![0];
// The latest deployed version, or { version: n } for one version.
const instanceId = await client.bpmn.startInstance(
processDef.processID,
new 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 businessId in the options object. The key is inherited by every child instance, external job, user task, and DMN execution emitted by the process - workers read it as job.businessId.
await client.bpmn.startInstance(processDef.processID, vars, { businessId: 'ORDER-42' });
// Filter listings by the same key.
const instances = await client.bpmn.listInstances({ businessId: 'ORDER-42' });
const tasks = await client.bpmn.listUserTasks({ businessId: 'ORDER-42' });
// DMN evaluations stamp it on the resulting execution row.
const result = await client.dmn.evaluate('loan-eligibility', vars, {
businessId: 'ORDER-42',
});
External job workers
The runtime uses native AbortController for cancellation. Handlers run with per-task-type concurrency limits.
import { Vars, BpmnError } from '@quantumbpm/sdk';
const ac = new AbortController();
const worker = client.newWorker({ clientId: 'billing-svc' });
worker.handle('send-email', async (job, signal) => {
const recipient = job.vars.get<string>('recipient');
await emailer.send(recipient, { signal });
return new Vars().set('messageID', 'msg-123'); // → Complete
}, { maxJobs: 10, lockDuration: '1m' });
process.once('SIGTERM', () => ac.abort());
await worker.run(ac.signal); // resolves once running handlers have finished
Heartbeats keep each job locked to the worker while its handler runs. The server clamps lockDuration 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 handle() throws for an invalid one. When a heartbeat finds the lock gone, the handler's signal aborts and nothing is reported for that job. When the signal passed to run() aborts, the worker stops polling, and running handlers keep heartbeating and report their results for up to shutdownTimeout (a worker option, '20s' by default). Handlers still running then have their signal aborted and are not reported. A job abandoned that way costs a retry, see When a lock runs out. On Kubernetes, keep terminationGracePeriodSeconds above the shutdown timeout.
Throw a BpmnError to fail with a code boundary error events can catch:
throw new BpmnError('INSUFFICIENT_FUNDS', new Vars().set('balance', 12));
Any other thrown value (or rejected promise) is reported with the code WORKER_ERROR and the message attached as the error variable (truncated to 2048 bytes by default, configurable via the maxErrorMessageBytes worker option). A boundary error event whose error definition has errorCode="WORKER_ERROR" catches it once the retry budget is exhausted - see Failure handling.
Use the generic on handle<TVars> for typed dispatch - the SDK exposes job.typed cast to TVars:
interface EmailJob { recipient: string; subject: string }
worker.handle<EmailJob>('send-email', async (job) => {
await emailer.send(job.typed.recipient, job.typed.subject);
return new Vars();
});
Full reference
See the SDK README for the complete API surface and TypeScript type definitions.