---
title: "Build Well-Documented and Authenticated APIs in Node.js with Fastify"
description: "Fastify, a plugin-based Node.js web framework, can be used to build a small API with a public Postgres-backed `/directory` route and a `/profile` route protected by RS256-signed JWTs verified against a base64-encoded public key."
url: "https://swe.quest/articles/build-well-documented-and-authenticated-apis-in-node-js-with-fastify-6b62dcc6e17b78f9"
canonical: "https://www.heroku.com/blog/build-openapi-apis-nodejs-fastify/"
source: "Heroku Engineering"
published: "2024-04-29"
difficulty: "medium"
kind: "How-to"
reading_minutes: 15
tags: ["javascript","api-design","identity-auth","postgres"]
---

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

A post on [Heroku Engineering](https://swe.quest/sources/heroku.md), published 2024-04-29, 15 min read, How-to. Original: [https://www.heroku.com/blog/build-openapi-apis-nodejs-fastify/](https://www.heroku.com/blog/build-openapi-apis-nodejs-fastify/)

## Summary

Fastify, a plugin-based Node.js web framework, can be used to build a small API with a public Postgres-backed \`/directory\` route and a \`/profile\` route protected by RS256-signed JWTs verified against a base64-encoded public key. The walkthrough builds custom plugins that decorate the Fastify instance (a \`db\` query helper using pg's Pool and a \`verifyJWT\` function via @fastify/jwt and @fastify/auth), autoloads plugins and routes, and attaches auth through the \`onRequest\` route option. It then generates an OpenAPI spec with @fastify/swagger, imports it into Postman as a client, and deploys to Heroku with Eco dynos and Heroku Postgres.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [JavaScript](https://swe.quest/tags/javascript.md) (Languages)
- [API design](https://swe.quest/tags/api-design.md) (Backend & APIs)
- [Identity & auth](https://swe.quest/tags/identity-auth.md) (Security)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)

## Prerequisites

- Node.js and npm ([easy posts on JavaScript](https://swe.quest/tags/javascript.md?difficulty=easy))
- REST API basics ([easy posts on API design](https://swe.quest/tags/api-design.md?difficulty=easy))
- JSON Web Tokens

## Keywords

Fastify, JWT, OpenAPI, Swagger, Postman, Heroku, Heroku Postgres

## Related posts

- [Build Internal Tools Using Neon, StackAuth, and Vercel](https://swe.quest/articles/build-internal-tools-using-neon-stackauth-and-vercel-451a972f29aed443.md): Neon · 2025-07-08 · Medium · 3 min read · Identity & auth, PostgreSQL, Serverless, Product engineering
  An open-source Next.js template for internal tools combines Neon serverless Postgres, Stack Auth and Vercel free tiers to provide a database, API routes, authentication, role-based access control and an admin panel.
- [Leveraging Shopify’s API to Build the Latest Marketplace Kit](https://swe.quest/articles/leveraging-shopify-s-api-to-build-the-latest-marketplace-kit-a198dca7199a1ac1.md): Shopify Engineering · 2022-04-08 · Medium · 5 min read · GraphQL, JavaScript, Identity & auth, E-commerce, API design
  Marketplace Kit 2.0 decouples its sample code from the Shopify CLI's generated Node app and splits it into a full-stack Express/React admin app and a Next.js buyer app, both open-sourced.
- [How to Build a Secure Project Management Platform with Next.js, Clerk, and Neon](https://swe.quest/articles/how-to-build-a-secure-project-management-platform-with-next-js-clerk-and-neon-a23e4da79b231f5e.md): Neon · 2025-02-22 · Medium · 29 min read · Identity & auth, React, PostgreSQL, Application security, Frontend
  A security-first tutorial for building Kozi, a collaborative project management app, using Next.js, Clerk authentication and a Neon serverless Postgres database accessed through Prisma.
- [Introducing @supabase/server](https://swe.quest/articles/introducing-supabase-server-014f7e505754650d.md): Supabase · 2026-05-06 · Medium · 8 min read · Identity & auth, Serverless, TypeScript, Developer tools
  @supabase/server, now in public beta, replaces the repeated setup code found across 25,000 analyzed Edge Functions: client creation, JWT verification, claims parsing, CORS and auth wiring.
- [Building a GPT Backed by a Heroku-Deployed API](https://swe.quest/articles/building-a-gpt-backed-by-a-heroku-deployed-api-48a6672b2eb1e607.md): Heroku Engineering · 2024-03-28 · Medium · 15 min read · AI agents, LLMs, JavaScript, PostgreSQL
  A custom OpenAI GPT can be backed by a simple Express API on Heroku that answers natural-language questions about an employee directory.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
