Scala 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.
Scala
HS256 · sub + exp
This example uses jwt-scala.
import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim}
object Main {
def main(args: Array[String]): Unit = {
val publicKey = Option(System.getenv("SAPLING_PUBLIC_KEY"))
.filter(_.nonEmpty)
.getOrElse(throw new IllegalStateException("Set SAPLING_PUBLIC_KEY first."))
val privateKey = Option(System.getenv("SAPLING_PRIVATE_KEY"))
.filter(_.nonEmpty)
.getOrElse(throw new IllegalStateException("Set SAPLING_PRIVATE_KEY first."))
val expiresAt = (System.currentTimeMillis() / 1000) + 3600
val claim = JwtClaim(
subject = Some(publicKey),
expiration = Some(expiresAt)
)
println(Jwt.encode(claim, privateKey, JwtAlgorithm.HS256))
}
}
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 Scala
Scala is a programming language designed to run on the JVM, supporting both functional and object-oriented programming patterns. It is designed to be more concise than Java.