Decoding JWT Tokens in the Terminal
A JWT is three base64url strings joined by dots: header.payload.signature. fx can decode the header and payload with its built-in fromBase64, so you don't need to paste tokens into a website. Install fx first.
The examples use token.txt, which holds one token on a single line:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzhmM2EyYyIsIm5hbWUiOiJBbmEgUmFtw61yZXoiLCJlbWFpbCI6ImFuYUBleGFtcGxlLmNvbSIsInBpY3R1cmUiOiJodHRwczovL2Nkbi5leGFtcGxlLmNvbS91LzhmM2EyYy5wbmc_cz02NCIsIm9yZyI6Imdsb2JleCIsInJvbGVzIjpbImFkbWluIiwiYmlsbGluZyJdLCJpc3MiOiJodHRwczovL2F1dGguZXhhbXBsZS5jb20iLCJhdWQiOiJhcGkuZXhhbXBsZS5jb20iLCJpYXQiOjE3NjcyMjU2MDAsImV4cCI6MTc2NzIyOTIwMH0.q3N7vY0dKc8ZtR2mW5xJpL9aHfE4uB6sQiO1gT_nV-cDecode the JWT Header
The token is not JSON, so pass -r to read it as a raw string. Split on ., take the first part, and decode it:
cat token.txt | fx -r '.split(".")[0]' fromBase64 JSON.parseOutput:
{
"alg": "HS256",
"typ": "JWT"
}Decode the JWT Payload (base64url)
If you try the same thing on the payload, it fails:
cat token.txt | fx -r '.split(".")[1]' fromBase64 .split(".")[1] fromBase64
^^^^^^^^^^
GoError: illegal base64 data at input byte 155JWTs use base64url, not standard base64. Base64url uses - and _ where base64 uses + and /, and it drops the trailing = padding. fromBase64 expects standard base64, so swap the characters back and add the padding before you decode:
cat token.txt | fx -r '.split(".")[1]' \
'.replaceAll("-", "+").replaceAll("_", "/")' \
'x => x + "=".repeat((4 - x.length % 4) % 4)' \
fromBase64 JSON.parseOutput:
{
"sub": "user_8f3a2c",
"name": "Ana Ramírez",
"email": "ana@example.com",
"picture": "https://cdn.example.com/u/8f3a2c.png?s=64",
"org": "globex",
"roles": [
"admin",
"billing"
],
"iss": "https://auth.example.com",
"aud": "api.example.com",
"iat": 1767225600,
"exp": 1767229200
}Decode a JWT From a Shell Variable or the Clipboard
If the token is in a variable, echo it into fx. This one-liner prints the header and the payload as an array:
echo "$TOKEN" | fx -r 'x => x.split(".").slice(0, 2).map(p => JSON.parse(fromBase64(p.replaceAll("-", "+").replaceAll("_", "/") + "=".repeat((4 - p.length % 4) % 4))))'The same pipe works with any command that prints a token, like pbpaste on macOS or wl-paste on Wayland. Everything runs locally, so nothing is sent to a third-party decoder site.
Convert JWT exp and iat to Human-Readable Dates
iat and exp are Unix timestamps in seconds. Multiply them by 1000 to get JavaScript milliseconds, then compare the result with Date.now() to see whether the token has expired:
cat token.txt | fx -r '.split(".")[1]' \
'.replaceAll("-", "+").replaceAll("_", "/")' \
'x => x + "=".repeat((4 - x.length % 4) % 4)' \
fromBase64 JSON.parse \
'x => ({
issued: new Date(x.iat * 1000).toISOString(),
expires: new Date(x.exp * 1000).toISOString(),
ttlMinutes: (x.exp - x.iat) / 60,
expired: x.exp * 1000 < Date.now()
})'Output:
{
"issued": "2026-01-01T00:00:00.000Z",
"expires": "2026-01-01T01:00:00.000Z",
"ttlMinutes": 60,
"expired": true
}Decode a JWT Inside a JSON Response
An OAuth token endpoint returns JSON with the JWT in access_token (or id_token). The response is already JSON, so leave out -r and start with the field:
fx oauth.json '.access_token.split(".")[1]' \
'.replaceAll("-", "+").replaceAll("_", "/")' \
'x => x + "=".repeat((4 - x.length % 4) % 4)' \
fromBase64 JSON.parse \
'{sub: x.sub, roles: x.roles, aud: x.aud}'Input:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzhm...",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "rt_5Kq2vX9mB3nL8pW1",
"scope": "openid profile email"
}Output:
{
"sub": "user_8f3a2c",
"roles": [
"admin",
"billing"
],
"aud": "api.example.com"
}Create an Unsigned JWT for Testing
toBase64 does the reverse. This command builds an alg: none token from a claims file. It converts the output to base64url and leaves the signature empty:
fx test-claims.json 'x => [{alg: "none", typ: "JWT"}, x].map(p => toBase64(JSON.stringify(p)).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "")).join(".") + "."'Input:
{
"sub": "test_user",
"roles": ["viewer"],
"exp": 1798761600
}Output:
eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJzdWIiOiJ0ZXN0X3VzZXIiLCJyb2xlcyI6WyJ2aWV3ZXIiXSwiZXhwIjoxNzk4NzYxNjAwfQ.WARNING
fx only decodes tokens. It never checks the signature, so a decoded payload tells you what the token claims, not that the claims are true. Anyone can create a token like the one above. Verify signatures on the server with a JWT library and the issuer's key, and make sure your services reject alg: none.
Add a jwt Helper to .fxrc.js
If you decode tokens often, add a helper to .fxrc.js in your home directory:
function b64url(s) {
s = s.replaceAll('-', '+').replaceAll('_', '/')
return fromBase64(s + '='.repeat((4 - s.length % 4) % 4))
}
function jwt(token) {
const [header, payload, signature] = token.trim().split('.')
const claims = JSON.parse(b64url(payload))
const date = t => t === undefined ? undefined : new Date(t * 1000).toISOString()
return {
header: JSON.parse(b64url(header)),
payload: claims,
issuedAt: date(claims.iat),
expiresAt: date(claims.exp),
expired: claims.exp !== undefined && claims.exp * 1000 < Date.now(),
signature,
}
}Now jwt works like any built-in:
fx oauth.json .access_token jwt .expiredtrueWith -r, fx treats each line as a separate string, so a file with one token per line is decoded token by token:
cat tokens.txt | fx -r jwt '{sub: x.payload.sub, alg: x.header.alg, expired: x.expired}'Output:
{
"sub": "user_8f3a2c",
"alg": "HS256",
"expired": true
}
{
"sub": "test_user",
"alg": "none",
"expired": false
}