ConfigsApr 11, 2026·2 min read

Fastify — Fast & Low Overhead Web Framework for Node.js

Fastify is a fast and low overhead web framework for Node.js, focused on providing the best developer experience with least overhead and a powerful plugin architecture. Schema-driven with JSON Schema validation built in.

TL;DR
Fastify provides schema-driven validation and fast JSON serialization for Node.js APIs.
§01

What it is

Fastify is a web framework for Node.js focused on speed and low overhead. It uses JSON Schema for request validation and response serialization, achieving throughput that matches or exceeds other Node.js frameworks. The plugin architecture keeps code modular and testable. Built-in TypeScript support and automatic Swagger generation round out the developer experience.

This tool is for Node.js developers who need better performance than Express without sacrificing developer experience. It is especially suited for API-heavy applications where validation and serialization matter.

§02

How it saves time or tokens

Fastify's schema-driven approach validates inputs and serializes outputs automatically. Define a JSON Schema for your route, and Fastify rejects invalid requests before your handler runs, generates OpenAPI documentation, and serializes responses faster than JSON.stringify. The plugin system encourages reusable modules.

§03

How to use

  1. Install Fastify.
  2. Define routes with JSON Schema validation.
  3. Register plugins for functionality.
  4. Start the server.
# Create project
mkdir my-api && cd my-api
npm init -y
npm install fastify
§04

Example

const fastify = require('fastify')({ logger: true })

// Route with schema validation
fastify.post('/users', {
  schema: {
    body: {
      type: 'object',
      required: ['name', 'email'],
      properties: {
        name: { type: 'string', minLength: 1 },
        email: { type: 'string', format: 'email' }
      }
    },
    response: {
      201: {
        type: 'object',
        properties: {
          id: { type: 'string' },
          name: { type: 'string' },
          email: { type: 'string' }
        }
      }
    }
  }
}, async (request, reply) => {
  const user = {
    id: crypto.randomUUID(),
    ...request.body
  }
  return reply.code(201).send(user)
})

fastify.listen({ port: 3000 })

Invalid requests are rejected automatically with descriptive error messages.

§05

Related on TokRepo

§06

Common pitfalls

  • Fastify's plugin encapsulation can be confusing. Plugins registered in one scope are not available in another without proper decoration.
  • JSON Schema validation is strict by default. Properties not in the schema are stripped from requests. Set additionalProperties: true if you need them.
  • Response serialization requires accurate response schemas. Mismatched schemas cause serialization errors or data loss.
  • Fastify uses async/await natively. Callback-style code from Express needs refactoring.
  • The decorator pattern for dependency injection has a learning curve. Understand fastify.decorate() before building complex applications.
  • Review the official documentation before deploying to production to ensure compatibility with your specific environment and requirements.
  • Start with default settings and customize incrementally. Changing too many configuration options at once makes debugging harder.

Frequently Asked Questions

How fast is Fastify compared to Express?+

Fastify is typically 2-3x faster than Express in benchmarks for JSON response handling. The speed comes from fast JSON serialization via fast-json-stringify and efficient routing via find-my-way.

Does Fastify support TypeScript?+

Yes. Fastify has built-in TypeScript support with type-safe route definitions, plugin types, and schema inference. The types are maintained as part of the core package.

Can Fastify generate OpenAPI documentation?+

Yes. The @fastify/swagger plugin generates OpenAPI 3.0 documentation from your route schemas automatically. Add @fastify/swagger-ui for a browsable Swagger interface.

Is Fastify compatible with Express middleware?+

Not directly, but the @fastify/express plugin provides a compatibility layer. It lets you use Express middleware within Fastify, easing migration from Express.

How does the plugin system work?+

Fastify plugins are functions that receive the Fastify instance and options. They can add routes, decorators, hooks, and register sub-plugins. Encapsulation ensures plugins do not leak state into other parts of the application.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets