Sapling Logo
SDK authentication quickstart

Perl JWT Generator

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

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

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

This example uses JSON::WebToken.

use strict;
use warnings;

use JSON::WebToken qw(encode_jwt);

my $public_key = $ENV{SAPLING_PUBLIC_KEY}
  // die "Set SAPLING_PUBLIC_KEY first.\n";
my $private_key = $ENV{SAPLING_PRIVATE_KEY}
  // die "Set SAPLING_PRIVATE_KEY first.\n";

my $claims = {
  sub => $public_key,
  exp => time + 3600,
};

print encode_jwt($claims, $private_key, 'HS256'), "\n";
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 Perl

Perl is a family of high-level interpreted scripting languages, including Perl 5 and Perl 6, which was renamed to Raku. It is popular as a scripting language because of its built-in regular expression and string parsing functionality.