User Pagination and Count
important
This is applicable for supertokens core version >= 3.5. For older core versions, please visit your backend SDK's reference docs.
This feature allows you to loop through (on your backend) all the users in your app. It also allows you to get the number of users.
#
Loop through users in your app#
Newest First- NodeJS
- GoLang
- Python
- Other Frameworks
Important
import { getUsersNewestFirst } from "supertokens-node";
async function getUsers() {
// get the latest 100 users
let usersResponse = await getUsersNewestFirst({
tenantId: "public"
});
let users = usersResponse.users;
let nextPaginationToken = usersResponse.nextPaginationToken;
// get the next 200 users
usersResponse = await getUsersNewestFirst({
tenantId: "public",
limit: 200,
paginationToken: nextPaginationToken,
})
users = usersResponse.users;
nextPaginationToken = usersResponse.nextPaginationToken;
// get for specific recipes
usersResponse = await getUsersNewestFirst({
tenantId: "public",
limit: 200,
paginationToken: nextPaginationToken,
// only get for those users who signed up with ThirdPartyEmailPassword
includeRecipeIds: ["thirdpartyemailpassword"],
})
users = usersResponse.users;
nextPaginationToken = usersResponse.nextPaginationToken;
}
import "github.com/supertokens/supertokens-golang/supertokens"
func main() {
// get the latest 100 users
result, err := supertokens.GetUsersNewestFirst("", nil, nil, nil, nil)
if err != nil {
// TODO: Handle error
return
}
// get the next 200 users
limit := 200
result, err = supertokens.GetUsersNewestFirst("", result.NextPaginationToken, &limit, nil, nil)
if err != nil {
// TODO: Handle error
return
}
// get for specific recipes
includeRecipeIds := []string{"thirdpartyemailpassword"}
result, err = supertokens.GetUsersNewestFirst("", result.NextPaginationToken, &limit, &includeRecipeIds, nil)
if err != nil {
// TODO: Handle error
return
}
}
- Asyncio
- Syncio
from supertokens_python.asyncio import get_users_newest_first
async def some_func():
# get the latest 100 users
users_response = await get_users_newest_first("public")
# get the next 200 users
users_response = await get_users_newest_first("public", 200, users_response.next_pagination_token)
# get for specific recipes
users_response = await get_users_newest_first(
"public",
200,
users_response.next_pagination_token,
# only get for those users who signed up with ThirdPartyEmailPassword
["thirdpartyemailpassword"]
)
from supertokens_python.syncio import get_users_newest_first
# get the latest 100 users
users_response = get_users_newest_first("public")
# get the next 200 users
users_response = get_users_newest_first("public", 200, users_response.next_pagination_token)
# get for specific recipes
users_response = get_users_newest_first(
"public",
200,
users_response.next_pagination_token,
# only get for those users who signed up with ThirdPartyEmailPassword
["thirdpartyemailpassword"]
)
#
Oldest First- NodeJS
- GoLang
- Python
- Other Frameworks
Important
import { getUsersOldestFirst } from "supertokens-node";
async function getUsers() {
// get the latest 100 users
let usersResponse = await getUsersOldestFirst({
tenantId: "public"
});
let users = usersResponse.users;
let nextPaginationToken = usersResponse.nextPaginationToken;
// get the next oldest 200 users
usersResponse = await getUsersOldestFirst({
tenantId: "public",
limit: 200,
paginationToken: nextPaginationToken,
});
users = usersResponse.users;
nextPaginationToken = usersResponse.nextPaginationToken;
// get for specific recipes
usersResponse = await getUsersOldestFirst({
tenantId: "public",
limit: 200,
paginationToken: nextPaginationToken,
// only get for those users who signed up with ThirdPartyEmailPassword
includeRecipeIds: ["thirdpartyemailpassword"]
});
users = usersResponse.users;
nextPaginationToken = usersResponse.nextPaginationToken;
}
- If the
nextPaginationToken
isundefined
, then there are no more users to loop through. - If there are no users in your app, then
nextPaginationToken
will beundefined
andusers
will be an empty array - Each element in the
users
array is according to the output of the core API as shown here.
import "github.com/supertokens/supertokens-golang/supertokens"
func main() {
// get the oldest 100 users
result, err := supertokens.GetUsersOldestFirst("", nil, nil, nil, nil)
if err != nil {
// TODO: Handle error
return
}
// get the next oldest 200 users
limit := 200
result, err = supertokens.GetUsersOldestFirst("", result.NextPaginationToken, &limit, nil, nil)
if err != nil {
// TODO: Handle error
return
}
// get for specific recipes
includeRecipeIds := []string{"thirdpartyemailpassword"}
result, err = supertokens.GetUsersOldestFirst("", result.NextPaginationToken, &limit, &includeRecipeIds, nil)
if err != nil {
// TODO: Handle error
return
}
}
- If the
result.NextPaginationToken
isnil
, then there are no more users to loop through. - If there are no users in your app, then
result.NextPaginationToken
will benil
andresult.Users
will be an empty array - Each element in the
result.Users
array is according to the output of the core API as shown here.
- Asyncio
- Syncio
from supertokens_python.asyncio import get_users_oldest_first
async def some_func():
# get the latest 100 users
users_response = await get_users_oldest_first("public")
# get the next 200 users
users_response = await get_users_oldest_first("public", 200, users_response.next_pagination_token)
# get for specific recipes
users_response = await get_users_oldest_first(
"public",
200,
users_response.next_pagination_token,
# only get for those users who signed up with ThirdPartyEmailPassword
["thirdpartyemailpassword"]
)
from supertokens_python.syncio import get_users_oldest_first
# get the latest 100 users
users_response = get_users_oldest_first("public")
# get the next 200 users
users_response = get_users_oldest_first("public", 200, users_response.next_pagination_token)
# get for specific recipes
users_response = get_users_oldest_first(
"public",
200,
users_response.next_pagination_token,
# only get for those users who signed up with ThirdPartyEmailPassword
["thirdpartyemailpassword"]
)
Multi Tenancy
Notice that we pass in the tenantId as "public"
. This means that the functions above will loop through the users of the public
tenantId. If you want to loop through other tenant IDs, you can pass in the tenant ID string to the function call.
This also implies that there is no way to loop through all users across all tenants in one go. If you want to do this, you will have to loop through each tenant one by one.
#
Get the number of users in your app- NodeJS
- GoLang
- Python
- Other Frameworks
Important
import {getUserCount} from "supertokens-node";
async function getCount() {
let count = await getUserCount()
}
import (
"fmt"
"github.com/supertokens/supertokens-golang/supertokens"
)
func main() {
tenantId := ""
count, err := supertokens.GetUserCount(nil, &tenantId)
if err != nil {
// TODO: Handle error
return
}
fmt.Println(count)
}
- Asyncio
- Syncio
from supertokens_python.asyncio import get_user_count
async def some_func():
user_count = await get_user_count()
print(user_count) # TODO..
from supertokens_python.syncio import get_user_count
user_count = get_user_count()
Multi Tenancy
By default, the getUserCount function will return the number of users across all tenants. If you want to get the number of users for a specific tenant, you can pass in the tenant ID string to the function call.