Skip to main content
Developer & Security 11 min read

JWT Tokens Explained: How JSON Web Tokens Work & When to Use Them

A clear, practical guide to JWT (JSON Web Tokens) — what they contain, how they work, common use cases, security pitfalls, and when to use JWTs vs sessions.

MD
Meet Dhameliya

JSON Web Tokens (JWTs) are one of the most widely used authentication mechanisms in modern web applications — and one of the most commonly misunderstood. Every developer working with APIs, single-page apps, or microservices will encounter JWTs. Understanding how they work (and what they don't do) is essential for building secure applications.

Use our JWT Decoder to inspect any JWT and see its header, payload, and signature in readable format.

What Is a JWT?

A JWT is a compact, URL-safe token that contains a JSON payload with claims (statements about the user or session). It is digitally signed so the recipient can verify that it was issued by a trusted party and has not been tampered with.

A JWT looks like this:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

It has three parts separated by dots:

  1. Header: Algorithm and token type (e.g., HS256, RS256)
  2. Payload: Claims — user ID, roles, expiration time, etc.
  3. Signature: Cryptographic signature that verifies the token's integrity

How JWT Authentication Works

  1. User logs in with username/password.
  2. Server verifies credentials and creates a JWT containing the user's ID, roles, and an expiration time.
  3. Server signs the JWT with a secret key (HMAC) or private key (RSA/ECDSA) and returns it to the client.
  4. Client stores the JWT (typically in memory or an httpOnly cookie) and sends it with every subsequent API request in the Authorization header: Authorization: Bearer eyJhbG...
  5. Server receives the request, verifies the JWT signature, checks expiration, and extracts the user's identity from the payload — without querying a database.

This is the key advantage of JWTs: stateless authentication. The server does not need to store session data or query a database on every request — the JWT itself contains all necessary information.

Standard JWT Claims

ClaimNamePurpose
issIssuerWho issued the token (your auth server)
subSubjectWho the token is about (user ID)
audAudienceIntended recipient (your API)
expExpirationUnix timestamp when the token expires
iatIssued AtUnix timestamp when the token was created
jtiJWT IDUnique identifier for this specific token

You can add custom claims (user roles, permissions, tenant ID) in the payload alongside these standard claims.

JWT vs Server-Side Sessions

FeatureJWTServer Sessions
StorageClient-side (no server state)Server-side (database/Redis)
ScalabilityExcellent (any server can verify)Requires shared session store
RevocationDifficult (token valid until expiry)Easy (delete session from store)
SizeLarger (payload is in every request)Small cookie (session ID only)
Best forAPIs, SPAs, microservicesTraditional server-rendered apps

JWT Security Pitfalls

Inspect any JWT's payload with our JWT Decoder — paste a token to see exactly what data it exposes.

When to Use JWTs

When NOT to use JWTs: Traditional server-rendered apps where sessions work fine, scenarios requiring instant revocation (admin force-logout), or when the payload would be very large.

Frequently Asked Questions

Can I decode a JWT without the secret key? expand_more
Yes — the header and payload are Base64url-encoded, not encrypted. Anyone can decode and read them. The secret key is only needed to verify the signature (prove the token hasn't been tampered with). This is why you should never put sensitive data in the JWT payload. Our JWT Decoder lets you inspect any token's contents instantly.
What is the difference between HS256 and RS256? expand_more
HS256 (HMAC-SHA256) uses a shared secret key — the same key signs and verifies the token. RS256 (RSA-SHA256) uses an asymmetric key pair — a private key signs the token and a public key verifies it. RS256 is preferred for distributed systems where multiple services need to verify tokens without sharing the signing secret.
How long should a JWT access token last? expand_more
Industry standard is 15-30 minutes for access tokens. Short expiration limits the window of abuse if a token is compromised. Use a refresh token (stored in an httpOnly cookie, expiring in 7-30 days) to issue new access tokens without requiring the user to re-login.
Is JWT the same as OAuth? expand_more
No. JWT is a token format — a way to encode and sign data. OAuth 2.0 is an authorization framework — a set of rules for granting access. OAuth 2.0 often uses JWTs as its token format, but it can also use opaque tokens. JWT can also be used outside of OAuth for simple API authentication.

Related guides

build Try These Free Tools

Put what you learned into practice — no signup required

MD

Meet Dhameliya

Founder & Product Manager

Computer Engineering graduate with 3+ years in IT. Meet built Utility Spark to create privacy-first browser tools that process your data locally — no uploads, no accounts.