π A modern and performant starter for building REST APIs with Fastify and TypeScript.
- β‘ Fastify - Ultra-fast web framework
- π· TypeScript - Static typing and better DX
- π§ Hot Reload - Automatic reload in development
- π Modular structure - Clean organization of plugins and routes
- π‘οΈ Type Safety - Custom TypeScript declarations
- Node.js (version 18 or higher)
- npm or yarn
# Clone the project
git clone <your-repo>
cd fastify-api-ts
# Install dependencies
npm install
# Run in development mode (hot reload)
npm run dev
The API will be available at http://localhost:8888
# Build the project
npm run build
# Run in production
npm start
Script | Description |
---|---|
npm run dev |
Starts the server in development mode with hot reload |
npm run build |
Compiles TypeScript to JavaScript in /dist |
npm start |
Starts the server in production mode |
npm test |
Runs tests (to be configured) |
src/
βββ app.ts # Fastify application configuration
βββ server.ts # Server entry point
βββ plugins/ # Custom Fastify plugins
β βββ sensible.ts # Plugin for HTTP errors
β βββ support.ts # Global utilities
βββ routes/ # Route definitions
β βββ root.ts # Root route
β βββ example/ # Example routes
βββ types/ # TypeScript declarations
βββ fastify.d.ts # Fastify type extensions
- Create a file in
src/routes/
- Export a Fastify plugin function:
import { FastifyInstance } from 'fastify';
export default async function (fastify: FastifyInstance) {
fastify.get('/hello', async (request, reply) => {
return { message: 'Hello World!' };
});
}
- Routes are automatically loaded by Fastify
Add your plugins in src/plugins/
and register them in src/app.ts
.
Create a .env
file at the root:
PORT=8888
NODE_ENV=development