Getting, Updating, and Deleting a Connection

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

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

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

Getting a connection

This endpoint allows us to retrieve a specific connection when needed. It requires an access token with read:connections scope, which will help us in retrieving some specific connection details.

Request parameters

Let's see how we can use this endpoint. To retrieve a specific connection, we have to send a GET HTTP request to the specified endpoint. We can retrieve the results by using certain parameters. Some of the important parameters are as follows:

Parameter Name

Type

Category

Description

id

String

Required

Defines the connection ID that needs to be retrieved.

include_fields

Boolean

Optional

Used to confirm whether the filtered value should be included in the response or not.

fields

String

Optional

Used to confirm whether fields should be included in the response or not.

The following code retrieves a connection through its connection ID. Click the “Run” button to retrieve a connection in the code widget below:

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

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 connection ID we want to extract.

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

  • Line 25: We invoke the getConnection function.

Response fields

The successful execution of the above code will list the connections and return the specified connections' metadata. Some of the important response fields follow.

Name

Description

name

Contains the connection name listed.

display_name

Contains the connection display name.

id

Contains the ID of the connection.

strategy

Contains the identifier for the connection.

realms

Contains the realms for the connection.

enabled_clients

Contains the enabled clients for the connection.

Updating a connection

We can also update the connection according to our requirements. In that case, Auth0 provides a connections endpoint that allows us to update the connection by simply sending a PATCH API call.

Request parameters

This endpoint requires the connection's ID to be passed as a path parameter. Since it is a PATCH request, the body of the request is also required, with the following important parameters.

Parameter Name

Type

Category

Description

id

String

Required

Defines the identifier for a connection.

passwordPolicy

String

Optional

Defines the password policy for a connection.

display_name

String

Optional

Defines the display name of the connection.

enabled_clients

Array

Optional

Defines the client ID of the client, which is enabled by the connection.

realms

Array

Optional

Defines the realms which can use this connection.

password_complexity_options

String

Optional

Defines the password complexity options.

Let's update the connection name of the connection we created in our previous lesson. Click the “Run” button to update the connection name.

Press + to interact
// Importing libraries here
const fetch = require('node-fetch');
const endpointUrl = new URL('https://{{DOMAIN}}/api/v2/connections/{{CONNECTION_ID}}');
const headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{ACCESS_TOKEN}}',
}
const bodyParameters = JSON.stringify({
"display_name": "Updated Sample Name"
});
const options = {
method: 'PATCH',
headers: headerParameters,
body: bodyParameters,
};
async function updateConnection() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
updateConnection();

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 display name of the connection.

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

  • Line 30: We invoke the updateConnection function.

Response fields

The successful execution of the above code returns the connection's metadata with the updated values.

Deleting a connection

We can delete this connection by changing the HTTP method from PATCH to DELETE at line 16 and removing bodyParameters at line 18 in the code widget above.