Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
beaf4d3
docs: adds article on working with the Fetch API
iOvergaard May 8, 2025
05c9a9a
docs: adds article on http-client
iOvergaard May 8, 2025
a129be6
docs: adds overview and main README
iOvergaard May 8, 2025
5661fcc
docs: updates code samples after testing
iOvergaard May 8, 2025
20a72e7
docs: corrects wrong variable use
iOvergaard May 8, 2025
2154db3
docs: adds an article on how to execute requests
iOvergaard May 14, 2025
bf64093
docs: adds reference to new article
iOvergaard May 14, 2025
5d7e975
docs: removes warning
iOvergaard May 14, 2025
590dfe5
docs: adds an article on generating your own custom client
iOvergaard May 14, 2025
1b2687d
docs: adds bullets to why to choose the http-client
iOvergaard May 14, 2025
dd9ce4a
docs: name the client the same
iOvergaard May 14, 2025
ae00ae6
docs: clarify what a custom client is
iOvergaard May 14, 2025
dc0ad38
docs: clarify what the umbraco http client is
iOvergaard May 14, 2025
a5177c5
docs: adds an advanced topics section
iOvergaard May 14, 2025
f90e7c7
docs: adds examples on authentication
iOvergaard May 14, 2025
1ad9cee
docs: adds extra bullet
iOvergaard May 14, 2025
0666e56
docs: align naming of http client
iOvergaard May 14, 2025
d4a4236
docs: simplify example
iOvergaard May 14, 2025
d3e6d16
docs: adds warning about error handling
iOvergaard May 14, 2025
e60bde1
docs: adds links and other helpful tips
iOvergaard May 14, 2025
fa5af18
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
iOvergaard May 14, 2025
cf56998
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
iOvergaard May 16, 2025
c6a5c94
Apply suggestions from code review
iOvergaard May 16, 2025
95e3067
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh May 16, 2025
ce084da
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh May 16, 2025
efca7a4
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh May 16, 2025
8478a29
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh May 16, 2025
1610574
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh May 16, 2025
fb99c21
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
8e7b26a
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
656054a
Update 16/umbraco-cms/customizing/foundation/fetching-data/try-execut…
eshanrnh May 16, 2025
ed25570
Update 16/umbraco-cms/customizing/foundation/fetching-data/try-execut…
eshanrnh May 16, 2025
1e03647
Update 16/umbraco-cms/customizing/foundation/fetching-data/try-execut…
eshanrnh May 16, 2025
0459273
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh May 16, 2025
a4d1712
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh May 16, 2025
a3b17c3
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh May 16, 2025
150a085
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
538ed00
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
a29574f
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
3fceda7
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
89e917f
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
742d345
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
ed1df4f
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
16dfc18
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
f957799
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
fcba065
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
5822ddd
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
ebae84f
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh May 16, 2025
7e796c8
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh May 16, 2025
e7fa87b
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh May 16, 2025
97b8ac9
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh May 16, 2025
c91b4f9
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh May 16, 2025
ac48356
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh May 16, 2025
7b4d4bf
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
69b4b52
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
8131140
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
9eece0d
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
9eefa33
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
930d333
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
b90ba6a
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
545c7cd
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
d4b5a1a
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh May 16, 2025
3dc3c9e
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
d88e2d2
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh May 16, 2025
682e137
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh May 16, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: adds examples on authentication
  • Loading branch information
iOvergaard committed May 14, 2025
commit f90e7c78fce4a50faa648fe96594b0facf9cd616
51 changes: 43 additions & 8 deletions 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,58 @@ The example assumes that you have a controller set up at the `/umbraco/MyApiCont

When making requests to the Umbraco API controllers, you may need to include an authorization token in the request headers. This is especially important when making requests to endpoints that require authentication.

The Fetch API does not automatically include authentication tokens in requests. You need to manually add the authentication token to the request headers. This can be done by adding an `Authorization` header to the request:
The Fetch API does not automatically include authentication tokens in requests. You need to manually add the authentication token to the request headers. While you can manage tokens manually, the recommended approach in the Backoffice is to use the **UMB_AUTH_CONTEXT**. This context provides tools to manage authentication tokens and ensures that your requests are properly authenticated.

### Example: Using `UMB_AUTH_CONTEXT` for Authentication

The following example demonstrates how to use `UMB_AUTH_CONTEXT` to retrieve the latest token and make an authenticated request:

```javascript
const token = 'your-auth-token';
const data = await fetch('/umbraco/MyApiController/GetData', {
method: 'GET',
import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
import type { UmbClassInterface } from '@umbraco-cms/backoffice/class-api';

async function fetchData(host: UmbClassInterface, endpoint: string) {
// Retrieve the authentication context
const authContext = await host.getContext(UMB_AUTH_CONTEXT);

// Get the latest token
const token = await authContext?.getLatestToken();

// Make the authenticated request
const response = await fetch(endpoint, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
});

if (!response.ok) {
throw new Error('Failed to fetch data');
}

return response.json();
}

// Example usage
const data = await fetchData(this, '/umbraco/management/api/v1/server/status');
console.log(data);
```

{% hint style="info" %}
The example assumes that you have a valid authentication token. You can replace this with your own token as needed. Read more about authentication in the [Security](../../../implementation/security.md) article.
{% endhint %}

Why Use **UMB_AUTH_CONTEXT**?

- Simplifies Token Management: Automatically retrieves and refreshes tokens when needed.
- Aligns with Best Practices: Ensures your requests are authenticated in a way that integrates seamlessly with the Backoffice.
- Reduces Errors: Avoids common pitfalls like expired tokens or incorrect headers.

{% hint style="info" %}
The **UMB_AUTH_CONTEXT** is only available in the Backoffice. For external applications, you will need to manage tokens manually or use an API user. Read more about API users in the [API Users article](../../../fundamentals/data/users/api-users.md).
{% endhint %}

## Management API Controllers

The Fetch API can also be used to make requests to the Management API controllers. The Management API is a set of RESTful APIs that allow you to interact with Umbraco programmatically. You can use the Fetch API to make requests to the Management API controllers like you would with any other API. The Management API controllers are located in the `/umbraco/api/management` namespace. You can use the Fetch API to make requests to these controllers like you would with any other API.
Expand Down