# TypeScript SDK

Install @dimmi/api to call the dimmi REST API from TypeScript with generated types, cursor pagination, and automatic retries.

`@dimmi/api` wraps [openapi-fetch](https://openapi-ts.dev/openapi-fetch/) with types generated from dimmi's OpenAPI contract. It works on Node.js 20+ or any runtime with `fetch`.

```bash
npm install @dimmi/api
```

## Create a client and make a call

```ts title="dimmi.ts"

const api = createDimmiApi({
  host: "https://trydimmi.com",
  token: process.env.DIMMI_API_TOKEN!,
});

const created = await unwrap(
  api.POST("/contacts", { body: { email: "ada@example.com", name: "Ada Lovelace" } }),
);

console.log(created.data.id);
```

Set `DIMMI_API_TOKEN` to your [API token](/developers/api/authentication/). If you self-host, point `host` to your own dimmi URL.

`api.GET`, `api.POST`, `api.PATCH`, `api.PUT`, and `api.DELETE` accept every path in the [API reference](/api-reference/). Each returns `{ data, error, response }`, and `unwrap()` returns `data` or throws.

## Paginate list endpoints

```ts

for await (const conversation of paginate(api, "/conversations", {
  params: { query: { "filter[status]": "open" } },
})) {
  console.log(conversation.id);
}
```

`paginate()` yields one item at a time and fetches new pages as you iterate. Use `pages()` with the same arguments if you want each raw page response instead.

## Handle API errors

```ts

try {
  await unwrap(api.POST("/contacts", { body: { email: "not-an-email" } }));
} catch (error) {
  if (error instanceof DimmiApiError) {
    console.log(error.status, error.code, error.errors);
  }
}
```

`unwrap()`, `paginate()`, and `pages()` throw `DimmiApiError`, exposing `status`, `code`, and `errors` from [API errors](/developers/api/errors/).

## Client options

| Option | Default | What it does |
| --- | --- | --- |
| `host` | None | Your dimmi base URL. The client appends `/api/v1`. |
| `baseUrl` | None | Full API URL, used instead of `host`. |
| `maxRetries` | `3` | Retry count after a `429` rate limit response. |
| `maxRetryDelay` | `30000` | Max retry delay in milliseconds (respects `Retry-After`). |
| `autoIdempotency` | `false` | Adds an [`Idempotency-Key`](/developers/api/idempotency/) to every `POST` that lacks one. |
| `headers` | None | Extra headers sent on every request. |
| `fetch` | `globalThis.fetch` | Custom `fetch` implementation. |

This package does not include webhook verification. For Node.js webhook checks, see [Verify webhook signatures](/developers/webhooks/verify-signatures/).

---

Index of all dimmi docs: https://trydimmi.com/llms.txt (full text: https://trydimmi.com/llms-full.txt)
