---
title: "Build Well-Documented and Authenticated APIs in Node.js with Fastify"
description: "This walkthrough builds and deploys a Node.js API with Fastify, covering a public directory endpoint and a JWT-protected profile endpoint backed by Heroku Postgres. It uses Fastify’s plugin architecture, including a custom database plugin that reads DATABASE_URL, creates a PostgreSQL pool, and decorates the server with db.query; @fastify/autoload registers plugins from the plugins directory. Authentication uses a JWT signed with an RSA256 private key, while the public key validates requests, and Fastify Swagger plugins generate an OpenAPI specification and UI that Postman can import as an API client. After local testing, the project seeds 50 database records, configures the RSA public key, and deploys through Heroku CLI commands, where the /directory endpoint returns usernames and the deployed API is shown running."
---

# Build Well-Documented and Authenticated APIs in Node.js with Fastify

[Heroku](https://yomu.fyi/company/heroku) · Julián Duque · Apr 29, 2024

**Type:** Tutorial

## Summary

This walkthrough builds and deploys a Node.js API with Fastify, covering a public directory endpoint and a JWT-protected profile endpoint backed by Heroku Postgres. It uses Fastify’s plugin architecture, including a custom database plugin that reads DATABASE\_URL, creates a PostgreSQL pool, and decorates the server with db.query; @fastify/autoload registers plugins from the plugins directory. Authentication uses a JWT signed with an RSA256 private key, while the public key validates requests, and Fastify Swagger plugins generate an OpenAPI specification and UI that Postman can import as an API client. After local testing, the project seeds 50 database records, configures the RSA public key, and deploys through Heroku CLI commands, where the /directory endpoint returns usernames and the deployed API is shown running.

## Context

The walkthrough presents Fastify as a Node.js alternative to Express for building an API that is authenticated, documented with OpenAPI, consumable through Postman, and deployable to Heroku.

## Approach / What changed

The implementation uses Fastify plugins for PostgreSQL queries and JWT authentication, Fastify Swagger plugins for OpenAPI output and a UI, Postman for importing the specification, and Heroku CLI commands for provisioning, configuration, database seeding, and deployment.

## Takeaways

- A custom Fastify database plugin creates a PostgreSQL pool from DATABASE\_URL and exposes a db.query method by decorating the Fastify instance.
- The /directory route is public, while /profile requires a JWT containing a username and returns additional information for that user.
- Fastify Swagger plugins generate an OpenAPI specification and UI, and the specification can be imported into Postman to create an API client.

**Tags:** [Authentication](https://yomu.fyi/topic/authentication), [Fastify](https://yomu.fyi/topic/fastify), [Heroku](https://yomu.fyi/topic/heroku), [Node.js](https://yomu.fyi/topic/node-js), [OpenAPI](https://yomu.fyi/topic/openapi)

- Source: [Heroku](https://www.heroku.com/blog/build-openapi-apis-nodejs-fastify)
- Source URL: https://www.heroku.com/blog/build-openapi-apis-nodejs-fastify
- Ingested by Yomu: 2026-09-09T20:05:16.468Z

[Read original post](https://www.heroku.com/blog/build-openapi-apis-nodejs-fastify)
