Clerk Go SDK
The Clerk Go SDK is built on top of the Clerk Backend API.
Installation
If you're using Go Modules and have a go.mod
file in your project's root, you can import clerk-sdk-go
directly:
import (
"github.com/clerk/clerk-sdk-go/v2"
)
Alternatively, you can go get
the package explicitly:
go get -u github.com/clerk/clerk-sdk-go/v2
Usage
For details on how to use this module, see the Go Documentation.
The Clerk Go SDK is organized using a resource-based structure similar to the Clerk Backend API. Each API supports specific operations, like Create
or List
. For example, the actortoken
resource supports the Create
, Revoke
, and GetClient
operations. For the list of supported resources, see https://github.com/clerk/clerk-sdk-go/tree/v2
where almost each directory represents a resource.
To execute API operations, you must configure Clerk Go with your Clerk Secret Key. To find your Clerk Secret Key:
- In the Clerk Dashboard, navigate to the API keys page.
- In the Secret Keys section, copy your Secret Key.
Depending on your use case, there are two ways to use the Clerk Go SDK:
- Without a client: The best option if you're using one API key.
- With a client: The best option if you're using more than one API key.
For most use cases, the API without a client is a better choice as it requires a minimal setup and provides a more concise API for invoking operations. However, if you need to operate on multiple Clerk instances from one application, or need more flexibility for tests and mocking, you can instantiate multiple API clients with different API keys.
The following examples demonstrate both approaches.
Usage without a client
If you only use one API key, you can import the packages required for the APIs you need. Then you can execute your desired request methods as functions, such as $resource$.Get()
or $resource$.Delete()
, where $resource$
is the resource you want to interact with, such as user
.
import (
"github.com/clerk/clerk-sdk-go/v2"
"github.com/clerk/clerk-sdk-go/v2/user"
)
// Each operation requires a context.Context as the first argument
ctx := context.Background()
// Set the API key with your Clerk Secret Key
clerk.SetKey("sk_live_xxx")
// Create a new user
newUser, err := user.Create(ctx, &user.CreateParams{})
if err != nil {
// Handle error
}
// Get user details
userDetails, err := user.Get(ctx, "user_id")
if err != nil {
// Handle error
}
// List users
userList, err := user.List(ctx, &user.ListParams{})
if err != nil {
// Handle error
}
for _, u := range userList.Users {
// Process each user
}
// Delete user
deletedResource, err := user.Delete(ctx, "user_id")
if err != nil {
// Handle error
}
Usage with a client
If you're working with multiple keys, it's recommended to use Clerk Go with a client. The API packages for each
resource export a Client
, which supports all the API's operations.
This way, you can create as many clients as needed, each with their own API key, as shown in the following example:
import (
"github.com/clerk/clerk-sdk-go/v2"
"github.com/clerk/clerk-sdk-go/v2/user"
)
// Each operation requires a context.Context as the first argument.
ctx := context.Background()
// Initialize a client with an API key
config := &clerk.ClientConfig{}
config.Key = "sk_live_xxx"
client := user.NewClient(config)
// Create a new user
newUser, err := client.Create(ctx, &user.CreateParams{})
if err != nil {
// Handle error
}
// Get user details
userDetails, err := client.Get(ctx, "user_id")
if err != nil {
// Handle error
}
// List users
userList, err := client.List(ctx, &user.ListParams{})
if err != nil {
// Handle error
}
for _, u := range userList.Users {
// Process each user
}
// Delete user
deletedResource, err := client.Delete(ctx, "user_id")
if err != nil {
// Handle error
}
For more usage details, see the examples or the library's README file.
Feedback
Last updated on