Skip to content

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-c

Decode 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:

bash
cat token.txt | fx -r '.split(".")[0]' fromBase64 JSON.parse

Output:

json
{
  "alg": "HS256",
  "typ": "JWT"
}

Decode the JWT Payload (base64url) ​

If you try the same thing on the payload, it fails:

bash
cat token.txt | fx -r '.split(".")[1]' fromBase64
  .split(".")[1] fromBase64
                 ^^^^^^^^^^

GoError: illegal base64 data at input byte 155

JWTs 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:

bash
cat token.txt | fx -r '.split(".")[1]' \
  '.replaceAll("-", "+").replaceAll("_", "/")' \
  'x => x + "=".repeat((4 - x.length % 4) % 4)' \
  fromBase64 JSON.parse

Output:

json
{
  "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:

bash
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:

bash
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:

json
{
  "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:

bash
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:

json
{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzhm...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "rt_5Kq2vX9mB3nL8pW1",
  "scope": "openid profile email"
}

Output:

json
{
  "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:

bash
fx test-claims.json 'x => [{alg: "none", typ: "JWT"}, x].map(p => toBase64(JSON.stringify(p)).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "")).join(".") + "."'

Input:

json
{
  "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:

js
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:

bash
fx oauth.json .access_token jwt .expired
true

With -r, fx treats each line as a separate string, so a file with one token per line is decoded token by token:

bash
cat tokens.txt | fx -r jwt '{sub: x.payload.sub, alg: x.header.alg, expired: x.expired}'

Output:

json
{
  "sub": "user_8f3a2c",
  "alg": "HS256",
  "expired": true
}
{
  "sub": "test_user",
  "alg": "none",
  "expired": false
}

See Also ​