[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/client](../../README) / [index](../README) / ApiClient

# Class: ApiClient\<Routes\>

Defined in: [client/src/ApiClient.ts:166](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/client/src/ApiClient.ts#L166)

Type-safe HTTP client bound to a route map.

Do not instantiate directly — use `createApiClient<Routes>(config)`.

## Type Parameters

### Routes

`Routes` *extends* [`ApiRouteMap`](../type-aliases/ApiRouteMap)

## Constructors

### Constructor

> **new ApiClient**\<`Routes`\>(`config?`): `ApiClient`\<`Routes`\>

Defined in: [client/src/ApiClient.ts:181](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/client/src/ApiClient.ts#L181)

#### Parameters

##### config?

[`ApiClientConfig`](../interfaces/ApiClientConfig) = `{}`

#### Returns

`ApiClient`\<`Routes`\>

## Methods

### setToken()

> **setToken**(`token`): `void`

Defined in: [client/src/ApiClient.ts:211](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/client/src/ApiClient.ts#L211)

Update (or clear) the bearer token used for subsequent requests.

#### Parameters

##### token

[`TokenSource`](../type-aliases/TokenSource) \| `null`

#### Returns

`void`

***

### get()

> **get**\<`Path`\>(`path`, `params?`, `options?`): `Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"GET"`, `Path`\>\>

Defined in: [client/src/ApiClient.ts:224](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/client/src/ApiClient.ts#L224)

Send a typed GET request.

#### Type Parameters

##### Path

`Path` *extends* `string`

#### Parameters

##### path

`Path`

##### params?

[`ParamRecord`](../type-aliases/ParamRecord)\<`Path`\>

##### options?

[`GetOptions`](../interfaces/GetOptions)\<[`QueryOf`](../type-aliases/QueryOf)\<`Routes`, `"GET"`, `Path`\>\>

#### Returns

`Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"GET"`, `Path`\>\>

#### Example

```ts
const user = await api.get('/api/users/{id}', { id: 1 });   // ^? UserResource
const list = await api.get('/api/users', undefined, { query: { page: 2 } });
```

***

### post()

> **post**\<`Path`\>(`path`, `body?`, `options?`): `Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"POST"`, `Path`\>\>

Defined in: [client/src/ApiClient.ts:247](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/client/src/ApiClient.ts#L247)

Send a typed POST request. The body may be a plain object (JSON), `FormData`, `Blob`,
`URLSearchParams`, or a string.

#### Type Parameters

##### Path

`Path` *extends* `string`

#### Parameters

##### path

`Path`

##### body?

[`BodyOf`](../type-aliases/BodyOf)\<`Routes`, `"POST"`, `Path`\>

##### options?

[`MutationOptions`](../interfaces/MutationOptions)\<[`QueryOf`](../type-aliases/QueryOf)\<`Routes`, `"POST"`, `Path`\>\>

#### Returns

`Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"POST"`, `Path`\>\>

#### Example

```ts
await api.post('/api/users', { name: 'Alice', email: 'alice@example.com' });
await api.post('/api/avatars', formData);   // multipart upload
```

***

### put()

> **put**\<`Path`\>(`path`, `body?`, `options?`): `Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"PUT"`, `Path`\>\>

Defined in: [client/src/ApiClient.ts:257](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/client/src/ApiClient.ts#L257)

#### Type Parameters

##### Path

`Path` *extends* `string`

#### Parameters

##### path

`Path`

##### body?

[`BodyOf`](../type-aliases/BodyOf)\<`Routes`, `"PUT"`, `Path`\>

##### options?

[`MutationOptions`](../interfaces/MutationOptions)\<[`QueryOf`](../type-aliases/QueryOf)\<`Routes`, `"PUT"`, `Path`\>\>

#### Returns

`Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"PUT"`, `Path`\>\>

***

### patch()

> **patch**\<`Path`\>(`path`, `body?`, `options?`): `Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"PATCH"`, `Path`\>\>

Defined in: [client/src/ApiClient.ts:267](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/client/src/ApiClient.ts#L267)

#### Type Parameters

##### Path

`Path` *extends* `string`

#### Parameters

##### path

`Path`

##### body?

[`BodyOf`](../type-aliases/BodyOf)\<`Routes`, `"PATCH"`, `Path`\>

##### options?

[`MutationOptions`](../interfaces/MutationOptions)\<[`QueryOf`](../type-aliases/QueryOf)\<`Routes`, `"PATCH"`, `Path`\>\>

#### Returns

`Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"PATCH"`, `Path`\>\>

***

### delete()

> **delete**\<`Path`\>(`path`, `params?`, `options?`): `Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"DELETE"`, `Path`\>\>

Defined in: [client/src/ApiClient.ts:277](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/client/src/ApiClient.ts#L277)

#### Type Parameters

##### Path

`Path` *extends* `string`

#### Parameters

##### path

`Path`

##### params?

[`ParamRecord`](../type-aliases/ParamRecord)\<`Path`\>

##### options?

[`RequestOptions`](../interfaces/RequestOptions)

#### Returns

`Promise`\<[`ResponseOf`](../type-aliases/ResponseOf)\<`Routes`, `"DELETE"`, `Path`\>\>
