Home / Guides / What is a JWT? How JSON Web Tokens work and how to decode one safely
What is a JWT? How JSON Web Tokens work and how to decode one safely
A JSON Web Token (JWT) is a compact string that carries information between two parties and can be checked for tampering. You meet them in sign-in systems, single sign-on and APIs. A browser or app logs in once, receives a token, and then presents it with each request instead of sending the password again.
The structure
A JWT is three parts separated by dots:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
- Header. Describes the token, for example
{"alg":"HS256","typ":"JWT"}. - Payload. The data (the claims), for example
{"sub":"1234567890","name":"John Doe","iat":1516239022}. - Signature. A value calculated from the first two parts with a secret or private key.
The header and payload are JSON encoded with Base64URL, a variant of Base64 that uses - and _ and drops the trailing =. That is encoding, not encryption: anyone who holds the token can read the header and payload. You can see this by pasting it into our JWT Decoder.
Common claims
| Claim | Name | Meaning |
|---|---|---|
iss |
Issuer | Who created the token |
sub |
Subject | Who the token is about, often a user ID |
aud |
Audience | Who the token is intended for |
exp |
Expiration time | After this moment the token must be rejected |
nbf |
Not before | Before this moment the token is not yet valid |
iat |
Issued at | When the token was created |
jti |
JWT ID | A unique identifier, useful to revoke or detect reuse |
Times are Unix timestamps: seconds since 1 January 1970 UTC.
How the signature works
The signature is what stops a user from editing their own token. Two main families exist:
- Symmetric (HS256, HS384, HS512). The same secret signs and verifies, using HMAC. Simple, but every service that verifies tokens needs the secret.
- Asymmetric (RS256, ES256, EdDSA). A private key signs and a public key verifies. Better when many services need to verify tokens but only one should create them.
When the server receives a token it recomputes the signature, compares it and checks the claims (exp, iss, aud). If anything fails, it rejects the token.
Decoding is not verifying
This distinction causes many mistakes. Decoding only turns the Base64URL text back into JSON, so anyone can do it. Verifying checks the signature with the key, which only the server (or someone with the right key) can do.
A decoded token tells you what it claims. It does not prove it is genuine. The JWT Decoder works in your browser, shows the header and payload and whether exp has passed, and deliberately does not verify the signature.
Common security mistakes
- Accepting
alg: none. Some libraries once allowed unsigned tokens when the header said so. Always require a specific algorithm on the server. - Algorithm confusion. If a server expects RS256 but also accepts HS256, an attacker can sign a token with the public key used as an HMAC secret. Fix the algorithm on the server and never trust the header to choose it.
- Weak HMAC secrets. A short or guessable secret can be brute-forced offline from a single token. Use a long random value (at least 32 random bytes).
- Skipping claim checks. Validate
exp,nbf,issandaud, not just the signature. A valid token meant for a different service should be rejected. - Putting secrets in the payload. Passwords, personal data and anything confidential does not belong there, because the payload is readable. Use encryption (JWE) if you must carry sensitive data.
- Long-lived tokens. A stolen token works until it expires. Use short lifetimes (minutes) for access tokens and a refresh mechanism.
- Hard-to-revoke tokens. Because the server does not look anything up, you cannot easily invalidate a token before it expires. Keep lifetimes short, keep a deny list for emergencies, and rotate keys.
- Storing tokens unsafely. In
localStoragethey can be stolen by any script injected into the page (XSS). A cookie withHttpOnly,SecureandSameSiteis usually the safer choice for browser sessions. See HTTP security headers explained.
When not to use a JWT
JWTs are not a replacement for sessions in every case. A classic server-side session with a random identifier is simpler to revoke and exposes nothing to the client. JWTs make sense when tokens must be verified by several independent services without calling a central database, or in standard sign-in protocols such as OpenID Connect.
Handling a token you found
If you receive a token and want to know what is in it:
- Decode it with the JWT Decoder and check the claims, especially
exp. - Do not paste live production tokens into websites you do not trust. Treat a valid token like a temporary password. Our decoder runs entirely in your browser, but the habit matters.
- Remember that decoding proves nothing. The only way to know a token is genuine is to verify its signature with the correct key.
If you want to understand the encoding step by hand, try the Base64, URL & Hex Encoder. To see how a signature is computed from a secret, our Hash Generator covers the hash functions behind HMAC. The relationship between these concepts is explained in encoding vs encryption vs hashing.