Import Users using User Import API
Use the user import API to bulk import users from external systems to your Authgear project
Some ways to add users to your Authgear project include; using the Add User UI in Authgear Portal, using the createUser() mutation in Admin API, and last but not least, having the user accounts created using sign-up page on AuthUI.
A common downside of all the above-listed methods is that they do not support batch import of users. Meaning, that you have to add users one by one. This isn't ideal for importing multiple users from existing legacy systems to Authgear. For adding bulk users, there is the User Import API.
In this post, you'll learn what the User Import API is and see examples of how to import bulk users to an Authgear project.
User Import API
The User Import API is an API that supports the bulk import of users from another system to an Authgear project. This API is not part of the Admin API GraphQL. However, the API endpoints require the Admin API JWT token to access it.
The following are other important things to note about the User Import API:
The actual process of importing the users is asynchronous. This means execution is done in the background. The API provides an endpoint developers can use to query the status of the import.
Once an import is initiated successfully, the API will return an ID for the task. This ID is required to query the status of the import.
The body of HTTP requests to the API has a limit of 500KB.
Using the User Import API does not trigger the
user.pre_create
anduser.created
hooks.The API supports the Bcrypt password format. To import passwords using this format specify the format
type
andpassword_hash
in an object that will be the value of the user'spassword
field. For example:
Endpoints
The Import User API has two endpoints, one for initiating a user import task and the other for checking the status of the task. The endpoints only support secure HTTPS request and require a valid Admin API JWT token using Bearer
authorization header (Authorization: Bearer <Admin API JWT Token>
).
Here are more details about the endpoints and their expected inputs.
Initiate Import
Check Status
Input Format
The endpoint that initiates an import accepts JSON input via an HTTP request body. The following sample JSON document shows the expected structure and fields of the input:
To understand the input better, let's take a close look at the three fields (upsert
, identifier
, records
) that are directly on the root of the above JSON document.
upsert
: This is an optional boolean that isfalse
by default. When the value for this field is set totrue
and a user already exists with the same identity, the user's data is updated based on the update behavior for each attribute.identifier
: This field is required. It tells Authgear what attribute to use to identify an existing user. The following strings are the accepted values:preferred_username
,email
, andphone_number
.records
: This is where a developer can provide the data of all the users they wish to import in an array. Each direct object in the array represents a single user. Within the object, you can define the standard attributes for the user using the various fields as shown in the sample above. You may also define custom attributes in an object nested inside thecustom_attributes
field as also shown above.
Update Behavior
The update behavior for an attribute determines how Authgear will treat that attribute when an existing user has the same value for the specified identifier
type. For example, if the identifier
is "email", the update behavior for each attribute is how Authgear will treat the attribute if a user already exists with the same email address as the current user you're trying to import.
Each attribute can have one of the three different types of update behavior described below:
UPDATED_IF_PRESENT_AND_REMOVED_IF_NULL: An attribute with this update behavior will update the user's attribute to the new value if that new value is not null. If the new value is explicitly null, the attribute will be deleted for the user. And if the attribute is absent, no operation is done.
UPDATED_IF_PRESENT: When this is the update behavior of an attribute, it will be updated if it is present. If the attribute is not present, no operation is done.
IGNORED: If a user exists already, the new value of this attribute is ignored. If the attribute is absent, nothing is done.
Update Behavior of each field
The following table shows all attributes and their update behavior for reference purposes:
Item | Update Behavior | Description |
---|---|---|
| UPDATED_IF_PRESENT_AND_REMOVED_IF_NULL | If it is not |
| UPDATED_IF_PRESENT | For example, in the first import, if |
All other standard attributes | UPDATED_IF_PRESENT_AND_REMOVED_IF_NULL | In particular, |
| UPDATED_IF_PRESENT_AND_REMOVED_IF_NULL | For each attribute in |
| UPDATED_IF_PRESENT | If present, the roles and groups of the user will match the value. For example, supposed the user originally has |
| UPDATED_IF_PRESENT | Re-importing a record without specifying |
| IGNORED | If it was not provided when the record was first imported, subsequent import CANNOT add it back. |
| UPDATED_IF_PRESENT_AND_REMOVED_IF_NULL | If provided, the user can perform 2FA with email OTP. |
| UPDATED_IF_PRESENT_AND_REMOVED_IF_NULL | If provided, the user can perform 2FA with phone OTP. |
| IGNORED | If it was not provided when the record was first imported, subsequent import CANNOT add it back. |
| IGNORED | If it was not provided when the record was first imported, subsequent import CANNOT add it back. |
Usage Example
In this section, you can find code for a simple example of using the User Import API to add multiple users to an Authgear project.
Pre-requisites
To follow this example and be able to run the code on your local machine, you must have the following:
An Authgear account. Sign up for free here.
Node.js installation on your local computer.
Install Express.js by running the following command from your project directory:
npm install express
.
Step 1: Get Admin API JWT
As mentioned earlier in this post, the User Import API requires the Admin API JWT to access.
First, install JsonWebToken (a Node package for generating JWT) by running the following command:
The following code shows how to get the token:
See our post on Admin API Authentication for a more detailed guide on how to get your key ID, and private key and generate Admin API JWT.
Step 2: Import Users from a JSON Document
In the following steps, we'll use the node-fetch package to make HTTP requests to the User Import API. Hence, install node-fetch by running the following command:
The following code sample demonstrates how to import 2 users from a JSON document that's stored in a simple constant (const data
):
If the user import was initiated successfully, you'll get a response that looks like this:
In the next step, we'll use the value of the id
field from the above response to query the status of the import task.
Step 3: Get the Status of the Import Task
Add a new route to the Express app that accepts the task id
as a parameter and uses that id to query the status of the task. Here's the code for the route:
The response to the request to query the status of the import task will look like this:
From the response, you can see the status
of the entire task (import was completed
), including a summary ( { "total": 2, "inserted": 2, "updated": 0, "skipped": 0, "failed": 0 }
).
The details
field contains an array of details such as the outcome
for each user in the original JSON document.
Last updated