Sapling Logo
SDK authentication quickstart

Erlang JWT Generator

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

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

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

This example uses the jose library.

PublicKey = list_to_binary(os:getenv("SAPLING_PUBLIC_KEY")),
PrivateKey = list_to_binary(os:getenv("SAPLING_PRIVATE_KEY")),
ExpiresAt = erlang:system_time(second) + 3600,

Jwk = jose_jwk:from_oct(PrivateKey),
Claims = #{
  <<"sub">> => PublicKey,
  <<"exp">> => ExpiresAt
},

Signed = jose_jwt:sign(
  Jwk,
  #{<<"alg">> => <<"HS256">>},
  Claims
),
{_, Token} = jose_jws:compact(Signed),
io:format("~s~n", [Token]).
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 Erlang

Erlang is a programming language for the Erlang virtual machine. It is designed for distributed, fault-tolerant, and highly concurrent/parallelizable systems. Applications of Erlang include WhatsApp and telephony switches.