Skip to content

Getting started ​

nestjs-endpoints lets you write each HTTP endpoint of a NestJS app as one file: Zod schemas for what comes in and what goes out, and a handler. Each endpoint is a regular NestJS controller underneath, so the rest of the app stays as it is.

From those schemas you get validation and, if you want one, a typed client for your front end.

Requirements ​

  • Node.js 24.22 or later
  • NestJS 12 or later
  • TypeScript 6 or later
  • Zod 4.1 or later
  • An ESM project, with "type": "module" in its package.json. CommonJS is not supported.
  • Run with the Nest CLI (nest start).

Note

Node with a TypeScript loader, such as SWC's (@swc-node/register) or oxc's (@oxc-node/core), works too. See Development.

Install ​

bash
npm install nestjs-endpoints zod
bash
pnpm add nestjs-endpoints zod
bash
yarn add nestjs-endpoints zod

Installing it writes a rules file and a skill for coding agents into your repository, which point an agent at the docs inside the package. See Built for agents. pnpm and bun run a dependency's install script only once your project allows it:

yaml
# pnpm-workspace.yaml
allowBuilds:
  nestjs-endpoints: true # false: install without the agent files
jsonc
// package.json
{
  "trustedDependencies": ["nestjs-endpoints"]
}

pnpm 11 and later stop the install until the package is listed, with true or false.

Set up the app module ​

NestjsEndpoints.forRoot sets the library up, once, in your app's root module. Its router is the app's root router, which finds the endpoint files under a folder and registers them:

ts
// src/app.module.ts
import { Module } from '@nestjs/common';
import { NestjsEndpoints } from 'nestjs-endpoints';
import { OrdersService } from './endpoints/orders/orders.service.js';

@Module({
  imports: [
    NestjsEndpoints.forRoot({
      router: { rootDirectory: './endpoints', providers: [OrdersService] },
    }),
  ],
})
export class AppModule {}

rootDirectory is relative to the file that calls forRoot(). The router takes providers, imports and exports as any Nest module does.

Set up Vitest ​

A new Nest project tests with Vitest. Add nestjs-endpoints to server.deps.inline in both of its configs, vitest.config.ts and vitest.config.e2e.ts, so that the endpoint files the package loads go through Vite like the rest of your code:

ts
// vitest.config.e2e.ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  resolve: { tsconfigPaths: true },
  test: {
    globals: true,
    root: './',
    include: ['**/*.e2e-spec.ts'],
    server: { deps: { inline: ['nestjs-endpoints'] } }, 
  },
});

See Testing for how to test endpoints.

Write an endpoint ​

An endpoint file is named <name>.endpoint.ts and default-exports its endpoint:

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

Hover over input or orders in the handler: both are typed, from the schema and from the provider's class.

  • The file's path under rootDirectory is the route: POST /orders/create. See File-based routing.
  • input is the JSON body. An endpoint is a POST unless it sets method.
  • endpoint.mutation() is for writes and endpoint.query() for reads. They serve the same way; the choice decides which hooks the React Query client generates.
  • inject names the providers the handler needs. See Dependency injection.

Call it ​

Start the app:

bash
npx nest start --watch

The router finds the endpoint files when the app starts. An endpoint file you add later is served after a restart, which nest start --watch does by itself. See Development.

Then send a request:

bash
$ curl -X POST localhost:3000/orders/create \
    -H 'Content-Type: application/json' \
    -d '{"productId": "sku_42", "quantity": 2}'
{"id":1,"status":"pending"}

A body that doesn't match input is answered with a 400 and the Zod issues, before the handler runs:

bash
$ curl -X POST localhost:3000/orders/create \
    -H 'Content-Type: application/json' \
    -d '{"productId": "sku_42", "quantity": "two"}'
{
  "statusCode": 400,
  "message": "Validation failed",
  "errors": [
    {
      "expected": "number",
      "code": "invalid_type",
      "path": ["quantity"],
      "message": "Invalid input: expected number, received string"
    }
  ]
}

A complete example ​

The examples/file-based-routing folder in the repository is a small working project: a NestJS API with nested routers and namespaces, and a React front end that calls it through the generated React Query client.

Next steps ​

Released under the MIT License.