reply.jwtSign()

Signs a JWT payload, available when the JWT plugin is registered.

Since Fastify 5 Spec ↗

Syntax

reply.jwtSign(payload, options?): Promise<string>

Parameters

NameTypeRequiredDescription
payload object No The claims to encode in the token.
options object No Sign options such as expiresIn.

Returns

Promise<string> — The signed JWT string.

Examples

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

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

app.post('/login', async (req, reply) => {
  const token = await reply.jwtSign(
    { sub: 'u_1' },
    { expiresIn: '15m' },
  );
  return { token };
});

Notes

Requires @fastify/jwt. reply.jwtSign() and request.jwtVerify() are added by the plugin. Configure the secret/keys and default sign options at registration time.