For the complete documentation index, see llms.txt. This page is also available as Markdown.

Fetch API

Use the Fetch API to request data from third-party APIs in the backoffice, and learn what it does not handle for requests to Umbraco.

The Fetch API is the built-in way to make network requests in the browser. Use the Fetch API in the backoffice to request data from third-party APIs.

Requests to Umbraco

To call the Management API or your own backoffice API controllers, use the Umbraco HTTP Client. For your own API controllers, a Custom Generated Client also gives you a typed function for each endpoint. The Management API endpoints start with /umbraco/management/api/.

The Umbraco HTTP Client handles the following for you, and the Fetch API does not:

  • Adds the access token of the current user.

  • Asks the user to log in again after a 401 response, and retries GET requests once the user has logged in.

  • Turns error responses into problem details, so tryExecute shows the title and detail from the server.

  • Shows the notifications that the server sends in the Umb-Notifications header.

  • Returns the ID from the Umb-Generated-Resource header.

Make a Request

The following example requests data from a third-party API. The Fetch API does not throw for error responses. The function throws an error itself when the response is not successful, so tryExecute can show a notification:

forecast.ts
export async function getForecast(city: string) {
  const response = await fetch(`https://api.example.com/forecast?city=${encodeURIComponent(city)}`);

  if (!response.ok) {
    throw new Error(`Request failed with status ${response.status}`);
  }

  return { data: await response.json() };
}

Authenticate Requests to Umbraco

When you cannot use the Umbraco HTTP Client for a request to Umbraco, add the authentication yourself:

  • Get the token from getLatestToken() on the UMB_AUTH_CONTEXT, and send it in the Authorization: Bearer <token> header.

  • Set credentials: 'include', so the browser sends the authentication cookies.

The following function wraps the Fetch API and adds both to every request:

The function returns the JSON from the response as data. Change how it reads the body for endpoints that return an empty body.

For an unsuccessful response, the function throws an UmbApiError with the HTTP status. The error carries the problem details when the server sends them.

getLatestToken() refreshes an expired token before it returns. You only get a 401 response when the session can no longer be refreshed. The Fetch API does not ask the user to log in again. tryExecute shows no notification for the 401 error from makeRequest(), so handle that error yourself, or use the Umbraco HTTP Client.

The token belongs to the current backoffice user, so a request has the permissions of that user. External applications authenticate as an API User instead, as described in the API Users article.

Other HTTP Libraries

You can use another HTTP library, such as Axios, in the same way. The getOpenApiConfiguration() method on the UMB_AUTH_CONTEXT returns the server URL as base, credentials: 'include', and a token() function:

Like the Fetch API, another HTTP library does not ask the user to log in again or turn error responses into problem details. The library does not show server notifications or return the ID from the Umb-Generated-Resource header either.

Executing the Request

Wrap the request in tryExecute, which shows a notification when the request throws. tryExecute returns the value that the request resolves to, or an object with error when the request throws. So resolve with the body as data, and throw when the response is not successful, as getForecast() and makeRequest() do.

The error that the request throws decides whether tryExecute shows a notification:

  • An UmbApiError with the HTTP status, like the error from makeRequest(), follows the rules for the Umbraco HTTP Client. For example, a 401, 403 or 404 response shows no notification. For all the rules, see When Notifications Are Shown.

  • A plain Error, like the error from getForecast(), shows a notification with the error message, whatever the status.

The following example wraps makeRequest() in tryExecute, in an element that extends UmbLitElement:

For more information about tryExecute, see the Executing Requests article.

Last updated

Was this helpful?