Fastify Documentation
website·Indexed 39 days ago
https://fastify.dev/docs/latest/A minimal and modular web framework for Node.js. This documentation provides guides on getting started, plugin development, benchmarking, and serverless deployment, alongside technical references for the Request and Reply objects, lifecycle hooks, encapsulation, and migration guides for v3, v4, and v5.
What's inside Fastify
- Type Providers are a TypeScript feature in Fastify that allow the framework to automatically infer type information from inline JSON Schemas. This removes the need to manually specify generic arguments on routes or maintain separate TypeScript types for every schema in a project.
Overview of Fastify Type Providers
v5.3.xType Providers are a TypeScript feature in Fastify that allow the framework to infer type information directly from inline JSON Schemas. This eliminates the need to manually specify generic arguments on routes or maintain separate TypeScript types for every schema in a project.Overview of Fastify Type Providers
v5.12.xType Providers are a TypeScript feature in Fastify that enables the inference of type information directly from inline JSON Schemas. This removes the need to manually specify generic arguments on routes or maintain separate TypeScript types for every schema in a project.Overview of Fastify Validation and Serialization
v5.12.xFastify uses a schema-based approach for request validation and response serialization to ensure high performance. It relies on two primary dependencies: Ajv v8 for validating incoming requests and fast-json-stringify for serializing response bodies. Both use JSON Schema (Draft 7).
Critical Security Warning: Schema definitions should be treated as application code. Because validation and serialization use
new Function(), providing user-generated schemas is unsafe. Additionally, avoid using the$asyncAjv feature for initial validation to prevent potential Denial of Service (DoS) attacks; use Fastify hooks likepreHandlerfor asynchronous tasks instead.Overview of Fastify Type Providers
v5.2.xType Providers are a TypeScript feature in Fastify that enables automatic type inference from inline JSON Schemas. This removes the need to manually specify generic arguments on routes and reduces the overhead of maintaining separate TypeScript types for every schema in a project.Overview of the Fastify Reply object
v3.29.xTheReplyobject is the second parameter of a Fastify handler function. It provides a set of methods and properties to manage the HTTP response, including setting status codes, manipulating headers, and sending the final payload. It also provides access to the server instance, the original request, and the request context.fastify.get('/', options, function (request, reply) { reply .code(200) .header('Content-Type', 'application/json; charset=utf-8') .send({ hello: 'world' }) })Overview of Fastify Type Providers
v5.11.xType Providers are a TypeScript feature in Fastify that enables automatic type inference from inline JSON Schemas. This reduces the need to manually define and maintain associated TypeScript types for every schema in a project. Official provider packages follow the@fastify/type-provider-{provider-name}naming convention.Overview of Fastify Type Providers
v4.29.xType Providers are a TypeScript-only feature in Fastify that allow for static type inference directly from inline JSON Schemas. This removes the need to manually define and maintain separate TypeScript types for every schema used in a project. They are implemented as additional packages that wrap different inference libraries.Overview of Fastify Validation and Serialization
Fastify uses a schema-based approach to validate incoming requests and serialize outgoing responses. It compiles JSON Schemas (Draft 7) into high-performance functions.
Key dependencies:
- Ajv v8: Handles request validation.
- fast-json-stringify: Handles response body serialization.
Security Warning: Schema definitions should be treated as application code. Because validation and serialization use
new Function(), providing user-generated schemas is unsafe. Additionally, avoid using Ajv's$asyncfeature for initial validation to prevent Denial of Service (DoS) attacks; use Fastify hooks likepreHandlerfor asynchronous tasks instead.Overview of Fastify Validation and Serialization
v3.29.xFastify uses a schema-based approach to validate incoming requests and serialize outgoing responses. It compiles JSON Schemas into high-performance functions.
- Validation: Handled by Ajv v6. Validation is only attempted if the content type is
application-json. - Serialization: Handled by fast-json-stringify.
Security Warning: Schema definitions are evaluated using
new Function(). Never use user-provided schemas. Additionally, avoid using the$asyncAjv feature during the first validation strategy to prevent Denial of Service (DoS) attacks; use Fastify hooks likepreHandlerfor async tasks instead.- Validation: Handled by Ajv v6. Validation is only attempted if the content type is
Overview of Fastify Type Providers
v5.4.xType Providers are a TypeScript feature in Fastify that enables automatic type inference from inline JSON Schemas. This reduces the need to manually specify generic arguments on routes or maintain separate TypeScript types for every schema in a project.Overview of Fastify Decorators
v5.12.xThe Decorators API allows developers to customize core Fastify objects (server instance, request, and reply objects) by attaching functions, plain objects, or native types. This API is synchronous. To register decorations asynchronously, use the
registerAPI withfastify-plugin.Using decorators is preferred over mutating objects inside hooks (e.g.,
req.user = 'Bob') because it defines the object shape before instantiation, preventing JavaScript engine deoptimization.// Recommended: Define the shape first fastify.decorateRequest('user', '') fastify.addHook('preHandler', (req, reply, done) => { req.user = 'Bob Dylan' done() })