Creating and Getting All Users

Learn to create users and get all user's using Auth0 API.

In Auth0, users are individuals who have been granted access to a specific application or set of applications through the use of authentication and authorization. Users can be authenticated through a variety of methods. Once authenticated, users are granted access to specific resources and permissions within the application by its administrator, who can update their information, assign them to different roles, or revoke their access as needed. In this lesson, we'll see how we can create a user or get a list of our users using an API call. We'll use the https://{{DOMAIN}}/api/v2/users endpoint to achieve these tasks. Creating a user is a POST request while getting all users is a GET request.

Press + to interact
Creating a user and getting all users endpoints
Creating a user and getting all users endpoints

Creating a user

Auth0 is a cloud-based identity management platform that allows developers to securely authenticate users and manage user access to applications. To create a new user in Auth0, we need to set up a connection, define user attributes, and configure a password.

Request parameters

To invoke this endpoint, we will use a POST request. So, we have a list of parameters that can be passed as body parameters. This endpoint takes a very extensive list of parameters. Let’s have a look at some important ones in the table below:

Parameter Name

Type

Category

Description

connection

String

Required

Defines the connection that will be used to create a user.

email

String

Optional

Defines the user's email.

email_verified

Boolean

Optional

Specifies whether the user's email address is verified or not. If true, the user will not receive a verification email.

blocked

Boolean

Optional

Specifies whether an administrator has blocked a user.

name

String

Optional

Defines the user's full name.

user_id

String

Optional

Defines the user's identifier.

username

String

Optional

Defines the user's username.

password

String

Optional

Defines the user's password.

The following code creates a user and returns the user ID. Please click the “Save” button to use that ID in the upcoming lessons. Let's create a user in the code widget below:

Press + to interact
// Importing libraries here
const fetch = require('node-fetch');
const endpointUrl = new URL('https://{{DOMAIN}}/api/v2/users');
let randomId = (Math.random() + 1).toString(36).substring(7)
const headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{ACCESS_TOKEN}}',
}
const bodyParameters = JSON.stringify({
"email": "sample.user@example.com",
"email_verified": true,
"name": "Sample User",
"user_id": randomId,
"connection": "{{CONNECTION_NAME}}",
"password": "testing@123",
"verify_email": false,
"username":"Sample-User"
});
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
async function createUser() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
createUser();

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

  • Line 4: We define the endpoint URL to create a user.

  • Lines 6–9: We define the headerParameters object, which tells the API call about the type of data we are sending and the access token.

  • Lines 11–20: We define the bodyParameters object.

    • Line 12: We define the email address of our user.

    • Line 13: We define whether an email is verified or not.

    • Line 15: We define a random ID for the user using the randomId variable we created on line 5.

    • Line 16: We define the connection that will be used for the user.

    • Line 17: We define the password for the user.

    • Line 19: We define the user's username.

  • Lines 22–26: We define the options object used to pass data required to make an API call.

  • Line 37: We invoke the createUser function.

Response fields

The successful execution of the above code creates a new user and returns its details. Some important response fields are as follows:

Name

Description

user_id

Contains the user ID that we created.

created_at

Contains the timestamp of user creation.

updated_at

Contains the timestamp of user updation.

logins_count

Contains the login count of the user.

Getting all users

This endpoint allows us to retrieve all available users when needed. It requires an access token with the read:users and read:user_idp_tokens scopes.

Request parameters

Let's learn how we can use this endpoint. To retrieve all the users, we’ll need to send a GET HTTP request to the specified endpoint. We can also filter the results by using certain parameters. Some of the important parameters are as follows:

Parameter Name

Type

Category

Description

per_page

Integer

Optional

Defines the number of results per page that will be reflected in the response.

fields

String

Optional

Filters the fields that will be included in the response.

include_fields

Boolean

Optional

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

include_totals

Boolean

Optional

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

sort

String

Optional

Used to sort the fields included in the response.

connection

String

Optional

Used to filter users of a specific connection.

q

String

Optional

Used to filter users with the help of a query.

The following code retrieves all the users that we have. Click the “Run” button to retrieve all users in the code widget below:

Press + to interact
// Importing libraries here
const fetch = require('node-fetch');
const endpointUrl = new URL('https://{{DOMAIN}}/api/v2/users');
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{ACCESS_TOKEN}}',
}
// Define Query Parameters here
const queryParameters = new URLSearchParams({
"per_page": 5,
"include_totals":true
});
const options = {
method: 'GET',
headers: headerParameters,
};
async function getAllUsers() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
getAllUsers();

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

  • Line 4: We define the endpoint URL to filter all the users.

  • Lines 12–15: We define queryParameters that apply the filter on the response.

    • Line 13: We define the number of results per page results that will be reflected in the response.

    • Line 14: We set the value of the include_totals parameter to true.

  • Line 24: We assign the query parameters defined in queryParameters to the endpointUrl.

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

  • Line 32: We invoke the getAllUsers function.

Response fields

The successful execution of the above code will list all the users and return metadata that the important response field follows.

Name

Description

user_id

Contains the user's ID.

email

Contains the user's email.

multifactor

Contains the users enrolled in multifactor authentication.

last_login

Contains the user's last login details.

last_ip

Contains the user's last login IP address.