Overview
ThefetchApi function is the primary method for making type-safe requests to Roblox API endpoints. It handles authentication, CSRF tokens, error parsing, caching, and retries automatically.
Signature
Parameters
The endpoint definition object containing method, path, baseUrl, and schema information. Use pre-defined endpoints from
rozod/lib/endpoints/* or create custom ones with the endpoint function.Parameters to pass to the endpoint. The type is automatically inferred from the endpoint definition. Can be undefined if the endpoint requires no parameters.
Additional options for the request.
When
true, returns the raw Response object instead of parsed data.When
true, throws an error instead of returning an AnyError object on failure.Number of times to retry the request on network failure.
Delay in milliseconds between retry attempts.
Time in milliseconds to cache the response. Requires
cacheKey to be set.Unique key for caching this request. Required when using
cacheTime.Storage type for caching: in-memory, localStorage, or Chrome storage.
Additional headers to include in the request.
Request mode (cors, no-cors, same-origin).
Credentials mode (include, same-origin, omit).
Return value
Returns aPromise that resolves to either:
- Success response: The typed response data matching the endpoint’s response schema
- Error response: An
AnyErrorobject with amessagefield (whenthrowOnErrorisfalse) - Raw Response: The raw
Responseobject (whenreturnRawistrue) - Throws: An error (when
throwOnErroristrueand request fails)
Examples
Basic request
With error throwing
With retries
With caching
Getting raw response
With custom headers
Type safety
The function provides full type safety:Error handling
By default,fetchApi returns either the success response or an AnyError object. Use the isAnyErrorResponse type guard to check:
The function automatically handles CSRF tokens, hardware-backed authentication (HBA), and generic challenges when configured.
Related functions
- fetchApiPages - Fetch all pages of paginated results
- fetchApiPagesGenerator - Generator for processing pages one at a time
- fetchApiSplit - Split large requests into smaller batches
- endpoint - Define custom endpoints