callApi function is the primary method for making HTTP requests. It’s a pre-configured instance created from createFetchClient() with default settings.
Function Signature
Parameters
The URL endpoint to make the request to. Can be a full URL or a relative path when used with
baseURL.Configuration object for the request. Includes all standard fetch options plus CallApi-specific features.
Request Options
HTTP method for the request. Defaults to
'GET' for most cases.Request headers as an object, Headers instance, or array of tuples.
Request body. Automatically serialized based on content-type or
bodySerializer.AbortSignal for canceling requests.
CallApi Options
Base URL prepended to relative
initURL paths.Path parameters for URL templates (e.g.,
/users/:id).Query parameters appended to the URL.
'simple': Returnsdatadirectly on success, throws on error'result': Returns{ data, error, response }object
Whether to throw errors instead of returning them. Can be a function for conditional throwing.
Expected response data type.
Request timeout in milliseconds.
Retry configuration for failed requests.
Request deduplication strategy (
'cancel', 'defer', or 'ignore').Schema Validation
Schema for validating request/response data.
Configuration for schema validation behavior.
Hooks
Called before request is sent.
Called after request is prepared and validated.
Called on successful response (2xx status).
Called on any error.
Called on HTTP errors (non-2xx status).
Called on network/request errors.
Called on schema validation errors.
Called on any response (success or error).
Return Type
Usage Examples
Basic GET Request
POST Request with Body
Using Result Mode
Path Parameters and Query Strings
Error Handling with Typed Error Data
Using Hooks for Logging
Request Timeout and Retry
Request Deduplication
Using AbortSignal
Type Inference
CallApi provides excellent TypeScript type inference:Related
- createFetchClient - Create a custom client with default configuration
- createFetchClientWithContext - Create a client factory with custom context types