Admin APIs
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

The Admin API GraphQL endpoint is at /_api/admin/graphql. For example, if your app is myapp , then the endpoint is https://myapp.authgearapps.com/_api/admin/graphql .

Accessing the Admin API GraphQL endpoint

Accessing the Admin API GraphQL endpoint requires your server to generate a valid JWT and include it as Authorization HTTP header.

Obtaining the private key for signing JWT

    Go to Settings -> 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.

Generating the JWT with the private key

Here is the sample code of how to generate the JWT with the private key.
Go
1
package main
2
3
import (
4
"encoding/json"
5
"fmt"
6
"os"
7
"time"
8
9
"github.com/lestrrat-go/jwx/jwa"
10
"github.com/lestrrat-go/jwx/jwk"
11
"github.com/lestrrat-go/jwx/jws"
12
"github.com/lestrrat-go/jwx/jwt"
13
)
14
15
// Replace "myapp" with your app ID here.
16
const AppID = "myapp"
17
18
// Replace "mykid" with the key ID you see in the portal.
19
const KeyID = "mykid"
20
21
func main() {
22
// Replace the following call with your own way to get the private key.
23
f, err := os.Open("private-key.pem")
24
if err != nil {
25
panic(err)
26
}
27
defer f.Close()
28
jwkSet, err := jwk.ParseReader(f, jwk.WithPEM(true))
29
if err != nil {
30
panic(err)
31
}
32
key, _ := jwkSet.Get(0)
33
34
now := time.Now().UTC()
35
payload := jwt.New()
36
_ = payload.Set(jwt.AudienceKey, AppID)
37
_ = payload.Set(jwt.IssuedAtKey, now.Unix())
38
_ = payload.Set(jwt.ExpirationKey, now.Add(5*time.Minute).Unix())
39
40
// The alg MUST be RS256.
41
alg := jwa.RS256
42
hdr := jws.NewHeaders()
43
hdr.Set("typ", "JWT")
44
hdr.Set("alg", alg.String())
45
hdr.Set("kid", KeyID)
46
47
buf, err := json.Marshal(payload)
48
if err != nil {
49
panic(err)
50
}
51
52
token, err := jws.Sign(buf, alg, key, jws.WithHeaders(hdr))
53
if err != nil {
54
panic(err)
55
}
56
57
fmt.Printf("%v\n", string(token))
58
}
Copied!

Including the JWT in the HTTP request

After generating the JWT, you must include it in EVERY request you send to the Admin API endpoint. Here is how it looks like
1
Authorization: Bearer <JWT>
Copied!
The header is the standard Authorization HTTP header. The token type MUST be Bearer.

Optional: Caching the JWT

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.

Trying out the Admin API GraphQL endpoint

The GraphiQL tool is NOT a sandbox environment and all changes will be made on real data. Use with care!
The above instruction is for server-side integration. If you want to explore what it can do, you can visit the GraphiQL tool.
    Go to Settings -> Admin API
    Click on the GraphiQL tool link

Inspecting the GraphQL schema

In the GraphiQL tool, you can toggle the schema documentation by pressing the Docs button in the top right corner.
Last modified 2mo ago