# Client from the contract (/client)

The API publishes its contract in OpenAPI 3.1 at `/api/v1/openapi.json`. Build your client from it: the types then match the API that answers your calls. The examples use `openapi-typescript` and `openapi-fetch`.

## Build types from `/api/v1/openapi.json` [#build-types-from-apiv1openapijson]

Take the contract from the API itself, not from this site: the site is for reading and has no copy of the contract. Keep the contract in your repository and compare it with the API in CI.

```bash
curl -s https://rm-api.ovkch.com/api/v1/openapi.json -H "Authorization: Bearer $TOKEN" > openapi.json
npx openapi-typescript openapi.json -o api.d.ts
```

```ts title="client.ts"
import createClient from 'openapi-fetch'
import type { paths } from './api.d.ts'

export const api = createClient<paths>({
  baseUrl: 'https://rm-api.ovkch.com/api/v1',
  headers: { Authorization: `Bearer ${token}` },
})
```

## Send the Cabinet Boundary in `params.header` [#send-the-cabinet-boundary-in-paramsheader]

The contract declares `x-cabinets` as a required header, so `openapi-fetch` requires it in `params.header` of every call. Build it from the Cabinets of the person behind the call. `null` below stands for the whole catalog, `*`.

```ts title="boundary.ts"
export const within = (cabinets: readonly string[] | null) =>
  ({ header: { 'x-cabinets': cabinets === null ? '*' : cabinets.join(',') } }) as const
```

Do not call the API with an empty list: when the person has no Cabinets, report the missing access yourself. The [Cabinet Boundary rule](/rules#send-the-cabinet-boundary-with-every-call) gives the limits.

## Walk a Window with the cursor [#walk-a-window-with-the-cursor]

Pass the `nextCursor` of each Page in `cursor` until a response comes without it:

```ts title="pages.ts"
let cursor: string | undefined
do {
  const { data, error } = await api.GET('/releases', {
    params: { ...within(cabinets), query: { cabinet: ['label-one'], orderBy: 'key', cursor } },
  })
  if (error) throw error
  for (const item of data.items) handle(item)
  cursor = data.nextCursor
} while (cursor)
```

To read a whole Window, order it by `key`: the order by `attention` or `status` changes over time, so Pages can skip or repeat rows. The [Page rule](/rules#read-a-window-page-by-page) describes the cursor.

## Distinguish errors by `code` [#distinguish-errors-by-code]

An error is `application/problem+json`. Choose what to do by `code`, not by the text of `detail`: `detail` is for the developer. Treat a `code` that you do not know as `internal`.

```ts title="errors.ts"
import type { components } from './api.d.ts'

type Problem = components['schemas']['Problem']

const { data, error } = await api.GET('/release', { params: { ...within(cabinets), query: { key } } })
if (error) {
  const problem = error as Problem
  if (problem.code === 'not_found') return undefined
  throw problem
}
```

Each code has its own section on the [rules page](/rules#distinguish-errors-by-code), and the [retry rule](/rules#retry-only-errors-with-retryable-true) says which errors you may retry.

## Choose the language of labels with `Accept-Language` [#choose-the-language-of-labels-with-accept-language]

Labels for people, such as the label of a status, come in the language of the `Accept-Language` header: `en` or `ru`. Without the header, they are in English.

```ts title="labels.ts"
const { data } = await api.GET('/release', {
  params: { header: { 'x-cabinets': 'label-one', 'Accept-Language': 'ru' }, query: { key } },
})
```

Make decisions by codes, not by labels: next to each label is a fixed code. The [language rule](/rules#choose-the-language-of-labels-with-accept-language) lists the labels and explains how to cache them.