For the complete documentation index, see llms.txt. This page is also available as Markdown.

Fluent API (JwtBuilder)

JwtBuilder fluent API reference — chainable methods returned by jwtNew().

jwtNew() returns a JwtBuilder object that lets you compose a JWT one method at a time. Every claim/header method returns this so calls can be chained. Terminate the chain with .sign() (JWS) or .encrypt() (JWE) to produce the compact token string.

Claim Methods

Method
Signature
Description

subject( val )

(any) → this

Sets the sub claim.

issuer( val )

(any) → this

Sets the iss claim.

audience( val )

(string|array) → this

Sets the aud claim. Accepts a string or an array of audiences.

claim( key, val )

(string, any) → this

Sets an arbitrary custom claim.

withPayload( val )

(struct) → this

Replaces the entire payload with the given struct.

expireIn( seconds )

(numeric) → this

Sets exp as now() + seconds.

expireAt( date )

(date) → this

Sets exp to an explicit DateTime.

issuedNow()

() → this

Sets iat to now().

issuedAt( date )

(date) → this

Sets iat to an explicit DateTime.

notBefore( date )

(date) → this

Sets the nbf claim.

jti( val )

(any) → this

Sets the jti (JWT ID) claim.

Header Methods

Method
Signature
Description

header( key, val )

(string, any) → this

Sets a JOSE header field (e.g. kid, typ, cty).

Terminal Methods

Method
Signature
Returns
Description

sign( [key], [algorithm] )

(any?, string?) → string

string

Signs and returns the compact JWS string.

encrypt( [key], [keyAlgorithm], [encAlgorithm] )

(any?, string?, string?) → string

string

Encrypts and returns the compact JWE string.

When the terminal arguments are omitted, the builder falls back to the module's defaultSigningKey / defaultAlgorithm (or defaultEncryptionKey / defaultKeyAlgorithm / defaultEncAlgorithm for .encrypt()).

Examples

Last updated

Was this helpful?