reply.setCookie()

Sets a response cookie, available when the cookie plugin is registered.

Since Fastify 5 Spec ↗

Syntax

reply.setCookie(name, value, options?)

Parameters

NameTypeRequiredDescription
name string No The cookie name.
value string No The cookie value.
options object No Options like httpOnly, secure, sameSite, path, and signed.

Returns

FastifyReply — The reply, for chaining.

Examples

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

const app = Fastify();
await app.register(cookie, { secret: process.env.COOKIE_SECRET });

app.post('/login', async (_req, reply) => {
  reply.setCookie('sid', 'abc123', {
    httpOnly: true,
    secure: true,
    sameSite: 'lax',
    path: '/',
    signed: true,
  });
  return { ok: true };
});

Notes

Requires @fastify/cookie. Use httpOnly and secure for session cookies and `signed: true` to tamper-protect the value. reply.clearCookie() removes a cookie.