Skip to content

Guide · Open source

How to use Fastify: install, define routes and run a server

Updated · written with AI from the project's own documentation and checked against our catalogue data

Fastify is a web framework for Node.js that you use to build web servers and APIs. This guide shows you how to install it, define a route that returns data, start a server and use the features the project highlights, such as schema validation, plugins and Pino logging. By the end you will have a small working server and know where to look next.

Fastifyfastify/fastify · ★ 37.3k · MIT

Quick start

  1. 1Check Node.js. Make sure Node.js is installed, since Fastify runs only on Node.js.
  2. 2Install Fastify. Run npm i fastify in your project, or run npm init fastify to generate a new project.
  3. 3Create the server file. Import Fastify and create an instance with logger set to true.
  4. 4Declare a route. Use fastify.get to define a route that sends back a JSON object.
  5. 5Start listening. Call fastify.listen with port 3000 and handle any error in the callback.
  6. 6Run the app. Start the server with npm run dev for dev mode or npm start for production mode in a generated project.
  7. 7Add schemas and plugins. Read the Validation and Serialization and Plugins references to add schema checks and reusable code.

What you need before you install Fastify

Fastify is for Node.js only, so you need Node.js installed. The framework assumes you already know some JavaScript, so if Node.js itself is new to you, learn its basics first.

  • Node.js installed on your computer
  • Basic JavaScript familiarity
  • A terminal to run npm commands

Install Fastify in a new or existing project

To create a new Fastify project, the README uses npm init, which downloads and runs Fastify Create. To add Fastify to a project you already have, install it as a dependency.

  • New project: create a folder, move into it, then run npm init fastify
  • Existing project: run npm i fastify inside the project folder
mkdir my-app cd my-app npm init fastify

Write your first route and start the server

A route pairs a URL with a function that sends back a response. The example below creates a Fastify instance with logging turned on, declares a GET route on the root path and listens on port 3000. The README notes that an async handler can also return the data directly instead of calling reply.send.

By default, listen binds to localhost (127.0.0.1 or ::1, depending on your system). If you run Fastify in a container such as Docker, you may need to bind to 0.0.0.0, but the README warns that listening on all interfaces carries security risks.

  • Set logger: true to enable built-in logging through Pino
  • Use fastify.get to declare a route for GET requests
  • Call fastify.listen with a port to start the server
import Fastify from 'fastify' const fastify = Fastify({ logger: true }) fastify.get('/', (request, reply) => { reply.send({ hello: 'world' }) }) fastify.listen({ port: 3000 }, (err, address) => { if (err) throw err // Server is now listening on ${address} })

Use async handlers and set the response type

You can write route handlers as async functions. The README shows setting the content type and status code with reply.type and reply.code, then returning the object.

fastify.get('/', async (request, reply) => { reply.type('application/json').code(200) return { hello: 'world' } })

Validate and serialize with JSON Schema

Fastify recommends using JSON Schema to validate routes and serialize outputs. The README says this is optional, but it is one of the core features. Fastify compiles the schema into a function internally.

The project's docs cover this in Validation and Serialization, which you can find in the documentation list.

  • Validate incoming request data against a schema
  • Serialize responses so only the fields you define are sent

Organize code with plugins and learn more

Fastify has a plugin architecture and is extensible through hooks, plugins and decorators. The README points to the Plugins reference, the How to write a good plugin guide and a Testing guide. A demo repository and live examples are also linked.

Core plugins are maintained by the Fastify team, and community plugins are listed in the Ecosystem guide. Much of the functionality beyond the core comes from plugins, so check the ecosystem when you need a feature.

  • Plugins Reference: how plugins work
  • Ecosystem: core and community plugins
  • Testing and Serverless guides in the docs
  • Official demo repository for a working example

What people use it for

  • Building a JSON API that returns data from routes
  • Creating backend services in Node.js that need fast request handling
  • Validating incoming request data and shaping responses with JSON Schema
  • Organizing a larger Node.js codebase into reusable plugins

Frequently asked questions

Is Fastify free to use?

Fastify is open source and released under the MIT licence, according to the project's listing. The MIT licence lets you use it in your projects, but check the licence text for your own obligations.

What do I need to run Fastify?

You need Node.js. Fastify is a framework for Node.js only, so it cannot be used with other languages. The framework assumes basic JavaScript knowledge.

How do I install Fastify?

In an existing project, run npm i fastify. To create a new project, run npm init fastify, which uses Fastify Create and Fastify CLI under the hood.

Is Fastify faster than Express?

The README reports a synthetic hello world benchmark where Fastify handled more requests per second than Express, but it also says results depend on your application. The project advises you to benchmark your own app if performance matters.

What are the alternatives to Fastify?

The project compares itself with Express, Koa, Hapi and NestJS. It is inspired by Hapi and Express. Pick the one whose features and ecosystem fit your project.

Why does my Fastify server not respond from another machine?

By default listen binds to localhost. In a container such as Docker you may need to bind to 0.0.0.0, but the README warns this carries security risks, so only do it deliberately.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Fastify suits Node.js developers building JSON APIs or backend services who care about throughput and want schema validation and plugins. It is a poor choice for beginners without Node.js basics, for non-Node.js projects, or for teams who expect many features built in, since much functionality comes from plugins.

Install options & downloads