jsonwebtoken
Node.js library to sign and verify JSON Web Tokens with HMAC, RSA, and ECDSA algorithms.
Official Documentation ↗v9.0. Check the official docs for the latest API surface.Installation
npm install jsonwebtokenpnpm add jsonwebtokenyarn add jsonwebtokenMethods
No methods match this filter.
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.
- Private keys below this size will be rejected with an error. (https://github.com/auth0/node-jsonwebtoken)
Security notes
Quoted from the official documentation (https://github.com/auth0/node-jsonwebtoken); this is not a complete threat model.
- Please do not report security vulnerabilities on the public GitHub issue tracker. (https://github.com/auth0/node-jsonwebtoken)
- The Responsible Disclosure Program details the procedure for disclosing security issues. (https://github.com/auth0/node-jsonwebtoken)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.