Call API Handlers from the Integration UI
It’s possible to call an API handler from your integration’s UI. This is useful if you want to interact with an API from the Integration UI. For example:
- create or update environment variables
- update the integration configuration
- get a list of sites for the team
To support this, we make an access token available to callback functions that you can use in calls to the Netlify API. If your integration allows users to authenticate using an OAuth identity provider, your API handler has access to an OAuth token as well.
To call an API handler from within callbacks in the Integration UI, do the following:
-
Use the
fetch
function provided in thesurfaceState
. Thisfetch
function will ensure all the necessary headers are set so that you can make authenticated calls to your API handler. -
Provide the
fetch
function the name of your API handler. As a convenience, thisfetch
function sets a base URL for your API handler, so you only need to provide the name of your API handler. For example, if you created an API handler withintegration.addApiHandler("save-config", ...)
you can call this API handler withfetch("save-config", ...)
. Aside from the preset base URL, thefetch
function works the same as the Fetch API.
Here’s a basic example showing how to call a function to save user input for integration configuration based on user interaction with a button.
Here’s a more complex example of a form element that includes a network call.