JWT best practices for APIs
November 8, 2026 · 16 min read
APIs that accept Bearer JWTs inherit the issuer’s security posture plus their own verification rules. Best practices are not exotic: TLS everywhere, algorithm allowlists, mandatory exp, issuer and audience checks, and secrets managed like production credentials - not copied from blog posts.
HTTPS and Authorization header
Terminate TLS 1.2+ at the load balancer. Reject tokens on plain HTTP redirects. Use Authorization: Bearer only; avoid query strings. Set Cache-Control: no-store on responses that echo tokens in error JSON.
Validate signature and claims on every request
- Pin allowed algorithms; reject none and unexpected HS256 when expecting RS256.
- Verify
issagainst configured issuer URLs. - Match
audto your API identifier or resource indicator. - Check
exp, optionalnbf, and scope/role claims for the route.
Short expiration and rotate secrets
Keep access tokens short-lived. Rotate HMAC secrets and RSA keys with kid in JWKS. Document overlap windows. Pair with refresh token rotation for interactive clients.
Claim design for APIs
Put user id in sub, permissions in scope or namespaced private claims. Avoid PII you would not log. Size matters: large JWTs hurt mobile networks and header limits.
await jwtVerify(token, JWKS, {
issuer: "https://auth.example.com/",
audience: "https://api.example.com",
algorithms: ["RS256"],
});
Operations and incident response
Log validation failure reasons without printing full tokens. Use JWT decoder locally for staging reproduction. Validate new keys with PEM validator before deploy.
FAQ
- Should APIs use introspection instead of JWT?
- Opaque tokens plus introspection add latency but simplify revocation. JWT fits high-throughput internal APIs with short TTL.
- Is HS256 OK for microservices?
- Only if one service issues and all verifiers share a strong secret via secure config. Prefer RS256 for many verifiers.
- Do GraphQL APIs differ?
- Same Bearer rules at HTTP layer; resolver-level authorization still needs claim checks per field.
- How do I test validation?
- Golden tokens with wrong aud, expired exp, and bad signature in contract tests.
Related: JWT security checklist · Common JWT misconfigurations · JWT aud and iss · JWT expiration practices