Skip to main content

TypeScript client

@blokkio/api is a small TypeScript client for the public API. It has no runtime dependencies, uses the global fetch, and covers every v1 endpoint.

:::note Not on npm yet The client is not published to npm. Its source is in the Blokkio backend repository under sdk/typescript. Build it from there, or copy src/index.ts (a single file) into your project. Until it is published, you can also generate a client from the OpenAPI document. :::

Install​

From the backend repository:

cd sdk/typescript
npm install
npm run build # emits dist/ (ES modules + .d.ts)

Then add it to your project from that folder, for example:

npm install ../path/to/backend/sdk/typescript

It needs a runtime with a global fetch (Node.js 18+, Deno, Bun, browsers), or you can pass your own fetch.

warning

Don't use an API key in browser code. Anyone who opens the page can read it. Call the API from your server.

Create a client​

import { BlokkioClient } from '@blokkio/api';

const blokkio = new BlokkioClient({
baseUrl: 'https://api.blokkio.com', // API origin, without /api/v1
apiKey: process.env.BLOKKIO_API_KEY! // bk_live_…
// fetch: customFetch, // optional
});

The client sends the key as Authorization: Bearer …. The key's scopes decide which methods you can call.

MethodEndpointScope
listWorkflows({ limit, cursor })GET /api/v1/workflowsworkflows:read
getWorkflow(workflowId)GET /api/v1/workflows/{id}workflows:read
executeWorkflow(workflowId, body, { wait, timeoutSeconds })POST /api/v1/workflows/{id}/executeworkflows:execute
listExecutions({ workflowId, status, limit, cursor })GET /api/v1/executionsexecutions:read
getExecution(executionId, { includeInput })GET /api/v1/executions/{id}executions:read
cancelExecution(executionId)POST /api/v1/executions/{id}/cancelexecutions:cancel
paginate(fetchPage)Iterates every item of a list(same as the list)

Examples​

Run a workflow and wait for the result​

const result = await blokkio.executeWorkflow(
workflowId,
{ data: { orderId: 42 } },
{ wait: true, timeoutSeconds: 30 } // max 60
);

if (result.status === 'success') {
console.log(result.output); // output of the last node that finished
} else {
console.log(`Run ${result.executionId} is ${result.status}`);
}

If the run is still going at the timeout, the promise resolves with a non-final status such as running or waiting_for_approval. Check it later with getExecution.

Fire and forget, then poll​

const { executionId } = await blokkio.executeWorkflow(workflowId, { data: { orderId: 42 } });

const final = ['success', 'warning', 'failed', 'cancelled'];
let execution = await blokkio.getExecution(executionId);
while (!final.includes(execution.status)) {
await new Promise((r) => setTimeout(r, 2000));
execution = await blokkio.getExecution(executionId);
}

for (const node of execution.nodes) {
console.log(node.nodeName, node.status);
}

Start from a specific trigger or environment​

const wf = await blokkio.getWorkflow(workflowId);
const webhook = wf.triggers.find((t) => t.kind === 'webhook');

await blokkio.executeWorkflow(workflowId, {
data: { orderId: 42 },
triggerId: webhook?.id,
environmentId: stagingEnvironmentId
});

Page through every workflow​

for await (const wf of blokkio.paginate((cursor) => blokkio.listWorkflows({ cursor, limit: 100 }))) {
console.log(wf.id, wf.name, wf.isPublished);
}

List recent failures​

const page = await blokkio.listExecutions({ workflowId, status: 'failed', limit: 10 });
for (const e of page.data) console.log(e.id, e.startedAt);

Cancel a run​

await blokkio.cancelExecution(executionId); // { executionId, status: 'cancelling' }

Errors​

Any non-2xx response throws a BlokkioApiError with the HTTP status and the parsed problem details:

import { BlokkioApiError } from '@blokkio/api';

try {
await blokkio.executeWorkflow(workflowId, { data: { orderId: 42 } });
} catch (e) {
if (e instanceof BlokkioApiError) {
if (e.status === 409 && e.problem.type === 'workflow-not-published') {
// publish the workflow first
} else if (e.status === 429) {
// rate limited, back off and retry
}
console.error(e.status, e.problem.type, e.problem.detail, e.problem.errors);
} else {
throw e; // network error etc.
}
}

A 202 from executeWorkflow is a success and does not throw.

Types​

The package exports types for every request and response: Workflow, WorkflowDetails, Trigger, ExecuteWorkflowRequest, ExecutionResult, Execution, ExecutionDetails, NodeResult, Page<T>, ExecutionStatus, ApiKeyScope, ProblemDetails and Json. Timestamps are ISO 8601 strings.