# NestJS (/docs/guides/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 8 in a NestJS app with create-prisma, from scaffold to seeded API to a live deploy on Prisma Compute.

Location: Guides > Frameworks > NestJS

## Introduction [#introduction]

This guide shows you how to use Prisma 8 in a NestJS API. You scaffold a project with a users controller backed by Prisma 8, 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: `npx create-db@latest` can create a Prisma Postgres database for you

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

To delegate this guide to your coding agent, copy the prompt below and hand it over:

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

1. Scaffold: `npx create-prisma@latest create my-nest-api --template nest --provider postgres --yes`. Then run `npx prisma@latest init` in `my-nest-api` so the Prisma agent skills are installed and stay current, and use them. Get a database connection string: use the one I give you, or create a Prisma Postgres database with `npx create-db@latest` and show me the claim URL it prints. Export it as `DATABASE_URL` in the shell; the generated scripts read the environment variable, not `.env`.
2. In `my-nest-api`, run `npm run db:init` with `DATABASE_URL` exported. Sample users are seeded automatically on the app's first query; there is no separate seed script.
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@latest auth whoami`; if I am not signed in, stop and ask me to run `npx prisma@latest auth login`. Then run `npm run build` followed by `npx prisma@latest deploy module.ts` and verify the live URL's /users endpoint. The deployed app provisions and seeds its own Prisma Postgres database; do not pass the local DATABASE_URL. If the deploy fails with `HostedStateBootstrapError`, a project with the module's name exists in my workspace but its hosted state cannot be verified; re-run the deploy with `--name <a unique name>`.

Use the installed Prisma 8 skills.
```

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

  

#### bun

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

#### pnpm

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

#### yarn

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

#### npm

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

Answer the prompts for contract authoring style and package manager. The scaffold generates the NestJS app with Prisma 8 wired in, installs dependencies, and emits the contract your queries are type-checked against.

Next, set the database connection for the local steps. Use your own PostgreSQL connection string, or create a Prisma Postgres database with `npx create-db@latest`; it prints a connection string and a claim URL you can open to keep the database. Export the variable in the shell you work in; the generated scripts read the environment variable, not `.env`:

```bash
export DATABASE_URL="<your connection string>"
```

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

  

#### bun

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

#### pnpm

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

#### yarn

```bash
yarn db:init
```

#### npm

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

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

If `db:init` stops with `Connection terminated unexpectedly`, a database you just created is still starting; wait a few seconds and run it again. The command is safe to repeat and reports `Applied 0 operation(s)` when there is nothing left to do.

`db:init` applies your schema (`src/prisma/contract.prisma`) to the database and signs it. Sample users are seeded automatically the first time the app queries the database.

## 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 8 client and query helpers
* `src/prisma/users.ts`: the `listUsers` query the service runs
* `src/prisma/db.ts`: the Prisma 8 client itself

Model access is namespace-qualified on PostgreSQL: `db.orm.public.User`. The [Prisma 8 overview](https://www.prisma.io/docs/orm) 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). The scaffold declares the app for [Prisma Composer](https://www.prisma.io/docs/composer) in `module.ts` and `service.ts`, so deploying is building and handing that declaration to the CLI. Sign in once (it opens a browser):

  

#### bun

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

#### pnpm

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

#### yarn

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

#### npm

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

Then build and deploy from the project directory:

  

#### bun

```bash
bun run build
bunx prisma@latest deploy module.ts
```

#### pnpm

```bash
pnpm run build
pnpm dlx prisma@latest deploy module.ts
```

#### yarn

```bash
yarn build
yarn dlx prisma@latest deploy module.ts
```

#### npm

```bash
npm run build
npx prisma@latest deploy module.ts
```

```text no-copy
my-nest-api
├─ database   postgres-database db_abc123
└─ app        compute-service cps_abc123
              https://xyz.ewr.prisma.build
```

The deploy creates a project named after your module in your workspace, and re-running the deploy reuses it: the CLI finds the hosted state it stored on the first run and converges the project to your module. If a project with that name exists but the CLI cannot identify or verify its stored state (one left behind by a different checkout, for example), the deploy stops with `HostedStateBootstrapError`; deploy under another name with `--name <unique-name>`, or rename the module in `module.ts`. The deploy also provisions its own Prisma Postgres database on the platform, declared in `module.ts`; the `DATABASE_URL` from your local steps is not involved, and the deployed database seeds on the app's first query. Verify the live endpoint returns the seeded users:

```bash
curl https://xyz.ewr.prisma.build/users
```

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]

Run [`npx prisma@latest init`](https://www.prisma.io/docs/cli/init) once to install the [Prisma 8 skills](https://www.prisma.io/docs/ai/tools/skills#available-skills-for-prisma-8) for your coding agent and keep them matching your installed packages. Prompts that map to this guide:

* "Using the prisma-8 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/fundamentals/reading-data): filtering, sorting, pagination, and writes.
* [Read the Prisma 8 overview](https://www.prisma.io/docs/orm) for the concepts behind contracts and typed queries.

## Related pages

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