# Fastify

A Fastify 5 REST API starter for Node.js 22 in plain JavaScript, for developers who want a lean API skeleton with a single runtime dependency. It includes routes written as Fastify plugins, structured request logs from Fastify's built-in pino logger, a /health check and node:test tests that use app.inject().

- Framework: Fastify (JavaScript)
- Category: Starters / REST APIs
- Creates: a server app
- Plan: Free
- Price: Free
- Author: Light Cloud
- Page: https://templates.light-cloud.com/starters/rest-apis/fastify-api-starter
- Updated: 2026-11-21

## Features

- GET / lists the endpoints and GET /health returns status, an ISO timestamp and process uptime
- Routes live as Fastify plugins in src/routes/, each one registered in buildApp() in src/app.js
- Structured JSON request logs from Fastify's built-in pino logger, switched on in src/server.js
- Binds 0.0.0.0 rather than Fastify's localhost default, so the server is reachable inside a container
- Tests call buildApp() and app.inject(), so they run without opening a port
- Unknown routes answer Fastify's JSON 404 with message, error and statusCode fields
- Graceful shutdown on SIGTERM and SIGINT through app.close()
- One runtime dependency, fastify; listens on PORT, defaulting to 3000

## What you need

- Free plan: it runs as a server app, and the Free plan includes 3 server apps
- To run it locally: Node.js 22 or newer and npm
- No API keys: it starts with nothing to configure

## About

Everything goes through buildApp(): the server calls it with the logger on and listens, while the tests call it with no logger and inject requests straight into the router. That split, plus Fastify's own JSON error replies, is why the starter needs no extra error middleware. The first change is usually a new plugin file next to src/routes/root.js with a JSON schema on each route, which Fastify uses to validate bodies and answer 400 on a mismatch. On Light Cloud it runs npm start in a container on the PORT the platform sets, and each push to your copy redeploys it.

## Questions

### How do I add a new route in Fastify?

Create a plugin in src/routes/ shaped like root.js, an async function (app) that calls app.get, app.post and so on, then register it in src/app.js with app.register(require('./routes/things')).

### How do I validate a request body in Fastify?

Pass a JSON schema in the route options, for example { schema: { body: { type: 'object', required: ['name'] } } }. Fastify checks the body and answers 400 in JSON when it does not match.

### Does this Fastify starter include CORS?

No. Install @fastify/cors and register it in buildApp() with the origins you want to allow.

### Why does my Fastify app not respond inside Docker?

Fastify listens on localhost by default, which nothing outside the container can reach. This starter sets the host to 0.0.0.0 in src/config.js, so platform traffic gets in.

### Can I run this Fastify API somewhere other than Light Cloud?

Yes. It has no Light Cloud code: any host with Node.js 22 runs it with npm ci --omit=dev and npm start, with PORT set.
