A JWT, or JSON Web Token, is a compact, URL-safe string that carries a set of claims in JSON form and is protected by a digital signature or message authentication code. Defined in RFC 7519, it is the most common token format for authentication and authorization in APIs, where a client presents the token with each request instead of a session identifier.
The value of a JWT is that any service holding the right key can verify the token locally, without querying a central session store. That property makes JWTs attractive for distributed systems, but it also creates revocation and expiry challenges that every implementation must address deliberately.
The three-part structure of a JWT
A signed JWT consists of three Base64URL-encoded parts separated by dots: a header, a payload and a signature. The result looks like xxxxx.yyyyy.zzzzz and fits easily into an HTTP header, a cookie or a query parameter.
The table below describes what each part contains and what it is used for.
| Part | Content | Purpose |
|---|---|---|
| Header | JSON with alg and usually typ, sometimes kid | Tells the verifier which algorithm and key to use |
| Payload | JSON object of claims such as sub, exp, iss | Carries identity, permissions and validity data |
| Signature | Signature or MAC over header and payload | Proves integrity and origin of the token |
A crucial point is that the payload of a signed token is encoded, not encrypted. Anyone who intercepts the token can decode and read the claims, so sensitive data must never be placed in the payload unless the token is encrypted with JWE.
How JWT authentication works in an API
In a typical flow, an authorization server issues the token after the user signs in, and the client then sends it to resource servers in the Authorization: Bearer header. Each API verifies the signature, checks the time-based claims and reads the permissions before serving the request.
- The user authenticates with credentials, a single sign-on provider or another factor.
- The issuer creates a JWT with claims such as subject, audience and expiration time, then signs it.
- The client stores the token and attaches it to every API call.
- The API validates the signature, issuer, audience and expiry, then authorizes the action based on the claims.
- When the access token expires, the client obtains a new one, usually with a refresh token.
Because verification needs only a key and a clock, JWTs support stateless authorization across microservices and API gateways. In OAuth 2.0 deployments they commonly serve as access tokens, and in OpenID Connect the ID token is always a JWT.
Registered claims and signing algorithms
RFC 7519 defines a set of registered claims with short three-letter names. They are optional by specification, but a secure API should require and check the ones that bound the token in time and scope.
| Claim | Meaning | Validation rule |
|---|---|---|
| iss | Issuer of the token | Must match a trusted issuer |
| sub | Subject, usually the user or client identifier | Used to identify the principal |
| aud | Intended audience | Must include the receiving API |
| exp | Expiration time in Unix seconds | Reject if the current time is later |
| nbf | Not-before time | Reject if the current time is earlier |
| iat | Issued-at time | Useful for age checks and auditing |
| jti | Unique token identifier | Enables replay detection and deny lists |
The signing algorithm determines who can create and who can verify tokens. Symmetric HMAC algorithms use one shared secret for both operations, while asymmetric algorithms let the issuer keep the private key and publish the public key, often through a JWKS endpoint.
| Algorithm | Type | Typical use |
|---|---|---|
| HS256 | HMAC with SHA-256, shared secret | Single service that both issues and verifies |
| RS256 | RSA signature with SHA-256 | Widely supported default for identity providers |
| ES256 | ECDSA on P-256 with SHA-256 | Smaller signatures, mobile and modern stacks |
| EdDSA | Ed25519 signature | Fast, deterministic signing in newer libraries |
JWT compared with other API credentials
A JWT is one of several ways an API can identify a caller. The choice depends on whether instant revocation or verification without a lookup matters more.
| Credential | Verification | Strengths | Weaknesses |
|---|---|---|---|
| JWT | Local signature check | Stateless, carries claims, scales well | Hard to revoke before expiry |
| Opaque access token | Lookup or introspection call | Instant revocation, no data exposure | Extra network call per request |
| Session cookie | Server-side session store | Simple for browser applications | Shared state across servers |
| API key | Database lookup of a static key | Easy for server-to-server access | Long-lived, no user context |
Many architectures combine these approaches: an API gateway introspects an opaque token at the edge and forwards a short-lived internal JWT to backend services, gaining revocation control externally and stateless checks internally.
Security pitfalls and best practices
Most JWT vulnerabilities come from lax validation in the verifying code, not from weaknesses in the format itself. Libraries that trust the header too much have historically allowed attackers to forge tokens.
- Reject the none algorithm and pin the expected algorithm on the server instead of reading it from the token header.
- Prevent algorithm confusion, in which an RS256 public key is misused as an HS256 secret.
- Keep access token lifetimes short, typically 5 to 15 minutes, and rotate refresh tokens on use.
- Use a strong HMAC secret of at least 256 bits, never a guessable password.
- Always check aud, iss and exp, and allow only a small clock skew of seconds.
- Store browser tokens in HttpOnly, Secure cookies where possible to reduce exposure to cross-site scripting.
Revocation remains the hardest problem. Practical options include short expiry with refresh tokens, a deny list keyed on jti, or a per-user token version stored centrally, each of which reintroduces some state in exchange for control.
In everyday language
A JWT works like a stamped wristband at a festival. At the entrance, staff check your ticket once and give you a wristband that says who you are and which areas you may enter, with a tamper-proof seal.
After that, every stage door only looks at the wristband and its seal instead of calling the ticket office. That is fast, but if a wristband is stolen, it keeps working until it expires, which is why good systems make the wristbands expire quickly.
Seen in the real world
A mobile banking app sends users through an identity provider when they sign in. The provider returns an RS256-signed access token valid for 10 minutes and a refresh token valid for 30 days. The access token contains the user identifier, the audience of the payments API and scopes such as reading balances and initiating transfers.
Each of the bank’s twelve backend services downloads the provider’s public keys once an hour and verifies every request locally, which removes thousands of introspection calls per second. When a customer reports a stolen phone, support revokes the refresh token, and the stolen session stops working within ten minutes at most, a window the security team accepted as a deliberate trade-off between performance and immediate revocation.