TypeScript 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.
TypeScript
HS256 · sub + exp
Install jsonwebtoken and its TypeScript types, then run this example in Node.js.
import {sign} from 'jsonwebtoken';
const publicKey = process.env.SAPLING_PUBLIC_KEY;
const privateKey = process.env.SAPLING_PRIVATE_KEY;
if (!publicKey || !privateKey) {
throw new Error(
'Set SAPLING_PUBLIC_KEY and SAPLING_PRIVATE_KEY first.',
);
}
const token = sign(
{sub: publicKey},
privateKey,
{algorithm: 'HS256', expiresIn: '1h'},
);
console.log(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 TypeScript
TypeScript is a strongly-typed programming language that transpiles into JavaScript. It is a superset or extension of JavaScript and adds optional static typing functionality. The language is developed and maintained by Microsoft.