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.
- Schemas you already have: The route schemas that validate requests also type the client.
- No glue package:
@fastify/swaggerand the HyperFetch CLI are all you need. - One command to resync: Change a schema, regenerate, and the compiler shows every call to fix.
How it fits together
| Step | Where | What happens |
|---|---|---|
| 1 | Fastify | @fastify/swagger builds the OpenAPI document from your route schemas |
| 2 | Terminal | @hyper-fetch/cli generate reads it and writes src/api/fastify.sdk.ts |
| 3 | Frontend | createSdk(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
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 });
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:
{
"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.
- 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.
@fastify/swaggerturns your route schemas into an OpenAPI document.@hyper-fetch/cli generateturns that document into a typed SDK.createSdk(client)gives you typed requests forsend(),useFetchanduseSubmit.
