Connect external APIs
In a brick, you can fetch data from external APIs and make it available on the brick’s props.
To achieve this, define the getExternalData
function on the brick’s schema
. This function receives the current page, all the brick’s props, and additional arguments that can be passed when calling the fetchPage
function.
It should be an async function that fetches data from external APIs and returns a promise resolving to an object. This object is then merged with the brick’s props, making the external API data available for use within the brick.
Usage Examples
Fetch based on a Page custom field
Fetchs based on a brick’s prop
Result
TypeScript signature
Arguments
Property | Definition |
---|---|
page | The page object for the current page. |
brickProps | The props for this brick. The generic type is inherited from the brick’s interface. |
args | Arguments that can be passed from the fetchPage , for example to receive a querystring parameter. |
Return value
A promise resolving to an object of type Partial<T>
, where T is the brick component’s interface. This returned object is merged with the brick’s props.
Use data fetched at Page level
If you defined the getExternalData
on the pageType
, rather than on the brick’s schema
, you can map the external data fetched at page-level to the brick props using the mapExternalDataToProps
function.
Usage example
mapExternalDataToProps
Signature
The function receives:
- The external data fetched at page level
- The current brick props
The return value should be an object which gets merged with the brick’s props.