How to Debug a JWT Locally Without Leaking It
A 401 is rarely a mystery. The token already contains the answer — you just have to read it, and you should read it without shipping it to someone else's server.
Last month a colleague spent an afternoon chasing a 401 on a staging API. The token was valid, the secret was correct, the deployment was green. The problem turned out to be a container clock running eleven minutes behind. Nobody saw it because the error message said invalid token and the team kept looking at the signing code instead of the claims.
If you work on anything with OAuth, SSO, or a mobile app that talks to a REST backend, you will end up reading JWTs by hand sooner or later. Here is the routine I use, plus the four claim-level mistakes that account for most of the failures I have seen.
What a JWT actually is
A JWT is three Base64URL strings joined by dots: header, payload, signature. If you understand that much, you already understand 90% of the format.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 ← header
.eyJzdWIiOiI4NDIxIiwibmFtZSI6Ik1hcnRhIiwiZXhwIjoxNzY5NDAwMDAwfQ ← payload
.4f2c1b9d8e7a6f5c4b3a2910d8e7f6a5b4c3d2e1 ← signature
Both the header and the payload are plain JSON that has been Base64URL-encoded. They are not encrypted. Anyone holding the token can read every claim in it. That is by design, and it is also why you should never put a password, a national ID number, or a long-lived refresh credential inside a payload.
Decode it yourself, in about six lines
You do not need a library. The browser has everything required, though Base64URL has two quirks: it swaps + for - and / for _, and it often drops the = padding. Miss either step and atob throws a fit.
function decodeSegment(segment) {
const padded = segment + '='.repeat((4 - segment.length % 4) % 4);
const base64 = padded.replace(/-/g, '+').replace(/_/g, '/');
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
return JSON.parse(new TextDecoder().decode(bytes));
}
const [header, payload] = token.split('.');
console.log(decodeSegment(header), decodeSegment(payload));
The TextDecoder part matters more than it looks. Calling atob and passing the result straight into JSON.parse works fine for ASCII names, then breaks the first time someone's display name contains an umlaut or a Chinese character. I have seen that bug twice in production dashboards.
If you would rather not keep a snippet around, the JWT parser on this site does the same work in the page and never sends the token anywhere. Same idea, no clipboard risk.
The four claims that break things
1. exp and clock skew
exp is a Unix timestamp in seconds. Not milliseconds. The mistake is common enough that libraries like jsonwebtoken expose a clockTolerance option, precisely because two machines rarely agree on the current second.
If your API server validates tokens and your auth server issues them on different hosts, a drift of even 60 seconds will reject freshly minted tokens. Check both clocks before you rewrite any code. On Linux, timedatectl will tell you whether NTP is actually running, which is not always the case in containers.
2. nbf in the future
The opposite problem. A token with nbf (not before) set a few minutes ahead is valid, correctly signed, and rejected by every verifier until that moment arrives. Useful for staged rollouts, confusing when you forget it is there.
3. aud and iss mismatches
This is the one I would put at the top of the list for microservice setups. Service A issues a token with "aud": "billing-api", and the token gets sent to reports-api, which correctly rejects it. The signature is fine. The audience is wrong. Both services share the same secret, so nothing looks broken until you read the payload.
4. Key rotation with a stale kid
When a provider rotates signing keys, tokens in flight still carry the old kid in the header. Most verifiers handle this by fetching the JWKS again, but caches get in the way. If a token fails only for a few minutes around a rotation, the JWKS cache TTL is your first suspect, not your validation code.
Reading timestamps without doing math in your head
Unix seconds are awkward to eyeball. A quick reference for the values you are likely to meet:
| Claim value | Date (UTC) |
|---|---|
| 1735689600 | 1 Jan 2025, 00:00 |
| 1767225600 | 1 Jan 2026, 00:00 |
| 1798761600 | 1 Jan 2027, 00:00 |
A lifetime of 900 seconds is fifteen minutes; 3600 is an hour; 86400 is a day. If you see exp - iat = 31536000, someone issued a token valid for a year, and you should probably ask why before it ends up in a log file. The timestamp converter is handy for the odd values that do not fit the pattern.
Rules for decoding safely
- Treat a JWT like a password. A production token in a support ticket, a screenshot, or a shared browser tab is a credential leak.
- Prefer local decoding. Any online decoder receives whatever you paste; a token with a long expiry is enough to impersonate a user until it expires.
- Strip tokens from logs and error reports. Sentry and friends will happily store request headers for months.
- Do not trust the payload for authorization decisions on the client. The server must verify the signature and the claims; the front end is only reading.
- If you need to share a problematic token with a teammate, replace the signature and the user identifier first. The claim shape is what matters for debugging.
A short checklist for the next 401
- Decode header and payload. Confirm
algis what you expect and notnone. - Compare
expandnbfagainst the current UTC time on both machines. - Check
audandissagainst what the receiving service requires. - Look at
kidand confirm the verifier can reach the JWKS endpoint. - Only then read the signature-verification code.
Nine times out of ten, step two or three ends the investigation. The signing code, which is usually the first place people look, is seldom the culprit.
If the payload turns out to be malformed JSON — it happens with hand-built tokens and faulty test fixtures — paste it into the JSON formatter to see where the structure breaks. And if you are comparing two tokens to spot the difference, the JSON diff tool will line up the claims for you.