jsonwebtoken
Create, verify and decode JSON Web Tokens for authentication and secure data transmission.
2. Maintained
github.com/auth0/node-jsonwebtoken3. Used by agents' projects
Dependency in 149 public GitHub projects set up for AI coding agents that we scanned (2.4%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What jsonwebtoken does
jsonwebtoken is the plain sign/verify/decode JWT library for Node.js, and since version 9 it refuses unsigned tokens, short RSA keys and mismatched key types unless you opt back in by name.
jsonwebtoken popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How jsonwebtoken is used
- jsonwebtoken is a dependency in 149 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (3.1% of the 4,867 JavaScript/TypeScript ones), #3 of 141 in auth and security libraries.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 452; those are left out of every share on this page.
- The most used alternative in our scan is jose (108 projects).
- Projects that use it most often also use dotenv (70.5%), Express (60.4%), cors (53.7%).
- It is more common in projects set up for Devin Desktop (9.3%) than for OpenAI Codex (1.5%).
By coding agent
Often used with
jsonwebtoken in practice
Strengths
In v9, <code>verify()</code> rejects unsigned tokens unless you list <code>none</code> in <code>options.algorithms</code>. Signing rejects RSA keys under 2048 bits unless you set <code>allowInsecureKeySizes</code>.
<code>verify</code> takes expected values for audience, issuer, JWT ID, subject and nonce, plus <code>maxAge</code> and <code>clockTolerance</code>. The audience can be matched against strings or regular expressions.
Failures come back as <code>TokenExpiredError</code>, <code>NotBeforeError</code> or <code>JsonWebTokenError</code>. Each has a fixed message such as <code>'jwt expired'</code> or <code>'jwt malformed'</code>.
In callback mode, the key argument can be a function that looks up the key from the token header. The README shows this with <code>jwks-rsa</code> and the header's <code>kid</code>.
Watch out for
<code>jwt.decode</code> reads the payload without validating the signature. The README warns against using it for untrusted messages and points to <code>jwt.verify</code> instead.
Even after verification, the README says a payload from an untrusted source must be sanitized, and you should only read the properties you expect.
<code>expiresIn</code>, <code>notBefore</code> and <code>maxAge</code> treat numbers as seconds. A unitless string is read as milliseconds, so <code>"120"</code> means <code>"120ms"</code>.
Sign a token, then verify it (synchronous)
var jwt = require('jsonwebtoken');
var token = jwt.sign({ foo: 'bar' }, 'shhhhh');
// verify a token symmetric - synchronous
var decoded = jwt.verify(token, 'shhhhh');
console.log(decoded.foo) // bar
// invalid token - synchronous
try {
var decoded = jwt.verify(token, 'wrong-secret');
} catch(err) {
// err
}More about jsonwebtoken
Head to head
- jsonwebtoken vs jose used together in 7 projects
Packages around it
- dotenv: env variables
- express: web framework
- cors: CORS middleware
- bcryptjs: password hashing
- zod: schema validation
- axios: HTTP client
More in Auth and security libraries
Alternatives
jsonwebtoken This page
Comprehensive JWT and JWK handling for Node.js and web runtimes.
Modular authentication middleware for Node.js supporting OAuth, JWT and local strategies.
Get started
Install it.
npm install jsonwebtokenRules for your agent
## jsonwebtoken
- Use `jwt.verify` for any token from a client or external request. Never use `jwt.decode`, which does not check the signature.
- Pass `algorithms` explicitly to `verify` (for example `['RS256']`). Do not add `'none'` unless unsigned tokens are really intended.
- Treat the decoded payload as user input. Read only the properties you expect.
- Give `expiresIn`, `notBefore` and `maxAge` a number of seconds or a string with units (`'1h'`, `'7d'`). A unitless string like `'120'` means milliseconds.
- Put a claim either in the payload (`exp`, `nbf`, `aud`, `sub`, `iss`) or in the options, never both. Use an object literal payload so claims get set.
- Use RSA keys of at least 2048 bits, and never sign HS* tokens with an asymmetric key. Do not set `allowInsecureKeySizes` or `allowInvalidAsymmetricKeyTypes` in new code.
- Handle `TokenExpiredError`, `NotBeforeError` and `JsonWebTokenError` separately when mapping failures to responses.