Skip to main content

Import

What it does

ApiClient is the main request abstraction in ByteKit. It wraps the Fetch API with timeouts, request/response interceptors, automatic retries, circuit breaker protection, localized error messages, and optional response validation through built-in schemas or external libraries like Zod and Valibot.

Constructor

ApiClientConfig

Basic setup

Methods

HTTP verbs

post, put, and patch accept either a plain body object or a full RequestOptions object with a body property — whichever is more convenient.

RequestOptions<T>

ListOptions<T>

Extends RequestOptions with pagination, sorting, and filter helpers.

Examples

Typed GET

POST with payload

POST with full options

Paginated list

Response validation with Zod

Interceptors

Interceptors let you transform requests before they are sent and responses before they are returned.

ApiError

Thrown when a response has a non-2xx status code.
ApiError also exposes a details getter, toJSON(), and a readable toString() for debugging.
v3: ApiError#toJSON(), ApiError#toString(), and ApiError#details now sanitize the response body by default. Sensitive fields (tokens, passwords, cookies, API keys, etc.) are redacted and long strings are truncated. If you need the raw body, read error.body directly in controlled code — do not log or serialize it in production.

Factory

createApiClient is a shortcut for new ApiClient(config).
ApiClient requires either baseUrl or baseURL in the config. An error is thrown at construction time if neither is provided.