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:
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.
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 argumentctx := context.Background()// Set the API key with your Clerk Secret Keyclerk.SetKey("sk_live_xxx")// Create a new usernewUser, err := user.Create(ctx, &user.CreateParams{})if err !=nil {// Handle error}// Get user detailsuserDetails, err := user.Get(ctx, "user_id")if err !=nil {// Handle error}// List usersuserList, err := user.List(ctx, &user.ListParams{})if err !=nil {// Handle error}for _, u :=range userList.Users {// Process each user}// Delete userdeletedResource, err := user.Delete(ctx, "user_id")if err !=nil {// Handle error}
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 keyconfig :=&clerk.ClientConfig{}config.Key ="sk_live_xxx"client := user.NewClient(config)// Create a new usernewUser, err := client.Create(ctx, &user.CreateParams{})if err !=nil {// Handle error}// Get user detailsuserDetails, err := client.Get(ctx, "user_id")if err !=nil {// Handle error}// List usersuserList, err := client.List(ctx, &user.ListParams{})if err !=nil {// Handle error}for _, u :=range userList.Users {// Process each user}// Delete userdeletedResource, err := client.Delete(ctx, "user_id")if err !=nil {// Handle error}