JSON Web Token encode/decode helpers for Node.js using JWK keys.
The package signs and verifies compact JWT tokens and validates standard registered claims during decode.
npm install tr-jwtNode.js >=24.0.0 is required.
const { encode, decode } = require('tr-jwt');Creates and signs a compact JWT.
alg: signing algorithmjwk: signing keydata: payload objectopts: optional claims merged into the payload
Supported algorithms:
HS256,HS384,HS512ES256,ES384,ES512RS256,RS384,RS512ML-DSA-44,ML-DSA-65,ML-DSA-87
Supported registered claims:
expnbfiatisssubaudjti
Example:
const { encode, decode } = require('tr-jwt');
const { macKeyGen } = require('tr-jwk');
const key = macKeyGen('ES256');
const token = encode('ES256', key, { sub: '1234' }, { exp: Math.floor(Date.now() / 1000) + 3600 });
const payload = decode(token, key);Behavior:
- Header is always emitted as
{ typ: "JWT", alg, ... } kidis copied from the JWK when present- claim values are validated before signing
Verifies the compact JWT signature and returns the decoded payload object.
Accepted verification keys:
- HMAC:
octJWK - EC: public or private EC JWK
- RSA: public or private RSA JWK
- ML-DSA: public or private JWK
Validation performed during decode:
- signature verification
- JSON header and payload decoding
- claim shape validation
expexpiry checknbfnot-before check
- Payload must be a JSON object.
- Compact serialization only.
audmay be a string or an array of strings.
Timo J. Rinne tri@iki.fi — https://github.com/rinne/
Copyright © 2023–2026 Timo J. Rinne tri@iki.fi.
See COPYING for the full MIT license text.
MIT License