DEV Community

Cover image for Introduction to Fastify — Fastify with TypeScript (part 1/18)
Antonio Tripodi
Antonio Tripodi

Posted on

Introduction to Fastify — Fastify with TypeScript (part 1/18)

Introduction to Fastify

I'm part of the Fastify core team, and this is the first chapter of the "Fastify with TypeScript" series, a practical guide to building solid backend APIs from scratch. In this article we'll look at what Fastify is, why it's one of the strongest choices today for building Node.js APIs, and what's coming up in the rest of the series.

Who is this for? You should be comfortable with JavaScript and have used Node.js and npm before. You don't need any experience with Fastify, and the TypeScript we use is explained as we go.


What is Fastify?

Fastify is a Node.js web framework designed from day one around two goals: speed and great developer experience. Compared to alternatives like Express, it offers:

  • extremely fast JSON serialization thanks to fast-json-stringify
  • request validation based on JSON Schema (powered by ajv)
  • a plugin system with automatic encapsulation
  • first-class async/await support (no next() callbacks)
  • structured logging out of the box (Pino)
  • a large collection of official plugins maintained by the @fastify team

Ten lines to a running server

Start from an empty folder and install Fastify in it:

mkdir hello-fastify && cd hello-fastify
npm init -y
npm pkg set type=module
npm install fastify
Enter fullscreen mode Exit fullscreen mode

Then create hello.ts in that same folder:

// hello.ts
import Fastify from 'fastify';

type Greeting = { hello: string };

const app = Fastify({ logger: true });

app.get('/', async (): Promise<Greeting> => ({ hello: 'world' }));

await app.listen({ port: 3000 });
Enter fullscreen mode Exit fullscreen mode

And run it:

node hello.ts
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:3000 and you'll get {"hello":"world"}. If Node answers Cannot find package 'fastify' instead, you ran the file from a folder where npm install fastify hasn't been run: Node looks for packages in the node_modules next to the file. "type": "module" tells Node the project uses ES modules, which is what import and top-level await need.

That's it: no build step, no ts-node, no bundler. This is real TypeScript (the Greeting type and the Promise<Greeting> annotation are not valid JavaScript), yet Node.js runs the .ts file directly by stripping the type annotations at load time (flag-free since Node 22.19 / 23.6 / 24). Notice also that the handler simply returns an object: Fastify serializes it, sets Content-Type, and sends it.

One thing to keep in mind: Node removes the types, it doesn't check them. Your editor checks them as you write, and from chapter 3 on we'll add a typecheck script that does it from the command line.

What this series uses

Tool Version
Node.js 22.19+ (24 LTS recommended)
TypeScript 6.x
Fastify 5.x
TypeBox 1.x (typebox package)

Every example is written in TypeScript with strict mode on, and is compatible with Node's type stripping, so you'll never need a compiler in development — only when building for production.

What we'll build

Over 18 chapters we'll grow that 10-line server into a production-ready user API: typed routes, runtime validation, JWT authentication with roles, PostgreSQL, tests that don't need a network port, security headers, rate limiting, structured logging, and finally a Docker image with graceful shutdown.

  1. Introduction to Fastify (you are here)
  2. Fastify core concepts
  3. Project setup
  4. Your first server (app.ts, server.ts, config.ts)
  5. Routing and request handling
  6. TypeScript generics in routes
  7. Validation with schemas (TypeBox)
  8. Plugins and encapsulation
  9. Hooks and the request lifecycle
  10. Error handling
  11. Decorators
  12. Authentication and authorization
  13. Database
  14. Security: CORS, Helmet, rate limiting
  15. Testing
  16. Logging
  17. Performance
  18. Production deployment

➡️ Next chapter: Fastify core concepts

This article is part of the "Fastify with TypeScript" series — chapter 1 of 18.

Top comments (0)