Saltar al contenido principal

TypeScript Client

@dsptch-work/api-client is a generated, typed TypeScript client for the DSPTCH public REST API. It gives you one class per resource, method names that match the API's operation IDs, fully typed request and response shapes, and a bundled fetch client — so you can call the API from Node, a browser, or an edge runtime without hand-writing requests.

The client is generated from the same OpenAPI specification that powers the API reference, so it always matches the documented surface.

Installing

npm install @dsptch-work/api-client
# or: pnpm add @dsptch-work/api-client · yarn add @dsptch-work/api-client

It is a public package — installing needs no registry configuration and no authentication. Making authenticated API calls does require a DSPTCH API key.

Requires Node 18 or newer (it uses the built-in fetch), or any modern browser/bundler.

Your first call

Configure your API key once, then call any endpoint. Each method returns { data, error } — no exceptions to catch on a normal error response.

import { Jobs } from '@dsptch-work/api-client';
import { client } from '@dsptch-work/api-client/client';

// The returned string is sent as the `X-Api-Key` header on every authenticated
// request. A function is used so the key can be resolved lazily — e.g. from
// `process.env` in a Node script, or from a secret store.
client.setConfig({ auth: () => process.env.DSPTCH_API_KEY });

const { data, error } = await Jobs.listJobs({
query: { 'filter[project_id]': projectId },
});

if (error) {
throw new Error(`Failed to list jobs: ${JSON.stringify(error)}`);
}

console.log(data);

The base URL (https://api.dsptch.app) is baked in, so there is nothing to configure for production. Override it per environment with client.setConfig({ baseUrl: '…' }).

Paginating list endpoints

List endpoints are cursor-paginated: each response carries a meta.page.next_cursor, and a null cursor is the last page. Rather than loop on that by hand, hand a list method to paginate and iterate every item across every page:

import { Jobs, paginate, collect } from '@dsptch-work/api-client';

for await (const job of paginate(Jobs.listJobs, { query: { 'filter[project_id]': projectId } })) {
process(job); // `job` is typed as the Jobs list element
}

// Or, when every page fits in memory, drain it into an array:
const jobs = await collect(paginate(Jobs.listJobs));

The item type is inferred from the method, and the options keep the method's own shape — required where the endpoint needs a path id, optional otherwise.

What's exported

Import pathContents
@dsptch-work/api-clientThe resource classes (Jobs, TimeCards, …), every type, and paginate / collect
@dsptch-work/api-client/clientThe configured client instance — use it for setConfig
@dsptch-work/api-client/zodZod schemas mirroring the spec, for callers that want to validate responses

Versioning

The client follows Semantic Versioning and is currently in the 0.x range: the API is still converging with the DSPTCH web app, so breaking changes can land between minor versions. Pin a version you have tested and review the changelog before upgrading. For contract-level changes across the whole API — including endpoints you call directly over HTTP — see the API changelog.