PAPI: Caller🔗
The API Caller tool allows arbitrary API endpoints to be called. Following from the example in the Builder doc:
Calling endpoints
const data = { title: "studioKeywi's Loudest Hits!" };
const newAlbum = await apiCaller.albums.post({ data });
if (!('id' in newAlbum)) throw new Error(`API Post failed and returned ${newAlbum}`);
const photos = await apiCaller.albums[newAlbum.id].photos.get();
if (!('length' in photos)) throw new Error(`API Get failed and returned ${photos}`);
photos.forEach(photo => {
// ...
});
The types for PAPI assist with representing responses from the calls you make with the shapes defined in the builder. This includes:
- Knowing which HTTP methods should be available for a given endpoint
- Knowing whether an endpoint can receive a body of data
- Knowing whether an endpoint can receive query parameters
- Knowing whether an endpoint has a different error response shape
- Knowing whether an endpoint's data and/or query parameters are optional or required
- Knowing whether an endpoint returns a different response when passing bodies of data, query parameters, or as requested
Warning
As a reminder, PAPI does not enforce or guarantee response shapes. It is intended as an augment to developer experience
API Caller Configuration🔗
The API Caller tool wraps around the fetch
WebAPI implementation available to the JS runtime, and all of the standard configuration values for the second argument to a fetch
call can be passed
Additionally, the following fields may be provided to alter the caller's behavior:
Provides JSON information used as the body of a request. Internally this will get converted with JSON.stringify
and sent as the body
property of the second argument to the fetch
call
Sets the automatic parsing mode for the request to one of arrayBuffer
, blob
, formData
, json
, raw
or text
(default: json
). The raw
mode may be used to directly return the fetch
response without processing
Provides query parameter values in a { [param]: value }
syntax. Internally this will be appended as a query parameter string to the URL in the first argument of the fetch
call