|
| 1 | +--- |
| 2 | +description: How external applications can consume the Management API. |
| 3 | +--- |
| 4 | + |
| 5 | +# External access to the Management API |
| 6 | + |
| 7 | +The Management API can be used directly for integrations between Umbraco and external systems. |
| 8 | + |
| 9 | +When consuming the Management API from an external source, we must use the OpenId Connect Client Credentials flow for authorization. For details on setting up Client Credentials, please refer fo the [API Users](../../fundamentals/data/users/api-users.md) article. |
| 10 | + |
| 11 | +With a set of Client Credentials in place, we can obtain an access token from the Management API token endpoint: `/umbraco/management/api/v1/security/back-office/token`. The token endpoint response looks like this: |
| 12 | + |
| 13 | +```json |
| 14 | +{ |
| 15 | + "access_token": "ZnEAKg5YwDc7621y6xZlEdT9kwp_ULGQPc5mnY9cDw0", |
| 16 | + "token_type": "Bearer", |
| 17 | + "expires_in": 299 |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +As shown, the access token should be used as a Bearer token when consuming the Management API. |
| 22 | + |
| 23 | +Also notice that access tokens have a fixed expiry. While we can keep issuing new tokens for our Client Credentials, please reuse tokens within their lifespan. This will be more performant, and will also keep from flooding the Umbraco database with tokens. |
| 24 | + |
| 25 | +{% hint style="info" %} |
| 26 | +Please notice that the Management API does not support OIDC Discovery. This is reserved for accessing protected for Members via the [Delivery API](../content-delivery-api/protected-content-in-the-delivery-api.md). |
| 27 | +{% endhint %} |
| 28 | + |
| 29 | +The following code sample demonstrates how to consume the Management API by: |
| 30 | + |
| 31 | +1. Obtaining an access token from the token endpoint. |
| 32 | +2. Fetching data from the "current user" endpoint. |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +{% hint style="info" %} |
| 37 | +This sample requires the [`IdentityModel`](https://www.nuget.org/packages/IdentityModel) NuGet package to run. |
| 38 | +{% endhint %} |
| 39 | + |
| 40 | +{% code title="Program.cs" lineNumbers="true" %} |
| 41 | +```csharp |
| 42 | +using System.Net.Http.Json; |
| 43 | +using IdentityModel.Client; |
| 44 | + |
| 45 | +// the base URL of the Umbraco site - change this to fit your own setup |
| 46 | +const string host = "https://localhost:44391"; |
| 47 | + |
| 48 | +var client = new HttpClient(); |
| 49 | + |
| 50 | +// request a client credentials token from the Management API token endpoint |
| 51 | +var tokenResponse = await client.RequestClientCredentialsTokenAsync( |
| 52 | + new ClientCredentialsTokenRequest |
| 53 | + { |
| 54 | + Address = $"{host}/umbraco/management/api/v1/security/back-office/token", |
| 55 | + ClientId = "umbraco-back-office-my-client", |
| 56 | + ClientSecret = "my-client-secret" |
| 57 | + } |
| 58 | +); |
| 59 | + |
| 60 | +if (tokenResponse.IsError || tokenResponse.AccessToken is null) |
| 61 | +{ |
| 62 | + Console.WriteLine($"Error obtaining a token: {tokenResponse.ErrorDescription}"); |
| 63 | + return; |
| 64 | +} |
| 65 | + |
| 66 | +// use the access token as Bearer token |
| 67 | +client.SetBearerToken(tokenResponse.AccessToken); |
| 68 | + |
| 69 | +// fetch user data from the "current user" Management API endpoint |
| 70 | +var apiResponse = await client.GetAsync($"{host}/umbraco/management/api/v1/user/current"); |
| 71 | +var apiUserResponse = await apiResponse |
| 72 | + .EnsureSuccessStatusCode() |
| 73 | + .Content |
| 74 | + .ReadFromJsonAsync<ApiUserResponse>(); |
| 75 | + |
| 76 | +if (apiUserResponse is null) |
| 77 | +{ |
| 78 | + Console.WriteLine("Could not parse a user from the API response."); |
| 79 | + return; |
| 80 | +} |
| 81 | + |
| 82 | +Console.WriteLine($"Hello, {apiUserResponse.Name} ({apiUserResponse.Email})"); |
| 83 | + |
| 84 | +public class ApiUserResponse |
| 85 | +{ |
| 86 | + public required Guid Id { get; set; } |
| 87 | + |
| 88 | + public required string Name { get; set; } |
| 89 | + |
| 90 | + public required string Email { get; set; } |
| 91 | +} |
| 92 | +``` |
| 93 | +{% endcode %} |
0 commit comments