Ruby 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.
Ruby
HS256 · sub + exp
This example uses the jwt gem.
require 'jwt'
public_key = ENV.fetch('SAPLING_PUBLIC_KEY') { raise 'Set SAPLING_PUBLIC_KEY first.' }
private_key = ENV.fetch('SAPLING_PRIVATE_KEY') { raise 'Set SAPLING_PRIVATE_KEY first.' }
claims = {
sub: public_key,
exp: Time.now.to_i + 3600
}
puts JWT.encode(claims, private_key, '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 Ruby
Ruby is a programming language that shares design philosophies of Python and Perl around scripting, usability, and programming productivity but with a more object-oriented approach. Its popularity and adoption increased with Ruby on Rails, an MVC web framework written in Ruby.