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
@fastifyteam
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 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 }); And run it:
node hello.ts 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.
- Introduction to Fastify (you are here)
- Fastify core concepts
- Project setup
- Your first server (app.ts, server.ts, config.ts)
- Routing and request handling
- TypeScript generics in routes
- Validation with schemas (TypeBox)
- Plugins and encapsulation
- Hooks and the request lifecycle
- Error handling
- Decorators
- Authentication and authorization
- Database
- Security: CORS, Helmet, rate limiting
- Testing
- Logging
- Performance
- Production deployment
➡️ Next chapter: Fastify core concepts
This article is part of the "Fastify with TypeScript" series — chapter 1 of 18.