Guides
Shared contracts
One Zod schema validates the API, documents it, and types every client that calls it.
An endpoint is described once, in packages/, and every app reads that description. Change the
shape of the data and every app that disagrees stops compiling, before anything ships.
The schema
packages/schemas holds Zod 4 schemas. Their bounds come from a sibling constants file, so a form
and the API reject the same input for the same reason.
export const userSchema = z.object({
id: z.uuid(),
email: z.email(),
name: z.string().min(USER_NAME_MIN_LENGTH).max(USER_NAME_MAX_LENGTH),
image: z.url().nullish(),
role: z.string().nullish(),
createdAt: z.iso.datetime(),
updatedAt: z.iso.datetime(),
});The contract
packages/contracts pairs a method and a path with the schemas for its inputs and its response:
export const usersContract = {
me: defineEndpoint({
method: 'GET',
path: '/v1/users/me',
response: userSchema,
}),
} as const;The API
The controller hands the contract’s schemas to Nest. Request schemas go on the parameter, where the global validation pipe checks them, and the response schema documents the endpoint in OpenAPI:
@Get('me')
@ApiOkResponse({ description: 'The session user.', standardSchema: usersContract.me.response })
me(@Session() session: AuthSession): User {
return this.usersService.toResponse(session.user);
}A body works the same way: @Body({ schema: createPostSchema }) both validates the request and
documents it, so there is no second declaration to drift.
The clients
The web and mobile apps call the same contract through one client from
@stack88/contracts/client. The method, the path, the inputs and the parsed response type all come
from the contract:
export function getMe(signal?: AbortSignal): Promise<User> {
return callEndpoint(usersContract.me, { signal });
}Errors
A failed call throws ApiError, which carries the API’s error envelope. Branch on errorCode,
which is stable, and show message: the API writes it in the language the client sent in
Accept-Language. Never match on the message text.
Add an endpoint
pnpm kit generate api-domain writes the schema, the contract and the Nest module together. See
Code generators, and the
contracts README
for the full descriptor format.