Fastify
Mount a Kizuna API on a Fastify application.
@kizunajs/fastify serves a Kizuna API from a Fastify application.
Requires Fastify >= 5.
The adapter
fastifyAdapter() is what you name in kizuna.config.ts. Naming it is what gives every handler on this API Fastify's own request and reply, typed.
Declare a route
Name Fastify in your config
Handler context
Each handler receives { params, query, body, headers, request, reply }. The request is Fastify's FastifyRequest and reply is FastifyReply, useful when you need raw headers, cookies, or anything else Fastify exposes.
Guards
A guard sits on the identity it authenticates, and receives the credential extracted and typed, alongside the native request/reply. See the Authentication guide.
Middleware
For request-scoped values handlers need, use request context. For other middleware, such as logging and rate limiting, use Fastify's own hooks (app.addHook('preHandler', ...)). Authentication belongs in guards.
Options
| Option | Default | Description |
|---|---|---|
responseValidation | false | Validate handler return values against response schemas. Enable in development. |
formatError | none | Reshape error (>= 400) response bytes for migrating clients. Most don't need it (use Problem Details extension members). See Migrating an existing API. |
The same options go on fastifyKizuna when you register the plugin yourself:
Type-safe request properties
Middleware that sets custom properties on request, such as a request id, needs Fastify's declaration merging to extend the FastifyRequest interface. Authentication data does not: guards hand handlers typed context directly.
Both middleware and handlers are then typed:
The adapter also sets request.kizunaRoute on every matched request, holding the route that matched.
Streams
A stream response hijacks the reply and is piped to reply.raw after its headers are flushed. The generator's signal fires when the raw response closes before the stream has finished.