How to Decode a JWT and Debug “401 Unauthorized” Errors
You send a request, and the API answers with 401 Unauthorized. No details, no hint, just a locked door. Most of the time the cause sits inside your token, and you can find it in about two minutes once you know how to decode a JWT and read what’s in it.
This guide shows you how to decode a token by hand, which claims cause the most trouble and a simple routine for tracking down the real reason behind a 401. I’ll also point out the traps, because a few of them look like token problems and aren’t.
What a JWT actually contains
A JWT (JSON Web Token) is a string made of three parts separated by two dots. The first part is the header, the second is the payload and the third is the signature.
The header says how the token was signed. It usually holds the algorithm, such as HS256 or RS256, and often a key ID called kid. The payload holds the claims, which are facts about the user and the token. The signature proves nobody changed the first two parts.
Here’s the thing most people miss. The header and payload are encoded, not encrypted. Anyone who holds the token can read them. So never put secrets in a payload, and treat every token like a password when you share it.
The encoding is called Base64URL. It’s Base64 with two characters swapped so the result is safe inside a URL, and with the padding removed.
How to decode a JWT step by step
You don’t need special software. Here’s the manual routine:
- Copy the token and drop the word Bearer if it’s attached.
- Split the string at the two dots. You now have three chunks.
- Decode the first two chunks from Base64URL. Swap the two URL safe characters back to the standard ones and add padding if your tool asks for it. Our Base64 decoder handles the decoding once you do.
- Read the result. Each chunk is plain JSON.
- Convert the time claims to dates with our timestamp converter, because they’re stored as numbers.
A payload might read {"sub":"42","aud":"billing","exp":1735689600}. That exp number is a Unix timestamp in seconds. It equals January 1, 2025, so a token carrying it is long expired and the server rejects it with a 401 the moment it checks.
Our JWT decoder decodes a token in one click, and so do debuggers such as jwt.io. Whichever you use, pick a test token or an expired one, and read our guide on reading a JWT safely before you paste anything live.
Decoding is not verifying
Reading a token and trusting a token are different jobs. Decoding shows you what the token claims. Verifying checks the signature with the right secret or public key and confirms the token is still valid. Only the server that owns the key can do the second part.
That’s why a token can look perfect when you decode it and still earn a 401. The payload may be fine while the signature fails, because the server uses a different key than the one that signed the token.
One more rule for developers. Tell your library which algorithms it may accept, and never allow the none algorithm. Libraries that trust the header blindly have led to real attacks.
The claims that cause most 401 errors
When you read the payload, check these first:
- exp is the expiry time. If it’s in the past, the server rejects the token. This is the number one cause of a 401.
- nbf means not before. If it’s in the future, the token isn’t valid yet. Clock differences between servers cause this one.
- iat is the issue time. A value far in the future points to a clock problem on the machine that issued the token.
- iss is the issuer. It must match the identity provider your API trusts. A token from a test tenant fails on a production API.
- aud is the audience. It names the API the token is meant for. A token minted for one API fails on another, and this one hits people who use Auth0, Okta, Azure AD or Keycloak.
- scope or roles list what the user may do. Missing permissions should give a 403, but some APIs return a 401 instead.
Time claims count in seconds, not milliseconds. A JavaScript timestamp from Date.now is in milliseconds, and mixing the two creates tokens that never expire or expire at once.
401 vs 403: which one are you seeing?
A 401 means the server doesn’t accept who you are. The token is missing, expired, malformed or fails verification. A 403 means the server knows who you are and still says no, usually because of permissions. Knowing the difference saves hours, because a 401 sends you to the token and a 403 sends you to roles and policies.
A debugging routine that finds the cause fast
Work through these steps in order. Most cases end by step four.
- Read the response. Many servers add an authenticate header with a reason such as invalid_token and a short description like expired. Read the body too.
- Look at the request your client really sent. Use your browser’s Network tab or your HTTP client’s verbose mode, and confirm the Authorization header exists and starts with Bearer and a space. Our API request builder lets you rebuild the call and test it.
- Decode the token and check exp, nbf, iss and aud.
- Compare iss and aud with your server’s config, character by character. A trailing slash counts.
- Check the algorithm and key ID against the keys your server uses.
- Read the server logs. A good API logs the exact reason it rejected a token, even when the client only sees a bare 401.
- Get a fresh token and try again. If the new one works, the old one expired or was revoked.
Header problems that look like token problems
Sometimes the token is perfect and never arrives. Watch for these:
- A missing or misspelled scheme, like Token instead of Bearer.
- Extra characters. Quotes, line breaks or spaces copied along with the token break the match.
- Redirects. Some clients drop the Authorization header when a request follows a redirect to another host. Our redirect checker shows where a request really ends up.
- Proxies and gateways that strip headers they don’t recognize.
- CORS preflight. The browser sends an OPTIONS request without your token first. If the server demands a token on that request, it answers 401 before your real request even starts.
- Cookies versus headers. Check whether the API expects the token in a cookie or in the Authorization header.
Signature and key problems
If the claims look right, suspect the signature. The usual causes are a wrong secret, the wrong environment or a rotated key. Identity providers rotate signing keys on a schedule, and your API must fetch the new public keys when it meets a kid it doesn’t know. A cached key list that never refreshes produces 401 errors that appear overnight.
Also check the algorithm. A token signed with RS256 can’t verify against a shared secret meant for HS256. And a dev token will never verify against production keys.
Habits that prevent repeat 401 errors
A few small habits cut the number of surprise 401 errors.
- Refresh access tokens a little before they expire, and retry a failed request once with a new token.
- Allow a small clock leeway, such as 30 to 60 seconds, when you check exp and nbf.
- Keep access tokens short lived, and rely on refresh tokens for longer sessions.
- Log the exact rejection reason on the server and return a generic message to the client.
- Use throwaway test tokens when you debug, never live ones.
- Write the expected iss and aud in your docs, so the next person doesn’t guess.
The short version
To decode a JWT, split it at the dots and decode the first two parts from Base64URL. Then check exp, nbf, iss and aud, because those four claims cause most 401 errors. If the claims look fine, look at the header the client sends, the signing algorithm and the keys on the server. Decoding shows what a token says, and only the server can verify it.
Take the last token that failed on you and run through the routine. Two minutes of reading beats an afternoon of guessing.