Docs

Get Management API Tokens for Single-Page Applications

In certain cases, you may want to use Auth0's Management API to manage your applications and APIs rather than the Auth0 Management Dashboard.

To call any of the endpoints in the Management API, you must authenticate using a specialized OpenID Connect (OIDC) protocolAccess Token called the Management API Token. Management API Tokens are JSON Web Tokens (JWTs) that contain specific granted permissions (also known as scopes) for the Management API endpoints you want to call.

Ways to use scopes

Limitations

Because single-page applications (SPAs) are public clients and therefore cannot securely store sensitive information (such as a Client Secret), they must retrieve Management API Tokens from the frontend, unlike other application types.

This means that Management API Tokens for SPAs have special limitations. Specifically, they will be issued in the context of the user who is currently signed in to Auth0, which limits updates to only the logged-in user's data. Although this restricts use of the Management API, it can still be used to perform actions related to updating the logged-in user's user profile.

Best practices

Available scopes and endpoints

With a Management API Token issued for a SPA, you can access the following scopes (and hence endpoints):

Scope for current user Endpoint
read:current_user GET /api/v2/users/{id}
GET /api/v2/users/{id}/enrollments
update:current_user_identities POST/api/v2/users/{id}/identities
DELETE /api/v2/users/{id}/identities/{provider}/{user_id}
update:current_user_metadata PATCH /api/v2/users/{id}
create:current_user_metadata PATCH /api/v2/users/{id}
delete:current_user_metadata DELETE /api/v2/users/{id}/multifactor/{provider}
create:current_user_device_credentials POST /api/v2/device-credentials
delete:current_user_device_credentials DELETE /api/v2/device-credentials/{id}

Requested scopes versus granted scopes

Using a Management API Token to call the Management API from a SPA

In this example, we will retrieve a Management API Token from a SPA and use this token to call the Auth0 Management API to retrieve the full user profile of the currently logged-in user.

Keep reading

1. Retrieve a Management API Token

Authenticate the user (using the Implicit grant) by redirecting them to the Authorization endpoint, which is where users are directed upon login or sign-up:

If you are not familiar with authentication for SPAs, see Implicit Flow.

Notice:

  • The audience is set to https://YOUR_DOMAIN/api/v2/. By default, an API representing the Auth0 Management API is registered for you when you create your tenant in the Auth0 Dashboard. The audience value represents the registered Management API's URI for your tenant.
  • The response_type is id_token token (indicating that we want to receive both an ID Token as well as an Access Token, which represents the Management API Token).
  • The requested scope is read:current_user. This scope will allow us to call either of the two endpoints listed in the table above for this scope.

Response

When we receive our Management API Token, it will be in JSON Web Token format. Decoding it and reviewing its contents will reveal the following:

Notice:

  • The aud is set to the audience you provided when authenticating (your tenant's Management API URI).
  • The granted scope is what you requested when you authenticated: read:current_user.
  • The sub is the user ID of the currently logged-in user.

2. Call the Auth0 Management API

Call the Auth0 Management API to retrieve the logged-in user's user profile from the Get User by ID endpoint. We can call this endpoint because we requested and were granted the proper scope (read:current_user) during authentication.

To call the endpoint, include the encoded Management API Token you retrieved in the Authorization header of the request. Be sure to replace the USER_ID and MGMT_API_ACCESS_TOKEN placeholder values with the logged-in user's user ID (sub value from the decoded Management API Token) and the Management API Access Token, respectively.




Value Description
USER_ID ID of the user for which you want to retrieve the user profile. Because of the limitations placed on Management API Tokens for SPAs, this should be the user ID for the logged-in user, which can be found in the sub claim of the decoded Management API Token.
MGMT_API_ACCESS_TOKEN Access Token for the Management API with the scope read:current_user.

Keep reading