Docs

Clerk Go SDK

The Clerk Go SDK is a wrapper over 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:

terminal
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. While operations for each resource vary, a similar pattern is applied throughout Clerk Go.

To execute API operations, you must configure Clerk Go with your Clerk secret key. To find your Clerk secret key:

  • Navigate to the Clerk Dashboard and select your application.
  • In the navigation sidebar, select API Keys.
  • In the Secret Keys section, you can copy your secret key. It is the same key across all frameworks.

Depending on your use case, there are two ways to use the Clerk Go SDK: With or without a client.

For most use cases, the API without a client is a better choice. 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(). The following example demonstrates this process:

import (
  "github.com/clerk/clerk-sdk-go/v2"
  "github.com/clerk/clerk-sdk-go/v2/$resource$"
)

// 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
resource, err := $resource$.Create(ctx, &$resource$.CreateParams{})

// Get
resource, err := $resource$.Get(ctx, id)

// Update
resource, err := $resource$.Update(ctx, id, &$resource$.UpdateParams{})

// Delete
resource, err := $resource$.Delete(ctx, id)

// List
list, err := $resource$.List(ctx, &$resource$.ListParams{})
for _, resource := range list.$Resource$s {
    // do something with the resource
}

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/$resource$"
)

// 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 := $resource$.NewClient(config)

// Create
resource, err := client.Create(ctx, &$resource$.CreateParams{})

// Get
resource, err := client.Get(ctx, id)

// Update
resource, err := client.Update(ctx, id, &$resource$.UpdateParams{})

// Delete
resource, err := client.Delete(ctx, id)

// List
list, err := client.List(ctx, &$resource$.ListParams{})
for _, resource := range list.$Resource$s {
    // do something with the resource
}

For more usage details, check out the examples or the library's README file.

Feedback

What did you think of this content?

Last updated on