# Building APIs with Fastify

> Build fast, schema-driven Node.js APIs with Fastify's plugin system, built-in validation and structured logging. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/full-stack/fastify
Last updated: 2026-11-28

Build fast, schema-driven Node.js APIs with Fastify's plugin system, built-in validation and structured logging.

Intermediate · about 10 hours · difficulty 3/5

Learn first: [Node.js Runtime Essentials](https://www.internhack.in/roadmaps/tracks/full-stack/nodejs-runtime), [REST API Design](https://www.internhack.in/roadmaps/tracks/full-stack/rest-api-design)

**Fastify** is a modern alternative to Express that is faster, has first-class TypeScript support and builds validation and logging into the framework.

### What is different

- **Schemas on routes**: declare JSON schemas (or use a Zod type provider) for body, params and response. Fastify validates input and serialises output quickly, and strips fields you did not declare.
- **Plugins and encapsulation**: features register as plugins with their own routes, hooks and decorators, which keeps large apps organised.
- **Hooks** such as onRequest and preHandler replace most Express middleware.
- **Logging** with Pino is built in, producing structured JSON logs.

### When to choose it

Pick Fastify for performance-sensitive APIs, for teams who like schema-first design, or when you want response serialisation to guarantee private fields never leak. The concepts (routing, auth, errors, layering) are the same as Express.

### Pitfalls

- Trying to use Express middleware directly; you need the compatibility plugin or a Fastify-native equivalent.
- Ignoring encapsulation and registering decorators where other plugins cannot see them.
- Not defining response schemas, missing one of Fastify's main benefits.

You are ready to move on when you have a Fastify API with plugin-based feature modules, schema validation on every route, and response schemas that hide internal fields.

## Key concepts

- **Routes with schemas**
- **Plugins and encapsulation**
- **Hooks**
- **Zod type provider**
- **Response serialisation**
- **Built-in Pino logging**

## Other options for this step

- [Building APIs with Express](https://www.internhack.in/roadmaps/tracks/full-stack/express): Build a typed REST API with Express: routing, middleware, a layered structure and centralised error handling.
- [API Routes with Next.js Route Handlers](https://www.internhack.in/roadmaps/tracks/full-stack/next-route-handlers): Keep frontend and backend in one Next.js project by writing HTTP endpoints as route handlers, and know when a separate server is better.

## Mini project

Build the placement portal API in Fastify with plugins for companies and applications, request and response schemas, an auth hook and structured logs.

## Check yourself

How can a Fastify response schema prevent a passwordHash field from ever reaching the client?

## Free resources

- [Fastify Documentation](https://fastify.dev/docs/latest/) (Fastify)
- [Pino logger](https://getpino.io/) (Pino)

Previous: [Building APIs with Express](https://www.internhack.in/roadmaps/tracks/full-stack/express) · Next: [API Routes with Next.js Route Handlers](https://www.internhack.in/roadmaps/tracks/full-stack/next-route-handlers)

## More in Node.js and API Design

- [Node.js Runtime Essentials](https://www.internhack.in/roadmaps/tracks/full-stack/nodejs-runtime)
- [REST API Design](https://www.internhack.in/roadmaps/tracks/full-stack/rest-api-design)
- [Building APIs with Express](https://www.internhack.in/roadmaps/tracks/full-stack/express)
- [Building APIs with Fastify](https://www.internhack.in/roadmaps/tracks/full-stack/fastify)
- [API Routes with Next.js Route Handlers](https://www.internhack.in/roadmaps/tracks/full-stack/next-route-handlers)
- [Error Handling, Validation and API Hardening](https://www.internhack.in/roadmaps/tracks/full-stack/api-errors-and-validation)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Full-Stack Developer Roadmap](https://www.internhack.in/roadmaps/tracks/full-stack)
