# Fetching Data

## Fetch Data Through HTTP

There are two main ways to fetch data through HTTP in the Umbraco Backoffice:

* [Fetch API](#fetch-api)
* [Umbraco HTTP Client](#umbraco-http-client).

The Fetch API is a modern way to make network requests in JavaScript, while the Umbraco HTTP client is a wrapper around it, providing a more convenient interface.

For most scenarios, the Umbraco HTTP Client is recommended because it:

* Automatically handles authentication and error handling.
* Provides type safety for requests and responses.
* Simplifies request and response parsing.
* Integrates seamlessly with the Backoffice.

The Fetch API is an alternative for simpler use cases.

The following table provides a comparison of the two options:

| Feature              | [Fetch API](/umbraco-cms/18.latest/extend-your-project/backoffice-extensions/foundation/fetching-data/fetch-api.md) | [Umbraco HTTP Client](/umbraco-cms/18.latest/extend-your-project/backoffice-extensions/foundation/fetching-data/http-client.md) |
| -------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| Authentication       | Manual                                                                                                              | Automatic                                                                                                                       |
| Error Handling       | Manual                                                                                                              | Built-in                                                                                                                        |
| Type Safety          | No                                                                                                                  | Yes                                                                                                                             |
| Request Cancellation | Yes (via AbortController)                                                                                           | Yes (via AbortController)                                                                                                       |
| Recommended Use Case | Common requests                                                                                                     | Complex or frequent requests                                                                                                    |

After selecting a method, refer to the sections below for implementation details and guidance on handling the received data.

### [Fetch API](/umbraco-cms/18.latest/extend-your-project/backoffice-extensions/foundation/fetching-data/fetch-api.md)

The Fetch API is a modern way to make network requests in JavaScript. It provides a more powerful and flexible feature set than the older XMLHttpRequest.

### [Umbraco HTTP Client](/umbraco-cms/18.latest/extend-your-project/backoffice-extensions/foundation/fetching-data/http-client.md)

The Umbraco HTTP Client is a wrapper around the Fetch API that provides a more convenient way to make network requests. It handles request and response parsing, error handling, and retries.

## Handle Requests

Once you have chosen a method to fetch data, the next step is to handle the execution of requests. This includes managing errors, refreshing tokens, and ensuring proper authentication.

## [Executing Requests](/umbraco-cms/18.latest/extend-your-project/backoffice-extensions/foundation/fetching-data/try-execute.md)

After fetching data, the next step is to execute the request. You can use the `tryExecute` function to handle errors and refresh the token if it is expired.

## Advanced Topics

### [Custom Generated Client](/umbraco-cms/18.latest/extend-your-project/backoffice-extensions/foundation/fetching-data/custom-generated-client.md)

For advanced scenarios, you can generate a custom client for your API using tools like [@hey-api/openapi-ts](https://github.com/hey-api/openapi-ts). This approach is ideal when working with custom API controllers or when you need type-safe, reusable client code.

### [Repositories](/umbraco-cms/18.latest/extend-your-project/backoffice-extensions/foundation/repositories.md)

Repositories provide a structured way to manage data operations in the Backoffice. They abstract the data access layer, allowing for easier maintenance and scalability.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.umbraco.com/umbraco-cms/18.latest/extend-your-project/backoffice-extensions/foundation/fetching-data.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
