Getting, Updating, and Deleting a Role

Learn how to get a single role, update a role and delete a role using Auth0 API.

In this lesson, we'll see three operations related to the roles. First, we'll see how to retrieve the details of a single role, then we'll update that role, and at the end, we will explore how we can delete it using the https://{{DOMAIN}}/api/v2/roles/{id}  endpoint.

Press + to interact
Getting, updating, and deleting the role
Getting, updating, and deleting the role

Getting a role

This endpoint allows us to retrieve some specific role when needed. It requires an access token with read:roles scope. To utilize this endpoint, we need to send a GET HTTP request to the roles endpoint.

Request parameters

To retrieve a specific role, we can send a GET HTTP request to the endpoint with the role IDs to filter the results.

Press + to interact
// Importing libraries here
const fetch = require('node-fetch');
const endpointUrl = new URL('https://{{DOMAIN}}/api/v2/roles/{{ROLE_ID}}');
const headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{ACCESS_TOKEN}}',
}
const options = {
method: 'GET',
headers: headerParameters,
};
async function getRole() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
getRole();

Let's look at the highlighted lines from the code shown above:

  • Line 4: We define the endpoint URL for this API call and pass the role ID we want to list.

  • Line 18: We make a GET request using the fetch function.

  • Line 25: We invoke the getRole function.

Response fields

The successful execution of the above code will list the specified role and return metadata. The important response fields are as follows:

Name

Description

id

Contains the ID of the role.

name

Contains the role name.

description

Contains the role description.

Updating a role

We can also update a role’s information using the roles endpoint provided by Auth0. This can be done by sending a PATCH HTTP request to the roles endpoint specified by Auth0.

Request parameters

Since it is a PATCH request, the body of the request is also required. Let's update some attributes of the role in the code widget below:

Press + to interact
// Importing libraries here
const fetch = require('node-fetch');
const endpointUrl = new URL('https://{{DOMAIN}}/api/v2/roles/{{ROLE_ID}}');
const headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{ACCESS_TOKEN}}',
}
const bodyParameters = JSON.stringify({
"name": "Admin",
"description": "Updated role description for the admin"
});
const options = {
method: 'PATCH',
headers: headerParameters,
body: bodyParameters,
};
async function updateRoles() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
updateRoles();

Let's look at the highlighted lines from the code shown above:

  • Line 4: We define the endpoint URL for the API call.

  • Lines 11–14: We define a bodyParameters object and update the values of the name and description fields.

  • Line 24: We make a PATCH request using the fetch function.

  • Line 31: We invoke the updateRoles function.

Response fields

The successful execution of the above code returns the metadata of the role with the updated values of the name and description fields.

Deleting a role

We can delete this role by changing the HTTP method from PATCH to DELETE at line 17 and by removing bodyParameters at line 19 in the code widget above.