API Reference

jsonwebtoken

Node.js library to sign and verify JSON Web Tokens with HMAC, RSA, and ECDSA algorithms.

Official Documentation ↗
CompatibilityThis documentation covers version v9.0. Check the official docs for the latest API surface.

Installation

npmnpm install jsonwebtoken
pnpmpnpm add jsonwebtoken
yarnyarn add jsonwebtoken

Methods

jsonwebtoken is a Node.js implementation of JSON Web Tokens. It supports signing and verifying tokens with HMAC, RSA, and ECDSA algorithms, with claim validation for expiry, issuer, and audience.

Key Features

  • Algorithms: HS256, HS384, HS512, RS256, RS384, ES256
  • Claims: exp, nbf, iss, aud, sub validation
  • Asymmetric keys: RSA and ECDSA PEM keys
  • Sync and async: Callback and promise APIs
  • Error types: TokenExpiredError, JsonWebTokenError

Errors

The following points are quoted verbatim from the official documentation (https://github.com/auth0/node-jsonwebtoken). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted from the official documentation (https://github.com/auth0/node-jsonwebtoken); this is not a complete threat model.

FAQ

Should I use HS256 or RS256?
HS256 uses a shared secret and is simpler for single services. RS256 uses a public/private key pair, letting services verify tokens with the public key without sharing the secret, which is safer for distributed systems.
Where should I store JWTs on the client?
Store access tokens in memory or a service worker for short-lived use, and refresh tokens in httpOnly, Secure, SameSite cookies. Avoid localStorage for sensitive tokens, as it is vulnerable to XSS exfiltration.
How do I invalidate a JWT before expiry?
JWTs are stateless, so you cannot revoke them directly. Use short expiry times with a refresh token, or maintain a server-side blocklist or version claim checked on verify for critical invalidation.