Express
Express has no schema of its own, so you describe your routes once as an OpenAPI document. The HyperFetch CLI reads that document and writes a typed SDK, so your frontend calls every route with typed responses, payloads, params and query params. There is no Express package to install: the OpenAPI document is the contract.
- No glue package: Any tool that writes OpenAPI for Express works with the HyperFetch CLI.
- One source of truth: The document that powers your API docs also types your client.
- One command to resync: Change a route, regenerate, and the compiler shows every call to fix.
How it fits together
| Step | Where | What happens |
|---|---|---|
| 1 | Express | swagger-jsdoc builds the OpenAPI document and a route serves it |
| 2 | Terminal | @hyper-fetch/cli generate reads it and writes src/api/express.sdk.ts |
| 3 | Frontend | createSdk(client) gives you typed requests for every route |
1. Serve the OpenAPI document
Pick the tool that matches how you write routes. This example uses swagger-jsdoc, which reads @openapi comments
above each route:
npm install swagger-jsdoc
import express from "express";
import swaggerJsdoc from "swagger-jsdoc";
const app = express();
const openapi = swaggerJsdoc({
definition: {
openapi: "3.0.0",
info: { title: "Shop API", version: "1.0.0" },
},
apis: ["./src/routes/*.ts"],
});
app.get("/openapi.json", (_req, res) => res.json(openapi));
app.listen(3000);
/**
* @openapi
* /users/{id}:
* get:
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* responses:
* 200:
* content:
* application/json:
* schema:
* type: object
* properties:
* id: { type: string }
* name: { type: string }
*/
router.get("/users/:id", getUser);
- tsoa generates the routes and the OpenAPI document from typed controller classes (
tsoa spec). - @asteasolutions/zod-to-openapi builds the document from the zod schemas you already validate with.
Any of them works, as long as the result is an OpenAPI 3 JSON document.
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 express.sdk.ts
It writes express.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/express.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 express.sdk.ts -o"
}
}
-o overwrites the old file without asking. To generate without a running server, write openapi to a file in a small script
(writeFileSync("openapi.json", JSON.stringify(openapi))) and pass that path to --url instead.
- Your Express API and your frontend live in different repositories.
- You are documenting the API with Swagger anyway and want the client to follow it.
- You want a changed response shape to break the frontend build, not production.
- Express gets an OpenAPI document from
swagger-jsdoc, tsoa or zod schemas. @hyper-fetch/cli generateturns that document into a typed SDK.createSdk(client)gives you typed requests forsend(),useFetchanduseSubmit.
