Guards scoped to a router module
Declare middleware, interceptors and guards once on a router module, and they cover every endpoint it owns, nested routers included. Plain NestJS has no such scope.
Declare Zod schemas and a handler, and get validation in and out and a typed axios or React Query client.
import { endpoint, z } from 'nestjs-endpoints';
import { OrdersService } from './orders.service.js';
export default endpoint.mutation({
input: z.object({
productId: z.string(),
quantity: z.number().int().min(1),
}),
output: z.object({
id: z.number(),
status: z.enum(['pending', 'paid', 'cancelled']),
}),
inject: { orders: OrdersService },
handler: ({ input, orders }) => orders.create(input),
});What that one file gives your app
POST /orders/createDerived from where the file sits. There is no decorator to write and no module to add it to.400 Validation failedBodies, search parameters and path parameters are parsed before the handler runs, and the response is checked before it is sent.input.quantity: numberThe handler's arguments are inferred from the schemas, and its return value is checked against output.client.orders.create()A generated axios call and a React Query hook, typed from the same schemas.Server
The body, search parameters and path parameters arrive parsed and typed, and so does everything you inject. You write no DTO classes, and no type annotations to keep in step with a schema.
export default endpoint.query({
query: z.object({ page: z.coerce.number().default(1) }),
input: z.object({
status: z.enum(['pending', 'paid', 'cancelled']).optional(),
}),
// ...
handler: ({ input, query }) => { // ...
},
});Server
The handler's return value is checked against the output schema as you type, and validated again before it is sent. Keys the schema doesn't declare are stripped, so nothing leaves the API that its contract doesn't name.
export default endpoint.query({
// ...
output: z.object({
id: z.number(),
status: z.enum(['pending', 'paid', 'cancelled']),
}),
handler: () => ({
id: 1,
status: 'refunded', }),
});Client
Generate an axios or React Query client from the same schemas. Routes, request bodies and responses autocomplete, and a server change that breaks a caller is a type error in the front end.
// axios
const client = createApiClient({ baseURL: '/api' });
await client.orders.create({
body: { productId: 'sku_42', quantity: '2' },});// React Query
function Order({ orderId }) {
// ...
const client = useApiClient();
const order = client.orders.useView({ path: { orderId } });
order.data?.
}NestJS
Each endpoint is an ordinary controller class. Providers, guards, interceptors, exception filters and the testing module work the way they do in any Nest app, on Express, Fastify or the Node adapter. Add one endpoint beside the controllers you already have, and move the rest when it suits you.
injectinjectOnRequestdecoratorsexport default endpoint.mutation({
input: z.object({ orderId: z.number(), reason: z.string() }),
output: z.object({ refunded: z.boolean() }),
// Any method decorator: guards, interceptors, metadata.
decorators: [UseGuards(AdminGuard)],
// Providers, as a constructor would inject them.
inject: { payments: PaymentsService },
// Per-request values, as parameter decorators would.
injectOnRequest: { user: decorated<User>(CurrentUser()) },
handler: ({ input, payments, user }) =>
payments.refund(input.orderId, input.reason, user.id),
});Performance
Validating a request and its response costs less in an endpoint than in a NestJS controller with NestJS's own schema validation. The same API, with the same Zod schemas, serves about a quarter more requests per second where both are validated, and the same number where nothing is.
GET with path and search parameters+27%
nestjs-endpoints40,635
NestJS32,021
POST with a JSON body+25%
nestjs-endpoints33,938
NestJS27,107
GET with no schemasLevel
nestjs-endpoints49,837
NestJS49,744
A router finds the endpoint files in a folder and derives each URL from the file's path. A router.module.ts gives a folder its own providers, guards and interceptors, and its own namespace in the generated client. Services, guards and tests sit beside the endpoints they belong to.
Install the package, then add NestjsEndpoints.forRoot to your app module. It needs Node.js 24.22, NestJS 12, TypeScript 6 and Zod 4.1 or later, in an ESM project.
Get startednpm install nestjs-endpoints zodpnpm add nestjs-endpoints zodyarn add nestjs-endpoints zod