@fastify/cookie
Plugin for Fastify that adds support for reading and setting cookies, including signed cookies.
@fastify/cookie
FastifyPlugin for Fastify that adds support for reading and setting cookies, including signed cookies.
Fit
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
Fastify does not parse cookies out of the box, so reading session identifiers or preferences from Cookie headers and setting Set-Cookie responses requires manual header handling on every route.
What It Does
@fastify/cookie decorates requests with parsed cookies and replies with a setCookie/clearCookie API, and supports signed cookies for tamper detection. It integrates with Fastify's plugin and hook system.
Installation
npm install @fastify/cookieUsage Example
import Fastify from "fastify";
import cookie from "@fastify/cookie";
const app = Fastify();
app.register(cookie, { secret: process.env.COOKIE_SECRET });
app.get("/", (req, reply) => {
const visits = Number(req.cookies.visits ?? 0) + 1;
reply.setCookie("visits", String(visits), { path: "/" });
reply.send({ visits });
});
app.listen({ port: 3000 });Related packages
Auto-load Fastify plugins from a directory — organize routes and plugins as files and let the framework discover them automatically.
@fastify/corsFastifyCORS plugin for Fastify — configure cross-origin resource sharing with full support for preflight requests and dynamic origins.
@fastify/helmetFastifySecurity headers plugin for Fastify — sets HTTP headers that protect against common web vulnerabilities like XSS, clickjacking, and MIME sniffing.
@fastify/jwtFastifyJWT authentication plugin for Fastify — sign, verify, and decode tokens with a decorator-based API that integrates with Fastify's request lifecycle.
@fastify/multipartFastifyMultipart form data / file upload plugin for Fastify — stream files directly to storage without buffering in memory.