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, a valid Admin API JWT token is required to access the endpoints of the User Import API.
Endpoints
The following are the endpoints for the User Import API.
Initiate Import
Use this endpoint to start a new user import task.
Check Status
Use this endpoint to verify the status of an existing user import task.
To learn more detailed information about the User Import API (such as endpoints, inputs, and responses), see the User Import API Reference.
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. In this step, we'll generate an Admin API JWT using Node.js.
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 (and examples for other programming languages/frameworks) on how to get your key ID, and private key and generate Admin API JWT.
Step 2: Import Users from a JSON Document
To import users to your Authgear project using the User Import API, make a POST HTTP(S) request to the Initiate Import endpoint (/_api/admin/users/import
).
The user data you wish to import must be provided as a JSON input in the HTTP(S) request body as specified in the API input format.
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
In this step, we'll make a GET HTTP(S) request to the Check Status endpoint (/_api/admin/users/import/{ID}
) to get the status of the user import task we initiated in the last step. You'll need to replace {ID}
in the URL with the value of id
in the previous response.
To do this, 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