# NestJS (/docs/guides/next/frameworks/nestjs)

> For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL.

Set up Prisma Next in a NestJS app with create-prisma, from scaffold to seeded API to a live deploy on Prisma Compute.

Location: Guides > Next > Frameworks > NestJS

## Introduction [#introduction]

This guide shows you how to use Prisma Next in a NestJS API. You scaffold a project with a users controller backed by Prisma Next, initialize the schema, query it over HTTP, and deploy the API to [Prisma Compute](https://www.prisma.io/docs/compute).

Every command below was run end to end against a live [Prisma Postgres](https://www.prisma.io/docs/postgres) database.

## Prerequisites [#prerequisites]

* [Node.js](https://nodejs.org) 24 or later
* A PostgreSQL connection string, or nothing at all: the scaffold can create the database for you

## Use with your agent [#use-with-your-agent]

Prefer to delegate this guide? Copy the prompt and hand it to your coding agent:

```text
Create a new NestJS API with Prisma Next, seed it, and deploy it to Prisma Compute.

1. Scaffold: `npx create-prisma@next create my-nest-api --template nest --provider postgres --prisma-postgres --yes` (or pass `--database-url "<url>"` instead of `--prisma-postgres` if I give you a connection string).
2. In `my-nest-api`, run `npm run db:init`, then `npm run db:seed`. If seeding fails with `Cannot read properties of undefined (reading 'where')`, change `db.orm.User` to `db.orm.public.User` in `src/prisma/seed.ts` and `src/prisma/users.ts`, then rerun.
3. Start `npm run dev` in the background, wait until it reports ready, and verify `curl http://localhost:3000/users` returns the seeded users; stop the dev server once verified. If it returns a 500 and the server logs `Cannot read properties of undefined (reading 'findAll')`, add explicit injection tokens: `@Inject(UsersService)` on the constructor parameter in `src/users.controller.ts` and `@Inject(PrismaService)` in `src/users.service.ts` (import `Inject` from `@nestjs/common`), then retry.
4. Deploy: check `npx @prisma/cli@latest auth whoami`; if I am not signed in, stop and ask me to run `npx @prisma/cli@latest auth login`. Then run `npx @prisma/cli@latest app deploy --create-project my-nest-api --env .env` and verify the live URL's /users endpoint.

Use the installed Prisma Next skills.
```

## 1. Scaffold and enter the project [#1-scaffold-and-enter-the-project]

  

#### bun

```bash
bunx create-prisma@next create my-nest-api --template nest --provider postgres
cd my-nest-api
```

#### pnpm

```bash
pnpm dlx create-prisma@next create my-nest-api --template nest --provider postgres
cd my-nest-api
```

#### yarn

```bash
yarn dlx create-prisma@next create my-nest-api --template nest --provider postgres
cd my-nest-api
```

#### npm

```bash
npx create-prisma@next create my-nest-api --template nest --provider postgres
cd my-nest-api
```

Pick Prisma Postgres at the database prompt to have a database created for you, or paste your own `DATABASE_URL`. The scaffold writes your connection string to `.env`, generates the NestJS app with Prisma Next wired in, installs dependencies, and emits the contract your queries are type-checked against.

## 2. Initialize and seed the database [#2-initialize-and-seed-the-database]

  

#### bun

```bash
bun run db:init
bun run db:seed
```

#### pnpm

```bash
pnpm run db:init
pnpm run db:seed
```

#### yarn

```bash
yarn db:init
yarn db:seed
```

#### npm

```bash
npm run db:init
npm run db:seed
```

```text no-copy
"summary": "Applied 5 operation(s) across 1 space(s), database signed"
Seeded 3 users.
```

`db:init` applies your schema (`src/prisma/contract.prisma`) to the database and signs it; the seed gives the first request something to return.

> [!NOTE]
> Seeing `Cannot read properties of undefined (reading 'where')`? The current `nest` template still generates the older unqualified query form. Update `db.orm.User` to `db.orm.public.User` in `src/prisma/seed.ts` and `src/prisma/users.ts`, then rerun `db:seed`.

## 3. Run and verify [#3-run-and-verify]

  

#### bun

```bash
bun run dev
```

#### pnpm

```bash
pnpm run dev
```

#### yarn

```bash
yarn dev
```

#### npm

```bash
npm run dev
```

The server starts on port 3000 (set `PORT` to change it). Query the API:

```bash
curl http://localhost:3000/users
```

```json no-copy
[
  { "id": "1", "email": "alice@prisma.io", "username": "alice", "name": "Alice", "createdAt": "2026-07-24T10:55:54.156Z" },
  { "id": "2", "email": "bob@prisma.io", "username": "bob", "name": "Bob", "createdAt": "2026-07-24T10:55:54.192Z" },
  { "id": "3", "email": "carol@prisma.io", "username": "carol", "name": "Carol", "createdAt": "2026-07-24T10:55:54.228Z" }
]
```

> [!NOTE]
> Getting a 500 with `Cannot read properties of undefined (reading 'findAll')` in the server logs? The `dev` script runs through `tsx`, which does not emit the decorator metadata NestJS constructor injection relies on, so the controller receives no service instance. Add explicit injection tokens: in `src/users.controller.ts`, change the constructor parameter to `@Inject(UsersService) private readonly usersService: UsersService`, and in `src/users.service.ts` to `@Inject(PrismaService) private readonly prisma: PrismaService` (import `Inject` from `@nestjs/common` in both files). The watcher reloads and the route works.

## Where things live [#where-things-live]

* `src/users.controller.ts`: the `GET /users` route
* `src/users.service.ts`: the service the controller calls
* `src/prisma.service.ts`: the injectable that exposes the Prisma Next client and query helpers
* `src/prisma/users.ts`: the `listUsers` query the service runs
* `src/prisma/db.ts`: the Prisma Next client itself

Model access is namespace-qualified on PostgreSQL: `db.orm.public.User`. The [Prisma Next overview](https://www.prisma.io/docs/orm/next) covers the contract-first model behind it.

## 4. Deploy to Prisma Compute [#4-deploy-to-prisma-compute]

NestJS is supported on [Prisma Compute](https://www.prisma.io/docs/compute), so the API can go from your terminal to a live URL in one command. Sign in once (it opens a browser):

  

#### bun

```bash
bunx @prisma/cli@latest auth login
```

#### pnpm

```bash
pnpm dlx @prisma/cli@latest auth login
```

#### yarn

```bash
yarn dlx @prisma/cli@latest auth login
```

#### npm

```bash
npx @prisma/cli@latest auth login
```

Then deploy from the project directory. The `--env .env` flag passes your `DATABASE_URL` to the deployment, so the live API talks to the same database:

  

#### bun

```bash
bunx @prisma/cli@latest app deploy --env .env
```

#### pnpm

```bash
pnpm dlx @prisma/cli@latest app deploy --env .env
```

#### yarn

```bash
yarn dlx @prisma/cli@latest app deploy --env .env
```

#### npm

```bash
npx @prisma/cli@latest app deploy --env .env
```

```text no-copy
First deploy of "my-nest-api" -- promoting to production.

Build settings
  Build Command     npm run build  · package.json scripts.build
  Output Directory  dist           · NestJS output

Building locally...
  Built      1.1 MB
Uploading...
Deploying...
Live in 9.6s
https://<your-app>.ewr.prisma.build
```

Verify the live endpoint returns the seeded users:

```bash
curl https://<your-app>.ewr.prisma.build/users
```

The first deploy asks you to pick or create a project (pass `--create-project my-nest-api` to skip the prompt), pins the directory to it in a gitignored `.prisma/local.json`, and promotes to production. For previews per Git branch and deploy-on-push, see [Deploy your first app](https://www.prisma.io/docs/prisma-compute/deploy).

## Common gotchas [#common-gotchas]

> [!WARNING]
> In a long-running server, don't call `db.runtime().close()` in request handlers; the client's connection pool is shared across requests. Close it only on process shutdown.

## Prompt your coding agent [#prompt-your-coding-agent]

The scaffold installs [Prisma Next skills](https://www.prisma.io/docs/ai/tools/skills#available-skills-for-prisma-next) for your coding agent. Prompts that map to this guide:

* "Using the prisma-next-queries skill, add GET /users/:id to the users controller that returns one user or a 404."
* "Expose GET /users/:id/posts using the Post model that ships with the starter contract."
* "Add a POST /users route that creates a user from the request body and returns it with a 201."

## Next steps [#next-steps]

* Change the schema in `src/prisma/contract.prisma`, then run `npm run contract:emit` and `npm run db:update`.
* [Learn the fundamentals](https://www.prisma.io/docs/orm/next/fundamentals/reading-data): filtering, sorting, pagination, and writes.
* [Read the Prisma Next overview](https://www.prisma.io/docs/orm/next) for the concepts behind contracts and typed queries.

## Related pages

- [`Astro`](https://www.prisma.io/docs/guides/next/frameworks/astro): Set up Prisma Next in an Astro app with create-prisma, from scaffold to rendered data, and deploy it to Prisma Compute.
- [`Elysia`](https://www.prisma.io/docs/guides/next/frameworks/elysia): Build an Elysia API on Prisma Next with the elysia template and deploy it to Prisma Compute.
- [`Hono`](https://www.prisma.io/docs/guides/next/frameworks/hono): Build a Hono API on Prisma Next with the hono template, add your own routes, and deploy it to Prisma Compute.
- [`Next.js`](https://www.prisma.io/docs/guides/next/frameworks/nextjs): Set up Prisma Next in a Next.js app with create-prisma, from scaffold to rendered data, and deploy it to Prisma Compute.
- [`Nuxt`](https://www.prisma.io/docs/guides/next/frameworks/nuxt): Set up Prisma Next in a Nuxt app with create-prisma, from scaffold to rendered data, and deploy it to Prisma Compute.