Create a minimal REST API in NodeJS with Fastify
NicolasBrondinBernard
Learn how to create a minimal REST API in Node.js with the Fastify framework. A simple and quick guide to understanding the basics of a fast and lightweight server.

Article published on 10/03/2025, last updated on 10/08/2026
Before diving into creating our first API with Fastify, you'll need to make sure you're using a version of NodeJS equal to or greater than 20.0.0!
You can check the current version with
node —version
Project setup
If everything is in order, we can start initializing our project. Start by creating a my-first-api folder, and inside this folder, run the following commands:
$> npm init
$> npm install fastify
Then open the package.json file, and add the following line:
"type": "module"
This has nothing to do with Fastify specifically
But declaring our project as a module will enable import, which is a more modern equivalent of require.
For example, we'll write:
import Fastify from 'fastify';
// instead of
const Fastify = require('fastify');
A minimal API
Create an app.js file in your project folder, and copy-paste the following code inside it:
// We import the framework
import Fastify from 'fastify'
// We initialize it with options
const fastify = Fastify({
logger: true
})
// We declare our first route
fastify.get('/', async function handler (request, reply) {
return { hello: 'world' }
});
// And we start the server!
try {
await fastify.listen({ port: 3000 })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
Try running your api with node index.js and you should see this in your console:
Server listening at http://[::1]:3000
Try accessing your API's first route by following this link: http://localhost:3000
If your browser responds with {"hello":"world"} then your API is up and running!
No spam. Only free content, news, and ever more resources to level up your skills!
Join +1500 developers
No comments yet