app.decorateReply()

Adds a property or method to every reply object.

Since Fastify 5 Spec ↗

Syntax

app.decorateReply(name, value)

Parameters

NameTypeRequiredDescription
name string No The property name added to FastifyReply.
value any No A function or value available on every reply.

Returns

FastifyInstance — The instance, for chaining.

Examples

import Fastify from 'fastify';

const app = Fastify();

app.decorateReply('ok', function (data: unknown) {
  return this.code(200).send({ data });
});

declare module 'fastify' {
  interface FastifyReply {
    ok(data: unknown): FastifyReply;
  }
}

app.get('/', async (_req, reply) => reply.ok({ hello: 'world' }));

Notes

Use a regular function (not an arrow) so `this` refers to the reply. This is handy for shared response envelopes. Augment FastifyReply for typing.