Skip to main content
zerotal

Documentation


Documentation / zerotal / client / createApiClient

Function: createApiClient()

createApiClient<Routes>(config?): ApiClient<Routes>

Defined in: packages/client/src/index.ts:64

Create a type-safe API client bound to your route map.

Type Parameters

Routes

Routes extends Partial<Record<`GET ${string}` | `POST ${string}` | `PUT ${string}` | `DELETE ${string}` | `PATCH ${string}`, RouteShape>>

Parameters

config?

ApiClientConfig = {}

Returns

ApiClient<Routes>

Example

// frontend/api.ts
import { createApiClient } from '@zerotal/client';
import type { Routes } from './routes';

export const api = createApiClient<Routes>({
  baseUrl: '/api',
  headers: { 'X-Requested-With': 'XMLHttpRequest' },
});

// Anywhere in your UI:
const user = await api.get('/users/{id}', { id: 1 });
//    ^? UserResource  (inferred from Routes)

const newUser = await api.post('/users', { name: 'Alice', email: 'alice@ex.com' });