Skip to content

Call Dockt from TypeScript

This guide calls Dockt from a TypeScript backend. It generates request and response types from Dockt’s OpenAPI description, so your editor checks the API calls.

The example uses native fetch. It announces a worker with an Assignment, uploads a document to the Person, and reads the Decision.

In your application project, make sure you have:

  • Node.js 20 or another server runtime with fetch, FormData and Blob.
  • An API credential with account:read and account:write, stored as DOCKT_API_TOKEN.
  • TypeScript configured for your backend.
  • A Workplace with an active Policy, created by following Get started with Dockt. The Assignment example refers to its external_ref, plant-antwerp.

Never expose DOCKT_API_TOKEN to browser or mobile code.

Generate a TypeScript declaration file from Dockt’s OpenAPI URL:

Terminal window
npx openapi-typescript https://api.dockt.com/openapi.json \
--root-types \
--root-types-no-schema-prefix \
--output src/dockt-schema.d.ts

Commit the generated file so you can review contract changes before you update your integration.

Create src/dockt.ts:

import type {
Assignment,
CreateAssignmentBody,
Decision,
Document,
ProblemDetails
} from './dockt-schema';
type SingleEnvelope<T> = {
object: 'single';
data: T;
};
export class DocktApiError extends Error {
constructor(
readonly status: number,
readonly requestId: string | null,
readonly problem: ProblemDetails | null
) {
super(problem?.detail ?? `Dockt request failed with status ${status}.`);
}
}
export const createDocktClient = (token: string) => {
const request = async <T>(path: string, init: RequestInit = {}) => {
const headers = new Headers(init.headers);
headers.set('authorization', `Bearer ${token}`);
headers.set('accept', 'application/json');
const response = await fetch(new URL(path, 'https://api.dockt.com'), {
...init,
headers,
signal: init.signal ?? AbortSignal.timeout(30_000)
});
const body = await response.json().catch(() => null);
if (!response.ok) {
throw new DocktApiError(
response.status,
response.headers.get('x-request-id'),
body as ProblemDetails | null
);
}
return body as T;
};
return {
async createAssignment(
body: CreateAssignmentBody & { dry_run?: false; evaluate_for?: never },
idempotencyKey: string
) {
const response = await request<SingleEnvelope<Assignment>>('/v1/assignments', {
method: 'POST',
headers: {
'content-type': 'application/json',
'idempotency-key': idempotencyKey
},
body: JSON.stringify(body)
});
return response.data;
},
async getAssignment(assignmentId: string) {
const response = await request<SingleEnvelope<Assignment>>(
`/v1/assignments/${encodeURIComponent(assignmentId)}`
);
return response.data;
},
async uploadPersonDocument(input: {
personId: string;
file: Blob;
filename: string;
documentType?: string;
idempotencyKey: string;
}) {
const form = new FormData();
form.set('file', input.file, input.filename);
if (input.documentType) form.set('document_type', input.documentType);
const response = await request<SingleEnvelope<Document>>(
`/v1/persons/${encodeURIComponent(input.personId)}/documents`,
{
method: 'POST',
headers: { 'idempotency-key': input.idempotencyKey },
body: form
}
);
return response.data;
},
async getDecision(decisionId: string) {
const response = await request<SingleEnvelope<Decision>>(
`/v1/decisions/${encodeURIComponent(decisionId)}`
);
return response.data;
}
};
};

This createAssignment method handles stored Assignments only. A dry run returns a different response type, AssignmentDryRun; use a separate method if you need it.

Don’t set a multipart Content-Type header when you send FormData. Your runtime adds the boundary.

Use the client from your server code. Refer to records by your own IDs through external_ref, so a retry never creates a duplicate:

import { createDocktClient } from './dockt';
const dockt = createDocktClient(process.env.DOCKT_API_TOKEN!);
const assignment = await dockt.createAssignment(
{
external_ref: 'preannouncement-9031',
workplace: { external_ref: 'plant-antwerp' },
person: {
external_ref: 'worker-4821',
attributes: {
'person.first_name': 'Jan',
'person.last_name': 'Peeters',
'person.date_of_birth': '1988-04-02',
'person.nationality': ['BE']
}
},
employer: {
external_ref: 'comp-77',
name: 'Alfa Welding BV',
country: 'BE',
registration_number: 'BE0123456789'
},
period: { start: '2026-11-03', end: null },
attributes: {
'assignment.employment_relationship': 'employee',
'assignment.posting_status': 'local'
}
},
'assignment-preannouncement-9031'
);
console.log(assignment.id, assignment.person.id, assignment.status);

Store assignment.id and assignment.person.id with your record before your backend reports success. latest_decision is null until Dockt has evaluated the work.

Read the file in your backend and upload it to the Person:

const document = await dockt.uploadPersonDocument({
personId: assignment.person.id,
file: new Blob([documentBytes], { type: 'application/pdf' }),
filename: 'identity-card.pdf',
documentType: 'passport_id',
idempotencyKey: 'worker-4821-identity-card'
});
console.log(document.id, document.status);

documentBytes is the file your backend received from your own upload or storage layer. A new upload returns status: "processing". If the file or reference already exists for that Person, Dockt returns the existing Document with its current status.

Handle decision.created in your webhook receiver and fetch the Decision it names:

const onDecisionCreated = async (event: {
data: { decision: { id: string }; target: { type: 'assignment' | 'engagement'; id: string } };
}) => {
const decision = await dockt.getDecision(event.data.decision.id);
const missing = decision.requirements.filter((requirement) => requirement.status === 'missing');
console.log(decision.result, missing.map((requirement) => requirement.rule_code), decision.input_requests);
};

Store the Decision ID your application acts on. Before a delayed or important action, fetch the Assignment again and check that its latest_decision.id still matches.

  • Set a request timeout. The example uses a 30-second abort signal; configure additional connection timeouts if your HTTP client supports them.
  • Retry only failures that can succeed later: network errors, 429, 500 and 503.
  • Reuse the same idempotency key and body when you retry a create or upload.
  • Branch on the Problem Details type, not on title or detail.
  • Log x-request-id, but never log credentials or document contents.

Continue with Receive webhooks and Test your integration.