Handling requests with Go

Go Middleware

The Clerk Go SDK provides an easy-to-use middleware that adds the active session to the request's context.

Pro tip! If you are signed into your Clerk Dashboard, your secret key should become visible by clicking on the eye icon. Otherwise, you can find your keys in the Clerk Dashboard on the API Keys page.

package main

import (

func main() {
	// Create a new Clerk client with your secret key from the Clerk Dashboard
	client, _ := clerk.NewClient(`YOUR_SECRET_KEY`)

	mux := http.NewServeMux()

	injectActiveSession := clerk.WithSession(client)
	mux.Handle("/hello", injectActiveSession(helloUserHandler(client)))

	http.ListenAndServe(":8080", mux)

func helloUserHandler(client clerk.Client) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()

		sessClaims, ok := ctx.Value(clerk.ActiveSessionClaims).(*clerk.SessionClaims)
		if !ok {

		user, err := client.Users().Read(sessClaims.Subject)
		if err != nil {

		w.Write([]byte("Welcome " + *user.FirstName))


What did you think of this content?