Sapling Logo
SDK authentication quickstart

Julia JWT Generator

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

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

Julia 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.
Julia HS256 · sub + exp

This example uses Julia's Base64 and SHA standard libraries plus JSON3.jl.

using Base64
using JSON3
using SHA

function base64url(data)
  replace(
    replace(rstrip(base64encode(data), '='), '+' => '-'),
    '/' => '_',
  )
end

public_key = get(ENV, "SAPLING_PUBLIC_KEY", "")
private_key = get(ENV, "SAPLING_PRIVATE_KEY", "")
if isempty(public_key) || isempty(private_key)
  error("Set SAPLING_PUBLIC_KEY and SAPLING_PRIVATE_KEY first.")
end

expires_at = floor(Int, time()) + 3600

header = base64url(JSON3.write(Dict("alg" => "HS256", "typ" => "JWT")))
payload = base64url(JSON3.write(Dict("sub" => public_key, "exp" => expires_at)))
signing_input = "$header.$payload"
signature = base64url(hmac_sha256(
  codeunits(private_key),
  codeunits(signing_input),
))

println("$signing_input.$signature")
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 Julia

Julia is a popular programming language for data science applications in industry and academia like numerical analysis and computational science. It is designed to be dynamic, concurrent/parallel and includes efficient libraries for linear algebra and floating-point calculations.