Installing & First Server

`npm i fastify` + 6 Lines of Code

Installing & First Server

Install Fastify, write a minimal server, and run it locally.

4 min read Level 1/5 #fastify#install#setup
What you'll learn
  • Install fastify with npm
  • Create an app and register a route
  • Listen on a port and return JSON

Fastify ships as a single npm package with no required peer dependencies. A working server is six lines of code.

Install

npm init -y
npm i fastify
npm i -D typescript tsx @types/node

If you are using ESM (recommended for Fastify 5), set "type": "module" in your package.json.

A Minimal Server

// src/index.ts
import Fastify from 'fastify';

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

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

await app.listen({ port: 3000 });

Run it during development with tsx --watch:

npx tsx --watch src/index.ts

Open http://localhost:3000 and you should see {"hello":"world"} plus a structured log line in the terminal.

What Just Happened

Fastify() returned an app instance with the logger enabled. app.get registered a route whose async handler returns a plain object — Fastify serialized it to JSON automatically. app.listen returned a promise we awaited, so any boot error surfaces cleanly.

The App Instance →