What Is a JWT (JSON Web Token)?
- The three parts of a JWT
- The header
- The payload
- The signature
- Signing is not encryption
- The claims you'll actually see
- How JWTs enable stateless authentication
- Verifying a signature
- Frequently asked questions
- Can I decode a JWT without the secret key?
- Where should I store a JWT in a browser?
- Are JWTs better than sessions?
On this page
- The three parts of a JWT
- The header
- The payload
- The signature
- Signing is not encryption
- The claims you'll actually see
- How JWTs enable stateless authentication
- Verifying a signature
- Frequently asked questions
- Can I decode a JWT without the secret key?
- Where should I store a JWT in a browser?
- Are JWTs better than sessions?
If you've built or consumed an API in the last decade, you've almost certainly handled a JSON Web Token — a long, opaque-looking string with two dots in it, usually arriving in an Authorization header. JWTs are the default way most modern applications carry proof of who a user is between a server that issued that proof and a server that needs to trust it. They look cryptic, which leads a lot of developers to treat them as magic strings that either work or don't.
They aren't magic, and they aren't even particularly secret. A JWT is a structured, self-describing container that anyone can read — the security comes from a signature on the end, not from the contents being hidden. That one distinction clears up most of the confusion around JWTs, including the most common security mistake developers make with them.
The three parts of a JWT
Every JWT is made of exactly three segments joined by dots: header.payload.signature. Each of those three segments is independently Base64URL-encoded — a URL-safe variant of Base64 that swaps the + and / characters for - and _ and drops the trailing = padding, so the whole token can be dropped into a URL, a cookie or a header without needing further escaping.
The header
The header is a small JSON object describing the token itself. It almost always contains alg (the signing algorithm, such as HS256 or RS256) and typ (the token type, normally "JWT"). Tokens signed with a rotating key set often also carry a kid, a key ID that tells the verifying server which public key to look up. Decode the first segment of any JWT and you'll see this JSON in full.
The payload
The payload — also called the claims set — is another JSON object, and it's the part people actually care about. It carries statements about the user and the token: who they are, when the token was issued, when it stops being valid, and whatever application-specific data the issuer chose to include, such as a role or a tenant ID.
The signature
The signature is computed over the encoded header and payload joined with a dot, using the algorithm named in the header and a key held by the issuer. It isn't reversible, and its only job is to let a recipient answer one question: has anything in this token changed since it was issued by someone holding the key?
Signing is not encryption
This is the point that trips up more developers than anything else about JWTs. A standard signed JWT (a JWS, in spec terminology) is not encrypted at all. The payload is merely Base64URL-encoded, which is an encoding scheme with no key and no secret — anyone who intercepts, copies or is handed the token can decode it and read every claim inside it in about two seconds.
What the signature gives you is integrity and authenticity, not confidentiality. If an attacker changes the user ID in the payload from 41 to 1 and re-encodes it, the signature no longer matches, and any server that verifies properly will reject the token. But the attacker could still read that the original user ID was 41 — and everything else you put in there.
Watch out
Never put anything in a JWT payload that you wouldn't be comfortable printing on the user's screen: no passwords, no API keys, no full card numbers, no internal notes about an account. Assume the payload is public, because to anyone holding the token, it is.
The claims you'll actually see
The JWT specification defines a set of registered claims with agreed-upon meanings, so different libraries and services interoperate. You don't have to use them, but almost everyone does:
- iss (issuer): who created and signed the token — typically your auth service or identity provider.
- sub (subject): who the token is about, usually a stable user ID. It should be unique and not something that changes, like an email address.
- aud (audience): who the token is intended for. An API should reject tokens whose audience isn't itself, even if the signature is valid.
- exp (expiration): a Unix timestamp in seconds after which the token must be rejected.
- nbf (not before): a timestamp before which the token isn't yet valid.
- iat (issued at): when the token was created, often used to enforce a maximum token age separately from exp.
- jti (JWT ID): a unique identifier for the token, which lets you track or revoke individual tokens.
Note that exp, nbf and iat are all in seconds, not milliseconds. A surprising number of bugs come from generating these with a JavaScript Date.now() call and forgetting to divide by 1000, which produces an expiry roughly fifty thousand years in the future — a token that never expires, which is exactly what you didn't want.
How JWTs enable stateless authentication
The traditional alternative is a session: the server generates a random session ID, stores the user data in a database or cache, and hands the ID to the browser in a cookie. Every subsequent request means a lookup to find out who that session belongs to.
A JWT inverts this. Instead of an ID pointing at server-side state, the token carries the state itself, signed so it can't be forged. A server verifies the signature with a key it already has, reads the claims, and serves the request — no database lookup, no shared session store. That's what makes JWTs attractive for APIs split across services or regions: any service holding the verification key can authenticate independently.
The trade-off is revocation. Because nothing is looked up, nothing can easily be invalidated: if a user logs out or you disable an account, any already-issued token stays cryptographically valid until its exp passes. The usual mitigation is short-lived access tokens — minutes, not days — paired with a longer-lived refresh token stored server-side, where it can be revoked.
Verifying a signature
How verification works depends on which family of algorithm the token uses, and the difference matters for how you architect things:
- HMAC algorithms (HS256, HS384, HS512) use a single shared secret for both signing and verifying. Simple, fast, and fine when the same party does both — but every service that can verify a token can also mint one.
- RSA and ECDSA algorithms (RS256, ES256 and friends) use a private key to sign and the matching public key to verify. The issuer keeps the private key; every consuming service only needs the public key, which can be published openly via a JWKS endpoint.
If you have more than one service verifying tokens, prefer asymmetric signing. It means a compromised API server leaks only a public key, not the ability to issue valid tokens for any user in your system.
Tip
Always pin the expected algorithm when verifying, rather than trusting the alg value in the token's own header. Historic vulnerabilities in JWT libraries involved attackers setting alg to "none" or swapping RS256 for HS256 and signing with the public key as the HMAC secret. Good libraries now require you to declare which algorithms you accept — declare them.
Frequently asked questions
Can I decode a JWT without the secret key?
Yes, completely. Decoding requires nothing but a Base64URL decoder, which is why a browser-based decoder can show you a token's header and payload instantly. The secret or public key is only needed to verify the signature — to confirm the token is genuine — not to read what's inside it.
Where should I store a JWT in a browser?
There's no universally correct answer, only trade-offs. localStorage is easy but readable by any JavaScript on the page, so a single XSS bug hands over the token. An httpOnly cookie is invisible to JavaScript, but introduces CSRF concerns that SameSite attributes or anti-CSRF tokens need to address. Most teams land on httpOnly, Secure, SameSite cookies plus short token lifetimes.
Are JWTs better than sessions?
Not inherently — they solve a different problem. For a single application with one database, server-side sessions are simpler, instantly revocable and perfectly adequate. JWTs earn their complexity when multiple independent services, or a third party, must validate identity without calling back to a central authority on every request.
Boiled down: a JWT is three Base64URL-encoded segments, the middle one is readable by anyone, and the last one only proves the first two haven't been tampered with. Keep secrets out of the payload, keep expiry times short, pin your algorithms on verification, and JWTs become a genuinely useful tool rather than a mysterious string you hope keeps working.
Ready to put this into practice?
Decode a JWT Free