Sapling Logo
SDK authentication quickstart

Go JWT Generator

Sign a short-lived Sapling SDK credential in Go without exposing your private API key to browser code.

  • HS256 signature
  • Server-side only
  • Public + private keys
SIGN Go logo Go HS256 · sub + exp

Go JWT generator quickstart

Create a token with your public API key in the sub claim, a near-term Unix timestamp in exp, and an HS256 signature made with your private API key.

Sapling JWTs use HS256 with a sub claim containing your public API key and an exp claim containing a Unix timestamp. The examples use a one-hour lifetime; shorten it further when your application can refresh tokens easily.
Go HS256 · sub + exp

This example uses github.com/golang-jwt/jwt/v5.

package main

import (
  "fmt"
  "os"
  "time"

  "github.com/golang-jwt/jwt/v5"
)

func main() {
  publicKey := os.Getenv("SAPLING_PUBLIC_KEY")
  privateKey := os.Getenv("SAPLING_PRIVATE_KEY")
  if publicKey == "" || privateKey == "" {
    panic("set SAPLING_PUBLIC_KEY and SAPLING_PRIVATE_KEY first")
  }

  claims := jwt.MapClaims{
    "sub": publicKey,
    "exp": time.Now().Add(time.Hour).Unix(),
  }
  token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

  signed, err := token.SignedString([]byte(privateKey))
  if err != nil {
    panic(err)
  }
  fmt.Println(signed)
}
Generated token text/plain
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.<base64url-claims>.<signature>

Return the compact token to the browser and pass it to Sapling.init as the key. Issue a fresh token after it expires; never send the private key.

About Go

Go is a programming language designed at Google, to have performance similarity to C but with easier readability, memory safety, garbage collection and easier concurrency.