@fastify/jwt

Adds JWT signing and verification helpers to the instance, request, and reply.

Since Fastify 5 Spec ↗

Syntax

app.register(jwt, { secret })

Parameters

NameTypeRequiredDescription
options object No secret or keys, sign/verify options, and cookie support.

Returns

Promise<void> — Resolves when the plugin is registered.

Examples

import Fastify from 'fastify';
import jwt from '@fastify/jwt';

const app = Fastify();
await app.register(jwt, { secret: process.env.JWT_SECRET! });

app.decorate('auth', async (req, reply) => {
  try {
    await req.jwtVerify();
  } catch {
    reply.code(401).send({ error: 'Unauthorized' });
  }
});

app.get('/me', { preHandler: [app.auth] }, async (req) => req.user);

Notes

Provides app.jwt, reply.jwtSign(), and request.jwtVerify(). Combine with a preHandler guard for protected routes. Supports reading tokens from a cookie via the `cookie` option.