Skip to main content
Version: v10.0.0

Fastify

Fastify routes already carry JSON schemas for params, bodies and responses. @fastify/swagger turns them into an OpenAPI document, and the HyperFetch CLI turns that document into a typed SDK. The schemas that validate your requests on the server then type them on the client. There is no Fastify package to install.


Purpose
  1. Schemas you already have: The route schemas that validate requests also type the client.
  2. No glue package: @fastify/swagger and the HyperFetch CLI are all you need.
  3. One command to resync: Change a schema, regenerate, and the compiler shows every call to fix.

How it fits together​

StepWhereWhat happens
1Fastify@fastify/swagger builds the OpenAPI document from your route schemas
2Terminal@hyper-fetch/cli generate reads it and writes src/api/fastify.sdk.ts
3FrontendcreateSdk(client) gives you typed requests for every route

1. Serve the OpenAPI document​

Register @fastify/swagger before your routes, so it sees all of them:

npm install @fastify/swagger
server.ts
import Fastify from "fastify";
import swagger from "@fastify/swagger";

const app = Fastify();

await app.register(swagger, {
openapi: { info: { title: "Shop API", version: "1.0.0" } },
});

app.get<{ Params: { id: string } }>(
"/users/:id",
{
schema: {
params: {
type: "object",
properties: { id: { type: "string" } },
required: ["id"],
},
response: {
200: {
type: "object",
properties: { id: { type: "string" }, name: { type: "string" } },
},
},
},
},
async (request) => getUser(request.params.id),
);

// The document itself, hidden from the document
app.get("/openapi.json", { schema: { hide: true } }, () => app.swagger());

await app.listen({ port: 3000 });
TypeBox and zod

Schemas written with TypeBox are JSON schemas, so they need nothing extra. With fastify-type-provider-zod, pass its jsonSchemaTransform as the transform option of @fastify/swagger.


2. Generate the SDK​

Run the CLI in your frontend project:

npx @hyper-fetch/cli@latest generate --template openapi --url http://localhost:3000/openapi.json --fileName fastify.sdk.ts

It writes fastify.sdk.ts to src/api. On the first run it also creates api.json with that location. Every path becomes a key and every method a $-prefixed key on it, so GET /users/{id} is sdk.users.$id.$get.


3. Call your API​

import { createClient } from "@hyper-fetch/core";

import { createSdk } from "./api/fastify.sdk";

export const client = createClient({ url: "http://localhost:3000" });
export const sdk = createSdk(client);


import { sdk } from "./client";

// Typed from the response schema of GET /users/:id
const { data, error } = await sdk.users.$id.$get.setParams({ id: "42" }).send();

The same requests work with useFetch and useSubmit from @hyper-fetch/react, with caching, retries and deduplication included.


Keep the SDK in sync​

Install the CLI in the frontend project and add a script, so regenerating is one command locally and in CI:

package.json
{
"scripts": {
"api:generate": "hyper-fetch generate -t openapi -u http://localhost:3000/openapi.json -f fastify.sdk.ts -o"
}
}

-o overwrites the old file without asking. To generate without a running server, call await app.ready() and write app.swagger() to a file in a small script, then pass that path to --url instead.

When it's helpful
  • Your route schemas are already complete and you want the frontend to use them.
  • The Fastify API and the frontend don't share a TypeScript project.
  • You want a changed response schema to break the frontend build, not production.
OpenAPI TypeScript client
Everything the generated SDK can do, from React hooks to regenerating it in CI.
Summary
  • @fastify/swagger turns your route schemas into an OpenAPI document.
  • @hyper-fetch/cli generate turns that document into a typed SDK.
  • createSdk(client) gives you typed requests for send(), useFetch and useSubmit.