Getting, Updating, and Deleting a User
Learn how to get a single user, update, and delete a user using Auth0 API.
In this lesson, we'll see three operations related to the users. First, we'll see how to retrieve details of a single user, then we'll update that user, and at the end, we will explore how we can delete it using the https://{{DOMAIN}}/api/v2/users/{id}
endpoint.
Getting a user
This endpoint allows us to retrieve a specific user when needed. It requires an access token with read:users
and read:user_idp_tokens
scopes. To utilize this endpoint, we need to send a GET
HTTP request to the users
endpoint.
Request parameters
We can invoke the endpoint with parameters to filter the results to retrieve a specific user. Some important parameters include:
Parameter Name | Type | Category | Description |
| String | Required | Defines the user ID that needs to be retrieved. |
| String | Optional | Filters the fields that will be included in the response. |
| Boolean | Optional | Used to confirm whether the filtered value should be included in the response. |
The following code filters a user, and we retrieve it through its user ID. Click the “Run” button to filter a user in the code widget below:
// Importing libraries hereconst fetch = require('node-fetch');const endpointUrl = new URL('https://{{DOMAIN}}/api/v2/users/{{USER_ID}}');const headerParameters = {'Content-Type': 'application/json','Authorization': 'Bearer {{ACCESS_TOKEN}}',}const options = {method: 'GET',headers: headerParameters,};async function getUser() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}getUser();
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 user ID we want to retrieve.
Line 18: We make a
GET
request using thefetch
function.Line 25: We invoke the
getUser
function.
Response fields
The successful execution of the above code will list the created user and returns metadata. Some of the important response fields are as follows.
Name | Description |
| Contains the user's ID. |
| Contains the user's email. |
| Contains the users enrolled in multifactor authentication. |
| Contains the user's last login details. |
| Contains the user's last login IP address. |
Updating a user
We can also update a user's information using the user update endpoint provided by Auth0. This can be done by sending a PATCH
HTTP request to the users
endpoint specified by Auth0 with some body parameters according to the changes we want to make.
Request parameters
This endpoint takes the user's ID and passes it as a path parameter. Since it is a PATCH
request, the body of the request is also required. Let's update some attributes of the client in the code widget below:
// Importing libraries hereconst fetch = require('node-fetch');const endpointUrl = new URL('https://{{DOMAIN}}/api/v2/users/{{USER_ID}}');const headerParameters = {'Content-Type': 'application/json','Authorization': 'Bearer {{ACCESS_TOKEN}}',}const bodyParameters = JSON.stringify({"username": "UpdatedUsername",});const options = {method: 'PATCH',headers: headerParameters,body: bodyParameters,};async function updateUser() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}updateUser();
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–13: We define a
bodyParameters
object and update the user'susername
.Line 23: We make a
PATCH
request using thefetch
function.Line 30: We invoke the
updateUser
function.
Response fields
The successful execution of the above code returns the user's metadata with the updated username
field values.
Deleting a user
We can delete this user by changing the HTTP method from PATCH
to DELETE
at line 16 and by removing bodyParameters
at line 18 in the code widget above.