Typed clients
DaloyJS ships two ways to call your API with full type-safety. Use whichever fits your consumer.
1. In-process typed client (zero codegen)
For TypeScript consumers in the same monorepo (tests, internal tools, Next.js server actions):
import { createClient } from "@daloyjs/core/client";
import { app } from "./server.js"; // your App instance
const client = createClient(app, { baseUrl: "http://localhost:3000" });
const r = await client.getBookById({ params: { id: "1" } });
// ^? { status: 200; body: { id: string; title: string } }
// | { status: 404; body: ProblemJson }
if (r.status === 200) {
console.log(r.body.title); // string, fully typed
}The client is keyed by operationId, returns a discriminated union of {status, body, headers}, and infers everything from the route definition itself. No build step.
2. Hey API SDK (cross-language, cross-repo, build-time)
For consumers outside the monorepo or in other languages, generate a fully typed fetch SDK with @hey-api/openapi-ts.
pnpm add -D @hey-api/openapi-ts// openapi-ts.config.ts
import { defineConfig } from "@hey-api/openapi-ts";
export default defineConfig({
input: "./generated/openapi.json",
output: { path: "./generated/client", format: "prettier" },
plugins: ["@hey-api/client-fetch", "@hey-api/typescript", "@hey-api/sdk"],
});// package.json
"scripts": {
"gen:openapi": "node --import tsx/esm scripts/dump-openapi.ts",
"gen:client": "openapi-ts",
"gen": "pnpm gen:openapi && pnpm gen:client"
}pnpm gen
# writes:
# generated/openapi.json
# generated/client/{client.gen.ts, sdk.gen.ts, types.gen.ts, index.ts}Using the generated SDK
import { client } from "./generated/client";
import { getBookById } from "./generated/client/sdk.gen";
client.setConfig({ baseUrl: "https://api.example.com" });
const { data, error } = await getBookById({ path: { id: "1" } });
if (error) console.error(error);
else console.log(data.title);Which one should I use?
| Use case | Pick |
|---|---|
| Same-repo TypeScript caller (tests, internal tools) | In-process createClient |
| Web app / mobile RN bundle in a separate repo | Hey API SDK |
| Non-TypeScript consumer (Python, Swift, Kotlin) | OpenAPI doc + their preferred generator |
| Public SDK for third parties | Hey API SDK, published as its own package |
Need a bigger contract to validate your generator output? Use the large fake REST demo as the stress case instead of a minimal tutorial app.