The Admin API allows your server to manage users via a GraphQL endpoint.
The Admin API allows your server to manage users via a GraphQL endpoint. You can list users, search users, view user details, and many more. In fact, the user management part of the portal is built with the Admin API.
The Admin API GraphQL endpoint is at /_api/admin/graphql
. For example, if your app is myapp
, then the endpoint is https://myapp.authgear.cloud/_api/admin/graphql
.
Accessing the Admin API GraphQL endpoint requires your server to generate a valid JWT and include it as Authorization
HTTP header.
See Authentication and Security to learn how to access the Admin API securely.
If you want to explore what the Admin API can do, you can visit the GraphiQL tool. The GraphQL schema can also be found there.
Go to Advanced -> Admin API
Click on the GraphiQL tool link
Toggle the schema documentation by pressing the Docs button in the top left corner.
The GraphiQL tool is NOT a sandbox environment and all changes will be made on real, live, production data. Use with care!
The Admin API is protected by cryptographic keys. Learn how to generate a valid JWT to authorize your request in this article.
Accessing the Admin API GraphQL endpoint requires your server to generate a valid JWT and include it as Authorization
HTTP header.
Go to Advanced -> Admin API
Click Download to download the private key. Make it available to your server.
Copy the key ID. Make it available to your server.
Here is the sample code of how to generate the JWT with the private key.
This example uses Express.js and the JsonWebToken package. So, first install both packages using the following commands:
and
Here's the code for the Express.js app that generates the JWT:
First, install the Firebase PHP JWT package using this command:
The PHP code for generating JWT:
After generating the JWT, you must include it in EVERY request you send to the Admin API endpoint. Here is how it looks like
The header is the standard Authorization HTTP header. The token type MUST be Bearer.
As you can see in the sample code, you expiration time of the JWT is 5 minutes. You make it last longer and cache it to avoid generating it on every request.
You should regularly change the API key used to authenticate API requests. It enhances security by minimizing the impact of compromised keys.
To rotate the API key
Go to Portal > Advanced > Admin API
Under "List of Admin API keys", click "Generate new key pair"
At this point both keys can be used to authenticate the admin API requests.
Make sure all your systems is updated to use the new key
Delete the old API key
The Authgear Admin API follows GraphQL Cursor Connections Specification to handle pagination of results.
Read more about the Connection model to understand the types like "Edge", "Node", and "Cursor" in GraphQL Cursor Connections Specification.
Queries and mutations are allowed in the Admin API
Download the latest schema here:
You can interactively access the same schema via the Explorer Doc sidebar
In this section of the Authgear documentation, you'll learn about all the GraphQL queries and mutations the Admin API supports.
Authgear provides a GraphQL API that you can use to manage users and other resources right from your application or using the GraphiQL Explorer in Authgear Portal > Advanced > Admin API.
The following section shows a detailed description and examples of supported queries and mutations.
The auditLogs query returns a list of all activities (logs) from the audit log.
Schema:
Example:
You can use this query to fetch all registered users on your application. The users
query returns a list of type User
.
The users
query depends on a search index. Hence, the data it returns is NOT immediately consistent as it requires reindexing before the most recent data and updates are included. If getting real-time user data is important for your use case, consider using the node
/nodes
or getUser
/getUsers
queries instead.
Schema:
Example:
A node represents a single object of different Types. The node query allows you to query a single object using the node ID. You learn more about node ID here: https://docs.authgear.com/reference/apis/admin-api/node-id.
Schema:
Example:
You can specify different object types to the node query to fetch an item of that type. Examples of node Types include User
, AuditLog
, Session
, Authenticator
, Authorization
, and Identity
.
The following example uses the AuditLog node type.
The nodes query returns a list of nodes. This works similarly to the node query except that instead of supplying a single ID, you can provide a list of IDs for the objects you are querying for.
Schema:
Example:
The groups query returns a list of all groups in an Authgear project. It will return nothing if no group has been created yet. Groups can be a field in the roles
query.
Schema:
Example:
You can use this query to get all the roles available in an Authgear project. The roles query will return nothing if no roles have been created for the Authgear project. Roles can also be a field in the node of the groups
query. See Manage Users Roles and Groups to learn more about roles and groups.
Schema:
Example:
The getUser
and getUsers
queries are a collection of Admin API queries for getting details about a single user or multiple users using specific attributes as the search key and in real-time. Unlike the users()
query, the result from the getUser and getUsers queries is immediately consistent.
Learn more about all the queries in this collection here.
With mutations, you can modify data from your application using the Admin API GraphQL. For example, you can use mutation to update
Calling this mutation will change a specific user account to an anonymous account. In other words, this query anonymizes a specific user. This action will delete the user's data like name and gender.
Schema:
Example:
The createIdentity mutation creates a new identity for a user.
Schema:
Example:
The createUser mutation makes it possible to create a new user account from the Admin API.
Schema:
Example:
This mutation deletes an authenticator for a specific user.
Schema:
Example:
You can use the deleteAuthorization mutation to delete an existing authorization for a user.
Schema:
Example:
The deleteIdentity mutation deletes the identity of a user.
Schema:
Example:
This mutation allows you to delete a specific user using the Admin API.
Schema:
Example:
Calling the generateOOBOTPCode mutation will generate a new OOB OTP Code for a user. This mutation allows you to specify the purpose and target of the OTP as input.
Schema:
Example:
The resetPassword mutation lets you rest a user's password from the Admin API.
Schema:
Example 1:
Example 2 (send new password to user):
You can include sendPassword: true
and setPasswordExpired: true
in the input for the resetPassword
mutation to send the new password to a user and set it as expired so they can set a new one the next time they log in. Here is an example of the mutation:
With the revokeAllSessions mutation, you can revoke all sessions for a specific user.
Schema:
Example:
This mutation revokes a specific user session. You can specify the session using the session ID.
Schema:
Example:
The scheduleAccountAnonymization mutation provides a means to schedule a user account anonymization from the Admin API.
Schema:
Example:
The scheduleAccountDeletion mutation provides a means to schedule a user account deletion from the Admin API.
Schema:
Example:
You can send a password reset message to a user from the Admin API using the sendResetPasswordMessage mutation.
Schema:
Example:
The setDisabledStatus mutation enables you to enable or disable a user's account.
Schema:
Example:
You can use the setVerifiedStatus mutation to set a user as verified and unveried from the Admin API.
Schema:
Example:
This mutation allows you to cancel a previously scheduled mutation.
Schema:
Example:
This mutation allows you to cancel a previously scheduled deletion.
Schema:
Example:
The updateIdentity mutation updates an existing identiy of a user.
Schema:
Example:
You can use this mutation to update an existing user's details. You can update standard attributes such as email and phone for the user. Or you can modify custom fields using the customAttributes
argument.
Schema:
Example 1 (Standard Attributes):
For this updateUser
example, we will be updating the standard attributes for a user. The first thing to do is to extract all the current values of the user's standard attributes into a variable. Then, add new fields or modify existing fields in the variable with new values.
Note: It is important to include the current values of the fields that you don't wish to update but still want to keep. The Admin API will delete any existing fields you omit in the variable.
The following block of code shows an example variable. If you're using GraphiQL, simply create the variable in the variable tab of GraphiQL like this:
Example 2 (Custom Attributes)
The following example shows how to update custom attributes.
Note: You must have created the custom attributes you wish to update in Authgear Portal > User Profile > Custom Attributes.
Create a variable and extract the current custom attributes into it. Modify the values of the attributes you wish to update or add new attributes.
Note: Again, it is important to include the current values of the fields that you don't wish to update but still want to keep. The Admin API will delete any existing fields you omit in the variable.
The following block of code shows an example of the variable. You can set the variable in the variable tab of GraphiQL.
Run this mutation to add a new access management group to your Authgear application.
Schema:
Example:
Note: The value of key
can not be empty, must be between 1 and 40 characters long, accepted characters are [a-zA-Z0-9:_]
and the prefix authgear:
is reserved for Authgear internal use.
You can use this mutation to add a new access management role to your Authgear application.
Schema:
Example:
Note: The value of key
can not be empty, must be between 1 and 40 characters long, accepted characters are [a-zA-Z0-9:_]
and the prefix authgear:
is reserved for Authgear internal use.
Use this mutation to add a role to one or more groups in a single operation.
Schema:
Example:
Adds a group to one or more roles in a single operation.
Schema:
Example:
Adds a user to one or more roles in a single operation.
Schema:
Example:
Adds a role to one or more users in a single operation.
Schema:
Example:
Adds a user to one or more groups in a single operation.
Schema:
Example:
Adds a group to one or more user in a single operation.
Schema:
Example:
Updates details about an existing role.
Schema:
Example:
Note: Pass null
as the value of key
, name
or description
if you do not wish to update them and pass and empty string (""
) to delete the value of name and description. Also, some GrahpQL libraries may not allow you to pass a literal null
directly in the query, in such cases, use a variable to defind the value of input
.
Updates details about an existing group.
Schema:
Example:
Note: Pass null
as the value of key
, name
or description
if you do not wish to update them and pass and empty string (""
) to delete the value of name and description. Also, some GrahpQL libraries may not allow you to pass a literal null
directly in the query, in such cases, use a variable to defind the value of input
.
Removes a user from one or more groups they're currently in.
Schema:
Example:
Removes a role from one or more groups.
Schema:
Example:
Removes a user from one or more roles.
Schema:
Example:
Removes a group from one or more roles in a single operation.
Schema:
Example:
Removes a role from one or more users in a single operation.
Schema:
Example:
Removes group from one or more users in a single operation.
Schema:
Example:
Use this mutation to delete an existing group.
Schema:
Example:
Use this mutation to delete an existing role.
Schema:
Example:
The node id
is a globally unique identifier of an object. It is needed when you call the GraphQL query and mutation. In this section, we will go through how to generate the node ID for calling the Admin GraphQL API.
The node id is a base64url encoded string with the format of <NODE_TYPE>:<ID>
. For example, in User:97b1c929-842c-415c-a7df-6967efdda160
, the node is "User" while "97b1c929-842c-415c-a7df-6967efdda160" is the ID for a specific user.
You can use your preferred programming language or tool to encode node IDs to base64url as shown below:
The query:
Query:
Query:
Query:
Query:
Query:
Overview and examples for the getUser/getUsers queries
The getUser and getUsers queries are a collection of Admin API queries for getting details about a single user or multiple users using specific attributes as the search key and in real-time.
Queries in the collection vary by the type of attribute they support as a search key. The queries are:
getUsersByStandardAttribute(attributeName: String!, attributeValue: String!)
getUserByLoginID(loginIDKey: String!, loginIDValue: String!)
getUserByOAuth(oauthProviderAlias: String!, oauthProviderUserID: String!)
The users
query is another type of query used to retrieve users. In the following section, we'll discuss the difference between the getUser/getUsers queries and the users
query.
Immediately Consistent: the getUser and getUsers queries search the database directly for users while the users
query depends on a search index. As a result, the users
query may not return details about a recently edited user, while getUser and getUsers queries are immediately consistent.
Exact Match: the getUser and getUsers queries only return a result that is an exact match. While calling the users
query may return users when there is a partial match of a user's email, phone number, or name.
Specific attribute: with the getUser and getUsers queries, you need to specify to retrieve the user by their email, username, or phone number. While the users
query uses a single searchKeyword
field.
The following section contains details and examples for each query in the getUser and getUsers queries:
The getUsersByStandardAttribute
query provides a way to retrieve users using a predefined standard attribute as the search key. The following are the standard attributes (attributeName
) that you can use:
email
preferred_username
phone_number
You can use the getUsersByStandardAttribute
query to get details for a user that registered with a loginID
or OAuth connection. For example, you can use the email
field in the standard attribute as a search key to find a user who linked an email address from an OAuth connection or registered using the email and password login method.
Inputs:
attributeName
: The name of the standard attribute that will be used as the search key. The value can only be: email
, preferred_username
, or phone_number
.
attributeValue
: The actual value for the user's standard attribute that you're using (attributeName
) as the search key. For example, the full email address of the user if you are using email
as the value for attributeName
.
Use the getUserByLoginID
query to retrieve details about a user using their loginID
as the search key. The following are the types of loginID
supported:
email
username
phone
Note: An email address linked to a user's account via social/enterprise login (OAuth) provider only can not be used as a search key in the getUserByLoginID query. Use getUsersByStandardAttribute instead to search for email addresses linked via OAuth provider only.
Inputs:
loginIDKey
: the value of this field should be the type of loginID associated with the user's account ( email
, phone
or username
).
loginIDValue
: enter the exact value of the identity associated with the user's account. For example, the user's full email address if their identity is email
. For phone
, use the full phone number including the "+" sign and country code, e.g. +441234567890.
The following example shows a getUserByLoginID
query and a response when a user is found.
The getUserByOAuth
query allows you to retrieve details about a user that linked an identity using a social/enterprise provider. For example, you can use the getUserByOAuth
query to search for a user that linked their account to Facebook using the User ID (sub) issued by Facebook OAuth provider as the search key.
Inputs:
oauthProviderAlias
: This is an identifier for each OAuth provider that can be set in Authgear portal via Authentication > Social / Enterprise Login. The default for value is google
, facebook
, github
, apple
for respective social login providers.
oauthProviderUserID
: The value for this field should be the sub
(User ID) returned by the OAuth provider. You can see the value of a user's oauthProviderUserID in their oauthConnections.claims
under the id
field. See the official documentation for each OAuth provider to learn more about their sub. This page shows an example of sub
from Facebook.
Learn how to perform common actions from the examples
In this section of the documentation, we've provided detailed guides showing examples of common user management actions you can perform using the Admin API. Click on any of the links below to view the corresponding guide:
Search for usersUpdate user's standard attributesUpdate user's pictureGenerate OTP codeLooking for more examples? See our full Admin API GraphQL queries and mutations examples page linked below:
Admin API ExamplesAuthgear Admin GraphQL API supports searching user by keywords. Keywords include email, phone number, username, SSO subject id and standard attributes.
You can use the user search query to search user by email and obtains the user details.
In the following example, the project wants to search the user by email and check if they haven't set up the TOTP authenticator.
You can adjust the fields in the query based on your needs. To explore more supporting fields, you can try it out via the GraphiQL tool.
The query has a maximum limit of 20, pagination parameters should be provided to obtain all the results. See detail.
searchKeyword
: Search for users by specified keyword.
first
: The number of items to be returned. Maximum is 20
.
after
: A cursor for use in pagination. You can pass the cursor value of the last edges item to a subsequent call to fetch the next page of results.
sortBy
: The field in which to order users. Supported values: CREATED_AT
or LAST_LOGIN_AT
.
sortDirection
: The direction in which to order users by the specified field. Supported values: ASC
or DESC
.
To update the user's standard attributes:
Fetch the user's original standard attributes by query first.
Change the attributes payload and update the user's standard attributes by mutation.
Follow the document here to generate the user node id.
The query:
The sample response:
Make sure to copy the original standard attributes as any attributes that are missing in the payload will be deleted. Next, change the values for attributes that you want to update.
The query:
The variables:
The sample variables:
The sample response:
To learn how to update a user's custom attributes, see the update custom attribute example.
Authgear allows your server to update the user's picture via the Admin APIs.
To update the user's picture:
Make a GET
request to /_api/admin/images/upload
to obtain the pre-signed upload url.
Make a POST
request to the pre-signed upload url to upload the image file. This call returns the result url once the upload is finished.
Call the Admin GraphQL API /_api/admin/graphql
to update the user's standard attributes. Update the picture
attribute with the result url returned by the previous call.
Make a GET
request to /_api/admin/images/upload
to obtain the pre-signed upload url.
The call will look like this:
The call returns the pre-signed upload url:
Upload the picture to the pre-signed upload url. We recommend you to pre-process the image before uploading. The image should be cropped into square and should be less than 10 MB.
Upload the file with FormData and the field name is file
. You can construct the multipart/form-data
request by most HTTP libraries.
The call will look like this:
The call returns the result url once the upload completed, the url is in format of authgearimages:///APP_ID/OBJECT_ID
:
Follow the doc of Update user's standard attributes to update the user's picture
attribute. Replace the attribute with the new picture url (e.g. authgearimages:///...
) obtained by the previous upload.