...
/Getting, Updating, and Deleting a Connection
Getting, Updating, and Deleting a Connection
Learn how to get a single connection, update a connection, and delete a connection using Auth0 API.
We'll cover the following...
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.
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 |
| String | Required | Defines the connection ID that needs to be retrieved. |
| Boolean | Optional | Used to confirm whether the filtered value should be included in the response or not. |
| String | Optional | Used to confirm whether |
The following code retrieves a connection through its connection ID. Click the “Run” button to retrieve a connection in the code widget below:
// Importing libraries hereconst 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
GETrequest using thefetchfunction.Line 25: We invoke the
getConnectionfunction.
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 |
| Contains the connection name listed. |
| Contains the connection display name. |
| Contains the ID of the connection. |
| Contains the identifier for the connection. |
| Contains the realms for the connection. |
| 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 |
| String | Required | Defines the identifier for a connection. |
| String | Optional | Defines the password policy for a connection. |
| String | Optional | Defines the display name of the connection. |
| Array | Optional | Defines the client ID of the client, which is enabled by the connection. |
| Array | Optional | Defines the realms which can use this connection. |
| 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.
// Importing libraries hereconst 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
bodyParametersobject and update the display name of the connection.Line 23: We make a
PATCHrequest using thefetchfunction.Line 30: We invoke the
updateConnectionfunction.
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.