Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/elysiajs/documentation/llms.txt

Use this file to discover all available pages before exploring further.

The @elysia/jwt plugin adds JWT support to Elysia handlers. It injects a jwt object into the handler context that exposes sign and verify methods, built on top of jose.

Installation

bun add @elysia/jwt

Basic usage

Handler values

The plugin adds a jwt object to the handler context under the name specified in the name config option.

jwt.sign(payload)

Signs a JWT payload and returns a token string.
sign: (payload: JWTPayloadSpec) => Promise<string>

jwt.verify(token)

Verifies a token against the configured secret. Returns the decoded payload on success, or false if verification fails.
verify: (token: string) => Promise<JWTPayloadSpec | false>

Configuration

The plugin extends config options from jose.

name

The property name under which the jwt object is injected into the handler context. Use different names to register multiple JWT instances with different secrets or algorithms in the same app:
app
    .use(
        jwt({
            name: 'myJWTNamespace',
            secret: process.env.JWT_SECRETS!
        })
    )
    .get('/sign/:name', ({ myJWTNamespace, params }) => {
        return myJWTNamespace.sign(params)
    })

secret

The private key used to sign and verify JWT payloads.

schema

A TypeBox schema for strict type validation of the JWT payload.

alg

Default: 'HS256' The signing algorithm. Supported values from jose: HS256 HS384 HS512 PS256 PS384 PS512 RS256 RS384 RS512 ES256 ES256K ES384 ES512 EdDSA

exp

The expiration time for signed tokens. Accepts a time string such as '7d', '2h', or '30m'.

Setting an expiration date

const app = new Elysia()
    .use(
        jwt({
            name: 'jwt',
            secret: 'kunikuzushi',
            exp: '7d'
        })
    )
    .get('/sign/:name', async ({ jwt, params }) => jwt.sign(params))

Standard JWT claims

All standard RFC7519 claims are supported as config options and as payload fields:
ClaimDescription
issIssuer — identifies who issued the JWT
subSubject — identifies the principal of the JWT
audAudience — recipients the JWT is intended for
expExpiration time
nbfNot before — earliest time to accept the token
iatIssued at
jtiJWT ID — unique identifier for the token
Keep your secret in an environment variable. Never hard-code secrets in source code.

Build docs developers (and LLMs) love