Understanding how to use the Management API

Hi there.

I’m struggling to get my head around the documentation on how to implement the Management API. I saw a community link at How do i delete a user from auth0? which was helpful. There seems to be two options:

  1. Using the Management API v2 Delete a user endpoint
  2. Using the Node Management API client(GitHub - auth0/node-auth0: Node.js client library for the Auth0 platform.)

For 1. above, is there more information and examples / tutorials on how to implement these? I assume some headers including tokens / secrets need to be included in requests.

For 2. above, is there somewhere in the documentation that refers to the Node Management API. I must of over looked it. I stumbled across this documentation as well https://node-auth0-docs.netlify.app/ - again, is this referred to in the core documentation somewhere?

I was able to implement the functionality I required after some trial and error / pot luck following reading the above community link. However, I feel like I can’t understand how to implement features of the Auth0 tool suite in a somewhat systematic way from looking at the documentation. Any help or guidance as to how to attack the documentation as it relates to the Management API or reference to other sources to help build up understanding of Auth0 more generally is greatly appreciated.

Hi @martinassaid

Welcome to the Auth0 Community.

In terms of the Management API. Using the explorer for me is the best way to get started and you’ll need an access token with permissions to query the API, see here for a testing scenario https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-testing

In a nutshell you can grab an access token by logging into your Auth0 Tenant and going to Applications > APIs > Auth0 Management API > “API Explorer” tab (the “Test” tab will also provide useful info in how to get an access token and how to use it).

Copy the provided token from the “API Explorer”. Then click on “Documentation” in the top right corner and select “Management API”. There’ll be a “SET API Token” button on the right hand side, click the button and paste the token in there.

You should now be able to click any of the endpoints from the left hand side and test them, also you can get the curl alternatives to execute on the command line/terminal.

In terms of the Node Management API Client, we have documentation for that here:
Getting started: GitHub - auth0/node-auth0 at 13b2008d2215b6020da76ea541120491f7776a03

Management API: ManagementClient | auth0

Examples: node-auth0/EXAMPLES.md at master · auth0/node-auth0 · GitHub

This should get you started. Let us know if you need anything further.

Warm regards

This is very helpful and has started me on a good path - many thanks @SaqibHussain. Will delve deeper in the new day and drop a note if further questions.

1 Like

Hello again @SaqibHussain. I’ve looked at this a bit further and have a couple of follow on questions I was hoping you could help with.

General Use of Management API
As a test, using a local NodeJS project, I tried running a delete request against a user. Config is as follows:

	const config = {
		method: "delete",
		maxBodyLength: Infinity,
		url: "https://lang-00-auth.au.auth0.com/api/v2/users/auth0%<user_id from dashboard>",
		headers: {
			"Authorization": `Bearer ${process.env.AUTH0_MANAGEMENT_API_TOKEN}`,
		},
	};

The user_id I’ve pulled from a user in the dashboard and just used the number identifier (e.g. 658a36d431ed5183faa679f6). I noticed when using the full user_id (e.g. auth0|658a36d431ed5183faa679f6) in the API Explorer that the user_id which gets populated in the config url string is transformed so I’m assuming this might be a reason why I’m getting an invalid uri error.

I’ve tried using the Management API Token from the Explorer and that seems to be sufficient in a testing environment.

Any ideas on why this request might be failing?

Node Management API Client
This is more of a general question - when do you use the Management API as against the Node Management API Client? … Or should I only be using the Node Management API Client in development?

My thanks again.

Hi @martinassaid

Thanks for getting back in touch.

If I was to delete a user using the Management API via node.js I would do something like the below in a node script and execute the script on the terminal:

import { ManagementClient } from 'auth0';

const management = new ManagementClient({
  domain: '<DOMAIN>',
  clientId: '<CLIENT-ID>',
  clientSecret: '<CLIENT-SECRET>',
});

await management.users.delete({ id: 'auth0|658acde2e4630df71a80c324' });

You can reference this doc for the API details https://auth0.github.io/node-auth0/classes/management.UsersManager.html#delete

It looks like you’re trying to hit the Management API via a HTTP client (like Axios) but if you have installed the auth0 package from NPM locally then you don’t need to take this approach. Just use the local package.

Let me know if you have further questions.

Warm regards

1 Like

If you do need to use the Management API over web then yes you would need to use a HTTP client of some sort, for delete user you would need to use the DELETE method against url like the below:

https://<domain>/api/v2/users/email%7C65858efa24db1b5e4bc13e86

The said access token is fine for testing.

Are you still having issues with this?

1 Like

Many thanks @SaqibHussain and apologies for not responding earlier.

I think I have the Management API working. I’ve tried using the Management API via node.js and I get an “Grant type ‘client_credentials’ not allowed for the client.” A quick look at the documentation and unfortunately it seems like it will be another process of wrapping my head around the documentation / forum responses. This might be an issue for another thread when I can look at it more closely.

The new issue aside, I am still unclear on when I would use the vanilla Management API as against Node Management API Client? If you have any thoughts / comments / links on the topic that would be great.

As always, my thanks for your considered responses.

Edit: regarding the error I encountered above, I found a community post here which suggested a machine-to-machine application needed to be established. Once I did this and updated the relevant environment variables the delete action via the Node Management API Client worked. I will take a look at why this is the case but for now it has advanced where I was at.

Hi @martinassaid

In terms of when you would use the Management API v node.js version. I think it depends on your use case. For example, if you have a custom API backend or regular web application say written in .NET for example and your want to provide users with the ability to modify their user_metadata properties then this may lend itself nicely to calling the Management API directly.

Another scenario could be, if you have a backend service that runs on a schedule via some task scheduler, this may require an application that can execute on the command line and in this case the node.js offering might lend itself nicely here.

There may also be instances where the node.js npm package could execute over web if you have a node/express application and the Management API could be executed by a console app so it could just be a question of best for the job or even preference for one over the other.

The bottom line is, both options are available and you choose whichever fits best your use case.

Warm regards.

1 Like

Perfect! Thanks @SaqibHussain - much appreciated.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.