How to List All Users with Pagination Using Clerk in Go
In this tutorial, we will demonstrate how to list all users using the Clerk SDK for Go. We will cover setting up a client, counting users, and retrieving user data with pagination. By the end of this post, you’ll have a working example that can efficiently list users from your Clerk account.
Prerequisites
Before diving in, make sure you have the following:
- Go installed on your machine.
- Clerk account and API key.
Step-by-Step Guide
1. Install Clerk SDK
First, install the Clerk SDK for Go:
go get github.com/clerkinc/clerk-sdk-go/clerk
2. Initialize the Project
Create a new Go file, for example main.go
, and start by importing the necessary packages:
package main
import (
"fmt"
"github.com/clerkinc/clerk-sdk-go/clerk"
"log"
)
3. Set Up the Clerk Client
Initialize the Clerk client with your API key. Replace "your_api_key_here"
with your actual API key:
func main() {
client, err := clerk.NewClient("your_api_key_here")
if err != nil {
fmt.Println("Error creating client:", err)
return
}
...
}
4. Define Pagination Parameters
Specify the pagination parameters limit
and offset
:
// Define pagination parameters
limit := 25
offset := 0
5. Retrieve Total User Count
Fetch the total count of users:
// Retrieve total user count
count, err := client.Users().Count(clerk.ListAllUsersParams{})
if err != nil {
fmt.Println("Error counting users:", err)
return
}
totalCount := count.TotalCount
fmt.Printf("Total users: %d\n", totalCount)
6. List Users with Pagination
With the total count of users, loop through and list users in chunks:
// Loop to paginate through the users
for offset < totalCount {
// List users for the current page
users, err := client.Users().ListAll(clerk.ListAllUsersParams{
Limit: &limit,
Offset: &offset,
})
if err != nil {
log.Printf("Error listing users at offset %d: %v", offset, err)
return
}
// Print user info
for _, user := range users {
fmt.Printf("%s, %s, %s\n", getFirstName(user), getLastName(user), user.EmailAddresses[0].EmailAddress)
}
// Update offset for next page
offset += limit
}
fmt.Println("Completed listing all users.")
7. Helper Functions
Define two helper functions to get the first and last names of users:
func getFirstName(user clerk.User) string {
if user.FirstName != nil {
return *user.FirstName
}
return ""
}
func getLastName(user clerk.User) string {
if user.LastName != nil {
return *user.LastName
}
return ""
}
Complete Code
Here’s the complete code for your reference:
package main
import (
"fmt"
"github.com/clerkinc/clerk-sdk-go/clerk"
"log"
)
func main() {
client, err := clerk.NewClient("your_api_key_here")
if err != nil {
fmt.Println("Error creating client:", err)
return
}
// Define pagination parameters
limit := 25
offset := 0
// Retrieve total user count
count, err := client.Users().Count(clerk.ListAllUsersParams{})
if err != nil {
fmt.Println("Error counting users:", err)
return
}
totalCount := count.TotalCount
fmt.Printf("Total users: %d\n", totalCount)
// Loop to paginate through the users
for offset < totalCount {
// List users for the current page
users, err := client.Users().ListAll(clerk.ListAllUsersParams{
Limit: &limit,
Offset: &offset,
})
if err != nil {
log.Printf("Error listing users at offset %d: %v", offset, err)
return
}
// Print user info
for _, user := range users {
fmt.Printf("%s, %s, %s\n", getFirstName(user), getLastName(user), user.EmailAddresses[0].EmailAddress)
}
// Update offset for next page
offset += limit
}
fmt.Println("Completed listing all users.")
}
func getFirstName(user clerk.User) string {
if user.FirstName != nil {
return *user.FirstName
}
return ""
}
func getLastName(user clerk.User) string {
if user.LastName != nil {
return *user.LastName
}
return ""
}
Conclusion
In this blog post, we covered how to paginate through a list of users from Clerk using the Clerk SDK for Go. With the provided code, you can efficiently list all users and manage large datasets with ease.
Feel free to ask any questions or provide feedback in the comments! Happy coding!