Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Heroku Engineering 15 min read How-to mediumAdept

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

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.

Assumes: Node.js and npmREST API basicsJSON Web Tokens

Related posts

  • Build Internal Tools Using Neon, StackAuth, and Vercel

    Neon mediumAdept

    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. Setup takes a few steps: clone the repo, fill in the env vars, push the schema with drizzle-kit, create nested admin/user roles in Neon's Auth tab, claim admin at /setup and deploy to Vercel. Access is controlled by verified-email domain whitelisting, per-email grants for contractors and explicit blocklists, all managed from /tools/admin. New tools are added under app/tools and registered in config/app.ts.

  • Leveraging Shopify’s API to Build the Latest Marketplace Kit

    Shopify Engineering mediumAdept

    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. The admin app uses App Bridge's authenticatedFetch to attach JWT session tokens, which the server decodes with Shopify's Node API inside the Apollo Server context to load the store's access token. Running apollo-server-express instead of apollo-server lets one Express instance serve REST and GraphQL together and reuse Node middleware for rate limiting and auth, at the cost of slightly more boilerplate and no performance penalty. Custom GraphQL and REST client wrappers add project-specific User-Agent headers, and REST is kept for simple calls like product listing counts where a GraphQL query adds nothing.

  • How to Build a Secure Project Management Platform with Next.js, Clerk, and Neon

    Neon mediumAdept

    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. Security rests on three layers: Clerk middleware protecting every /app route, server actions as the only path to the database so credentials never reach the client, and cross-checking the requesting user ID against record ownership on every read and write. The guide walks through the Prisma schema, the middleware configuration, sidebar and layout components, and task server actions that verify ownership before mutating data.

  • Introducing @supabase/server

    Supabase mediumAdept

    @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. A withSupabase wrapper (or createSupabaseContext for custom error handling) takes a declarative auth mode such as 'user', 'none', 'secret', 'publishable' or a combination. It rejects unauthorized requests before the handler runs and supplies a context with a user-scoped RLS client, a service-role admin client and verified claims. It handles the new asymmetric JWT signing keys and API keys internally, so migrating needs no jose or JWKS setup. It returns a standard Request-to-Response handler that runs on Edge Functions, Vercel, Cloudflare Workers, Bun and Hono, and exposes lower-level primitives for per-route auth.

  • Building a GPT Backed by a Heroku-Deployed API

    Heroku Engineering mediumAdept

    A custom OpenAI GPT can be backed by a simple Express API on Heroku that answers natural-language questions about an employee directory. The API makes two Chat Completions calls: one turns the user's question into a PostgreSQL query using the table schema in a system prompt and JSON response format, and a second turns the returned rows into a readable answer, resending the schema because the models keep no memory between requests. The walkthrough covers bearer-token auth, environment configuration, local curl testing, Heroku deployment with Postgres, and configuring the GPT action. It also warns that all data leaves Heroku's trust boundary to reach OpenAI.

The quest for software engineering knowledge.

Look:
Layout: