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