Skip to content

nestjs-endpointsEnd‑to‑end type safety, built on NestJS controllers.

Declare Zod schemas and a handler, and get validation in and out and a typed axios or React Query client.

src/endpoints/orders/create.endpoint.ts
ts
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

  • RoutePOST /orders/createDerived from where the file sits. There is no decorator to write and no module to add it to.
  • Validation400 Validation failedBodies, search parameters and path parameters are parsed before the handler runs, and the response is checked before it is sent.
  • Typesinput.quantity: numberThe handler's arguments are inferred from the schemas, and its return value is checked against output.
  • Clientclient.orders.create()A generated axios call and a React Query hook, typed from the same schemas.

Server

Your schemas type the handler.

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.

src/endpoints/orders/list.endpoint.ts
ts
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
}) => {
input: {
    status?: "pending" | "paid" | "cancelled" | undefined;
}
// ... }, });

Server

A wrong response doesn't compile.

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.

src/endpoints/orders/$orderId.view.endpoint.ts
ts
export default 
endpoint
.
query
({
// ...
output
:
z
.
object
({
id
:
z
.
number
(),
status
:
z
.
enum
(['pending', 'paid', 'cancelled']),
}),
handler
: () => ({
id
: 1,
status: 'refunded',
Type 'string' is not assignable to type '"pending" | "paid" | "cancelled"'.
}), });

Client

Your front end knows the API.

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.

ts
// axios
const 
client
=
createApiClient
({
baseURL
: '/api' });
await
client
.
orders
.
create
({
body
: {
productId
: 'sku_42', quantity: '2' },
Type 'string' is not assignable to type 'number'.
});
tsx
// React Query
function 
Order
({
orderId
}) {
// ... const
client
=
useApiClient
();
const
order
=
client
.
orders
.
useView
({
path
: {
orderId
} });
order
.
data
?.
}

NestJS

It's still 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.

inject
Providers, as a constructor takes them.
injectOnRequest
@Req(), @Session() and your own parameter decorators.
decorators
@UseGuards(), @UseInterceptors(), @SetMetadata().
src/endpoints/admin/refund.endpoint.ts
ts
export 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

Faster than a controller that validates.

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.

  1. GET with path and search parameters+27%

    nestjs-endpoints40,635

    NestJS32,021

  2. POST with a JSON body+25%

    nestjs-endpoints33,938

    NestJS27,107

  3. GET with no schemasLevel

    nestjs-endpoints49,837

    NestJS49,744

Requests per second on one CPU core. Longer is better.

Your folders are your API.

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.

src/endpoints/RouteGenerated client
health.endpoint.tsGET /healthclient.health()
orders/A folder is a path segment.
router.module.tsThe folder's providers and guards, and the client's "orders" namespace.
orders.service.tsNot an endpoint file, so not a route.
create.endpoint.tsPOST /orders/createclient.orders.create()
list.endpoint.tsPOST /orders/listclient.orders.list()
$orderId.view.endpoint.tsPOST /orders/:orderId/viewclient.orders.view()
$orderId.cancel.endpoint.tsPOST /orders/:orderId/cancelclient.orders.cancel()
File-based routing

What else is in the box

Add it to a NestJS app.

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 started
bash
npm install nestjs-endpoints zod
bash
pnpm add nestjs-endpoints zod
bash
yarn add nestjs-endpoints zod

Released under the MIT License.