Getting, Updating, and Deleting a Client

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

In this lesson, we'll see three operations related to the clients. First, we'll see how to retrieve the details of a single client, and then we'll update and delete it using the https://{{DOMAIN}}/api/v2/clients/{id} endpoint.

Press + to interact
Getting, updating, and deleting a client
Getting, updating, and deleting a client

Getting a client

To retrieve the client’s information, which we created earlier, this endpoint requires the same permissions scopes for the list clients’ operation.

Request parameters

This endpoint takes only one required parameter, client's ID, which is passed as a path parameter. It also supports two optional query parameters, fields and include_fields. We can use these to filter the response fields of the client, and we have already discussed these parameters previously. Let's see the code in the widget below:

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

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

  • Line 4: We define the URL for this API call and pass the client ID as a path parameter.

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

  • Line 25: We invoke the getClient function.

Response fields

The successful execution of the above code returns the same fields, as returned by the list clients operation.

Updating a client

After creating a client, we can update its parameters using the PATCH method on the clients endpoint.

Request parameters

This endpoint requires the client's ID as a path parameter. Additionally, we pass the parameters we wish to update in body parameters, which are the same as we have seen while creating a client. Let's update some attributes of the client in the code widget below:

Press + to interact
// Importing libraries here
const fetch = require('node-fetch');
const endpointUrl = new URL('https://{{DOMAIN}}/api/v2/clients/{{NEW_CLIENT_ID}}');
const headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{ACCESS_TOKEN}}',
}
const bodyParameters = JSON.stringify({
"name": "Updated Sample Client",
"description": "A sample client updated by an API call",
});
const options = {
method: 'PATCH',
headers: headerParameters,
body: bodyParameters,
};
async function updateClient() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
updateClient();

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

  • Line 4: We define the endpoint URL for the API call and pass the client ID as a path parameter.

  • 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 updateClient function.

Response fields

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

Deleting a client

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

Note: If we delete a client, we must create another one to be able to use it in the upcoming lessons. To create a client, please go back to the previous lesson and create the client again.