Sapling Logo
SDK authentication quickstart

PowerShell JWT Generator

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

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

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

This example signs the token with .NET's HMAC-SHA256 implementation.

if (-not $env:SAPLING_PUBLIC_KEY -or -not $env:SAPLING_PRIVATE_KEY) {
  throw "Set SAPLING_PUBLIC_KEY and SAPLING_PRIVATE_KEY first."
}

function ConvertTo-Base64Url {
  param([byte[]]$Bytes)
  [Convert]::ToBase64String($Bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_')
}

$header = @{ alg = "HS256"; typ = "JWT" } | ConvertTo-Json -Compress
$payload = @{
  sub = $env:SAPLING_PUBLIC_KEY
  exp = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() + 3600
} | ConvertTo-Json -Compress

$headerPart = ConvertTo-Base64Url ([Text.Encoding]::UTF8.GetBytes($header))
$payloadPart = ConvertTo-Base64Url ([Text.Encoding]::UTF8.GetBytes($payload))
$signingInput = "$headerPart.$payloadPart"

$hmac = [Security.Cryptography.HMACSHA256]::new(
  [Text.Encoding]::UTF8.GetBytes($env:SAPLING_PRIVATE_KEY)
)
try {
  $signature = ConvertTo-Base64Url (
    $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($signingInput))
  )
} finally {
  $hmac.Dispose()
}

"$signingInput.$signature"
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 PowerShell

PowerShell is Microsoft's command-line shell and scripting language for Windows, which is where it sees most adoption. It is the default automation tool for Windows-based scripts. PowerShell additionally supports Linux and macOS.