GuidesAuthenticationClerk

Clerk (with Next.js)

Add Clerk authentication to a Next.js app, sync users to Prisma Postgres with a webhook, build a posts API, and deploy to Prisma Compute.

Introduction

Clerk is an auth provider: it handles sign-up, sign-in, user management, and webhooks.

In this guide you wire Clerk into a new Next.js app, keep a copy of each Clerk user in a Prisma Postgres database, expose a small posts API that only signed-in users can call, and deploy the app to Prisma Compute. Every command below was run end to end against a live database.

Using Prisma ORM 7?

Prisma ORM 8 is a release candidate. Prisma ORM 7 remains fully supported; the Prisma ORM 7 version of this guide is at /guides/v7/authentication/clerk/nextjs.

Prerequisites

  • Node.js 22.18 or newer on the 22 line, 24.11 or newer on the 24 line, or 26 or newer (24 recommended)
  • A PostgreSQL connection string, or nothing at all: npx create-db@latest creates a Prisma Postgres database for you
  • A Clerk account
  • ngrok installed and signed in (ngrok config add-authtoken), or any other tool that gives your local server a public URL, so Clerk can reach your webhook while you develop

1. Scaffold the project

Create a Next.js app with Prisma ORM already wired in. The next template generates the app, installs dependencies, and sets up the files that Prisma Compute deploys later. --no-deploy skips the scaffold's offer to deploy right away; you deploy in step 8:

npm create prisma@latest -- clerk-nextjs-prisma --template next --provider postgres --no-deploy
cd clerk-nextjs-prisma

The scaffold asks three questions, and this guide answers them as follows: write your models in Prisma Schema Language, the .prisma syntax you know from Prisma ORM 7 (Prisma ORM 8 calls the file the contract, and step 2 edits it); use npm, to match the commands on this page; and install the Prisma agent skills for the coding agents you use, or none.

Next, create a Prisma Postgres database with create-db. It needs no account and prints a connection string and a claim URL; the database is deleted after 24 hours unless you open the claim URL, which adds it to your Prisma Data Platform account:

npx create-db@latest

Export the connection string in the shell you work in. The Prisma CLI, and the npm run scripts in package.json that call it, read the environment variable, not .env:

export DATABASE_URL="<your connection string>"

2. Define the contract

2.1. Replace the models

The contract holds only models: the database connection lives in prisma.config.ts, and there are no datasource or generator blocks. The template ships demo models with seeded users. Replace the whole file with a User model that stores the Clerk user id next to the email, and a Post model that belongs to a user. Keep the // use prisma-8 line: every Prisma ORM 8 contract file starts with it.

In Prisma ORM 8 on PostgreSQL, DateTime reads and writes JavaScript Date objects. TimestamptzString is a timestamp with time zone column read and written as a plain ISO 8601 string, which is enough for a timestamp you only sort by.

src/prisma/contract.prisma
// use prisma-8

model User {
  id      Int    @id @default(autoincrement())
  clerkId String @unique
  email   String @unique
  name    String?
  posts   Post[]
}

model Post {
  id        Int               @id @default(autoincrement())
  title     String
  content   String?
  author    User              @relation(fields: [authorId], references: [id])
  authorId  Int
  createdAt TimestamptzString @default(now())
}

The template's seed helpers refer to fields that no longer exist, so delete them:

rm src/prisma/seed.ts src/prisma/users.ts

2.2. Emit the contract

Emitting the contract replaces prisma generate: it writes the types your queries are checked against into src/prisma/:

npm run contract:emit

2.3. Plan the migration

The template also ships a migration for its demo models. Delete it and plan one that creates your models. migration plan writes the migration file without touching the database; the deploy in step 8 builds the production database by replaying migrations/, so it needs this file:

rm -rf migrations
npx prisma migration plan --name init
"dir": "migrations/app/20261019T0955_init"

2.4. Create the tables

Now create the tables in the database from step 1. db:init applies the contract directly and records in the database which contract it matches (the output calls this signing). It does not replay the migration you just planned; that file is for the deploy:

npm run db:init
"summary": "Applied 6 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.

3. Set up Clerk

3.1. Create a Clerk application

Sign in to Clerk and create an application, or open one you already have. On the application's overview page, select Next.js and the Manual setup tab: it shows the install command and your API keys.

Under Configure > User & authentication > Email, turn on Sign-up with email, Require email address, and Sign-in with email. The sign-up form only shows the fields an application requires, so without Require email address the form offers social sign-in buttons only.

Install the Clerk Next.js SDK:

npm install @clerk/nextjs

Create a .env.local file in the project root and paste your keys from the dashboard. Next.js loads this file in development and at build time; the Prisma CLI does not, which is why DATABASE_URL stays exported in your shell. Use .env.local rather than .env: with output: "standalone", next build copies .env into the build output that step 8 uploads, and .env.local stays out of it:

.env.local
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=<your-publishable-key>
CLERK_SECRET_KEY=<your-secret-key>

The template's .gitignore lists only .env, so add the new file to it:

.gitignore
.env.local

3.2. Add clerkMiddleware() as the Next.js proxy

clerkMiddleware() reads the Clerk session on every request so that auth() and currentUser() work in your routes. Next.js 16 runs this kind of code from a proxy.ts file (the file was called middleware.ts in Next.js 15 and older). The template puts the app in src/, so create the file there:

src/proxy.ts
import { clerkMiddleware } from "@clerk/nextjs/server";

export default clerkMiddleware();

export const config = {
  matcher: [
    // Skip Next.js internals and all static files, unless found in search params
    "/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
    // Always run for API routes
    "/(api|trpc)(.*)",
    // Always run for Clerk-specific frontend API routes
    "/__clerk/(.*)",
  ],
};

On Next.js 15 or older, name the file middleware.ts instead. The code is the same; only the file name changes.

clerkMiddleware() leaves every route public, and the routes in this guide check for a user themselves, which is where Clerk recommends the check lives.

3.3. Add the Clerk provider and sign-in buttons

Replace src/app/layout.tsx so that ClerkProvider wraps the app and a header shows sign-in, sign-up, and user buttons. Show renders its children only when the condition in when holds, so the header shows the sign-in and sign-up buttons to visitors and the user button to signed-in users; it replaces the SignedIn and SignedOut components from earlier Clerk versions. mode="modal" opens the sign-in and sign-up forms on your page instead of redirecting to Clerk's hosted pages:

src/app/layout.tsx
import type { Metadata } from "next";
import { ClerkProvider, Show, SignInButton, SignUpButton, UserButton } from "@clerk/nextjs";
import "./globals.css";

export const metadata: Metadata = {
  title: "clerk-nextjs-prisma",
  description: "Generated by create-prisma",
};

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body>
        <ClerkProvider>
          <header className="navbar">
            <Show when="signed-out">
              <SignInButton mode="modal" />
              <SignUpButton mode="modal" />
            </Show>
            <Show when="signed-in">
              <UserButton />
            </Show>
          </header>
          {children}
        </ClerkProvider>
      </body>
    </html>
  );
}

Append styles for the header, and for the post form you build in step 5, to the template's stylesheet:

src/app/globals.css
.navbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 1rem;
  height: 4rem;
  padding: 0 1rem;
}

.post-form {
  display: grid;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}

.post-form input,
.post-form textarea,
.post-form button {
  padding: 0.5rem;
  border: 1px solid #e5e5e5;
  border-radius: 0.375rem;
  font: inherit;
}

4. Build a posts API

Create a route that stores a post for the signed-in user. auth() returns the Clerk user id from the session; a request without one gets 401. The route then looks up the matching database user, so a post always points at a User row:

src/app/api/posts/route.ts
import { auth } from "@clerk/nextjs/server";
import { db } from "@/src/prisma/db";

export async function POST(req: Request) {
  const { userId: clerkId } = await auth();
  if (!clerkId) return new Response("Unauthorized", { status: 401 });

  const { title, content } = await req.json();

  const user = await db.orm.public.User.where({ clerkId }).first();
  if (!user) return new Response("User not found", { status: 404 });

  const post = await db.orm.public.Post.create({
    title,
    content,
    authorId: user.id,
  });

  return Response.json(post, { status: 201 });
}

db is the Prisma ORM client the template created in src/prisma/db.ts, and the template maps @/ to the project root, so the import path is @/src/prisma/db. db.orm is the ORM query API, and on PostgreSQL every model sits under its schema, so the User table is db.orm.public.User. .first() returns one matching record or null, and .create() takes the fields directly and returns the inserted row. Reading data and Writing data cover the rest of the query API.

5. Add a post form

Create a client component with a title field, a content field, and a submit button. On submit it calls the posts API and, if the API accepted the post, clears the fields and reloads the page so the new post appears in the list; a rejected post keeps the draft in the fields:

src/app/components/PostInputs.tsx
"use client";

import { useState } from "react";

export default function PostInputs() {
  const [title, setTitle] = useState("");
  const [content, setContent] = useState("");

  async function createPost(e: React.FormEvent) {
    e.preventDefault();
    if (!title || !content) return;

    const res = await fetch("/api/posts", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ title, content }),
    });
    if (!res.ok) return;

    setTitle("");
    setContent("");
    location.reload();
  }

  return (
    <form onSubmit={createPost} className="post-form">
      <input
        type="text"
        placeholder="Title"
        value={title}
        onChange={(e) => setTitle(e.target.value)}
      />
      <textarea
        placeholder="Content"
        value={content}
        onChange={(e) => setContent(e.target.value)}
      />
      <button type="submit">Post</button>
    </form>
  );
}

6. Render the home page

Replace the template's home page with a server component that reads the Clerk session with currentUser(), finds the matching database user, loads that user's posts newest first, and renders the form above the list. The shell and users classes come from the template's stylesheet:

src/app/page.tsx
import { currentUser } from "@clerk/nextjs/server";
import { db } from "@/src/prisma/db";
import PostInputs from "@/src/app/components/PostInputs";

export default async function Home() {
  const user = await currentUser();
  if (!user) return <main className="shell">Sign in to post</main>;

  const dbUser = await db.orm.public.User.where({ clerkId: user.id }).first();
  if (!dbUser) return <main className="shell">Your user is not in the database yet</main>;

  const posts = await db.orm.public.Post.where({ authorId: dbUser.id })
    .orderBy((p) => p.createdAt.desc())
    .all();

  return (
    <main className="shell">
      <PostInputs />
      <ul className="users">
        {posts.map((post) => (
          <li key={post.id}>
            <div>
              <strong>{post.title}</strong>
              <p>{post.content}</p>
            </div>
          </li>
        ))}
      </ul>
    </main>
  );
}

Start the dev server in the shell where DATABASE_URL is exported:

npm run dev

Open http://localhost

: signed out, the page says Sign in to post, and the header shows Sign in and Sign up. Confirm that the posts API rejects anonymous requests:

curl -i -X POST http://localhost:3000/api/posts \
  -H "Content-Type: application/json" \
  -d '{"title":"Hello","content":"World"}'
HTTP/1.1 401 Unauthorized

Leave sign-up for step 7, because Clerk sends user.created once, when it creates the user, and nothing in your app listens for it yet, so an account created now would never reach the database.

7. Sync Clerk users with a webhook

Clerk owns sign-up, so your database learns about a new user from a webhook. Clerk sends a user.created event to a route in your app, and the route stores the user.

7.1. Create the webhook route

verifyWebhook() checks the request signature with the secret in CLERK_WEBHOOK_SIGNING_SECRET, which you set in the next step, and returns the typed event. The handler rejects anything it cannot verify, then upserts the user. conflictOn names the unique field Prisma ORM uses to look for an existing row: keying on the email means a redelivered event does not create a duplicate, and a user you delete in Clerk and sign up again with the same email gets their new clerkId instead of a failed insert:

src/app/api/webhooks/clerk/route.ts
import { verifyWebhook } from "@clerk/nextjs/webhooks";
import { NextRequest } from "next/server";
import { db } from "@/src/prisma/db";

export async function POST(req: NextRequest) {
  let evt;
  try {
    evt = await verifyWebhook(req);
  } catch (err) {
    console.error("Error verifying webhook:", err);
    return new Response("Error verifying webhook", { status: 400 });
  }

  console.log(`Received webhook with ID ${evt.data.id} and event type of ${evt.type}`);

  if (evt.type === "user.created") {
    const { id, email_addresses, primary_email_address_id, first_name, last_name } = evt.data;
    const email = email_addresses.find((e) => e.id === primary_email_address_id)?.email_address;
    if (!email) return new Response("User has no email address, skipped", { status: 200 });

    const name = [first_name, last_name].filter(Boolean).join(" ") || null;
    await db.orm.public.User.upsert({
      create: { clerkId: id, email, name },
      update: { clerkId: id, name },
      conflictOn: { email },
    });
  }

  return new Response("Webhook received", { status: 200 });
}

The route returns 200 for a verified event it chooses to skip, because any other status makes Clerk retry the delivery.

7.2. Register the endpoint in Clerk

Clerk needs a public URL to reach your dev server. In a second terminal, expose port 3000 with ngrok and copy the https://... forwarding URL it prints:

ngrok http 3000

Open the Webhooks page of your Clerk application and select Add Endpoint. Paste the forwarding URL followed by /api/webhooks/clerk into Endpoint URL, select the user.created event under Subscribe to events, and select Create:

https://a60b-99-42-62-240.ngrok-free.app/api/webhooks/clerk

The endpoint page shows a Signing Secret. Add it to .env.local; the dev server reloads the file when you save it and logs Reload env: .env.local:

.env.local
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=<your-publishable-key>
CLERK_SECRET_KEY=<your-secret-key>
CLERK_WEBHOOK_SIGNING_SECRET=<your-signing-secret>

7.3. Test the webhook

On the endpoint page, open the Testing tab, choose user.created under Send event, and select Send Example. Under Message Attempts, the delivery shows as Succeeded, and your dev server logs the event:

Received webhook with ID user_abc123 and event type of user.created
POST /api/webhooks/clerk 200

The example payload has no email addresses, so the handler skips it without writing a row. A real sign-up writes one.

7.4. Sign up and post

Open http://localhost

, select Sign up, and create an account. Clerk delivers user.created to your tunnel, the dev server logs it, and the page shows the post form. If the page says Your user is not in the database yet, it rendered before the webhook arrived, so reload it. Write a post and submit it: it appears below the form.

If the message stays after a reload, the webhook did not reach your app. Check that the tunnel is still running, that the endpoint URL in Clerk matches it (an ngrok URL changes every time you restart ngrok unless you use a static domain), and that /api/webhooks/clerk is at the end of the URL. Then delete the user in Clerk's Users tab and sign up again with the same email: Clerk sends user.created once per user, and the upsert updates the existing row with the new clerkId.

8. Deploy to Prisma Compute

Next.js is supported on Prisma Compute. The template already describes the app for the platform in module.ts and service.ts, which you don't need to edit, and sets output: "standalone" in next.config.ts. Keep that setting: Compute runs the standalone server that next build emits.

Sign in to your Prisma Data Platform account once (it opens a browser):

npx prisma auth login

Build and deploy from the project directory. next build reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY from .env.local and bakes it into the client bundle, so that key is the one Clerk variable you never set on Compute; the two secrets stay out of the build. The deploy provisions a Prisma Postgres database for the app, replays the migration from step 2 against it, and starts the app:

npm run build
npx prisma deploy
clerk-nextjs-prisma
├─ database   postgres-database db_abc123
└─ app        compute-service cps_abc123
              https://xyz.ewr.prisma.build

The deploy creates a project named clerk-nextjs-prisma in your Prisma Data Platform workspace, and re-running the deploy reuses it. If the deploy stops with HostedStateBootstrapError, a project with that name already exists in your workspace; re-run it with --name <unique-name> and use that name in the project link command below. The deployed database is separate from the one you used locally, so it starts empty.

The live URL already shows Sign in to post, but sign-in cannot complete yet, because the server needs two more values. It needs CLERK_SECRET_KEY to read sessions. It also needs the signing secret of a webhook endpoint that points at the live URL, so register that endpoint first. On the Webhooks page, add a second endpoint with the live URL followed by /api/webhooks/clerk, subscribe it to user.created, and copy its Signing Secret.

Link the directory to the project the deploy created:

npx prisma project link clerk-nextjs-prisma

Then add both secrets to the production role, which is the scope a deploy without --environment targets (the one you just ran). Read each value into your shell with read -rs, which neither echoes it nor records it in your shell history: paste the secret key at the first prompt and the live endpoint's signing secret at the second, pressing Enter after each. When you pass only the variable name, the CLI reads the value from your shell environment. The values are stored encrypted and are never shown again, so keep your own copy:

read -rs CLERK_SECRET_KEY && read -rs CLERK_WEBHOOK_SIGNING_SECRET && export CLERK_SECRET_KEY CLERK_WEBHOOK_SIGNING_SECRET
npx prisma project env add CLERK_SECRET_KEY --role production
npx prisma project env add CLERK_WEBHOOK_SIGNING_SECRET --role production

Variables apply on the next deploy, so deploy again. The code has not changed, so there is no need to rebuild:

npx prisma deploy

Open the live URL and sign up with an email you have not used yet. Clerk sends user.created only when it creates a user, and the deployed database starts empty, so an account from step 7 would sign in without ever being written there. Clerk delivers the event to the live endpoint, the app writes the user, and the page shows the post form; if it says Your user is not in the database yet, reload. Create a post to confirm the posts API works on Compute.

This deploys your Clerk application's development instance: the pk_test_ and sk_test_ keys belong to it, and it runs on any domain. A Clerk production instance needs a domain you own, so it is a step for when you move off the prisma.build URL; see Deploying to production in the Clerk docs. For previews per Git branch and deploy on push, see Deploy on push; there the build runs in GitHub Actions, which has no .env.local, so give that workflow NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY as a repository secret before you connect the repository.

Common gotchas

Error verifying webhook: Error: @clerk/backend: Missing webhook signing secret. The route runs but CLERK_WEBHOOK_SIGNING_SECRET is not set. Add it to .env.local; if the dev server does not log Reload env: .env.local, restart it.

The page says Your user is not in the database yet right after sign-up. Clerk delivers the webhook after the sign-up completes, so the first render can beat it. Reload the page. If it persists locally, check the tunnel and endpoint URL as described in step 7.

Deploy fails with MIGRATION_PATH_NOT_FOUND. The deploy builds the production database by replaying migrations/, and db:init alone does not write a migration. Run npx prisma migration plan --name init as in step 2 and deploy again.

npm run db:init says CONFIG.DB_CONNECTION_REQUIRED. DATABASE_URL is in a file but not exported. The Prisma CLI reads the environment, so run export DATABASE_URL="..." in the same shell.

Use with your agent

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

Use with your agent
Use with your agent
Add Clerk authentication to a new Next.js app on Prisma ORM, sync users to Prisma Postgres with a webhook, and deploy to Prisma Compute. Follow https://www.prisma.io/docs/guides/authentication/clerk/nextjs.md.

1. Scaffold: `npm create prisma@latest -- clerk-nextjs-prisma --template next --provider postgres --no-deploy --yes`. Then run `npx prisma@latest init` in `clerk-nextjs-prisma` so the Prisma agent skills are installed, and use them. Create a Prisma Postgres database with `npx create-db@latest`, show me the claim URL it prints, and export the connection string as `DATABASE_URL` in the shell.
2. Replace `src/prisma/contract.prisma` with the `User` and `Post` models from the guide, delete `src/prisma/seed.ts` and `src/prisma/users.ts`, run `npm run contract:emit`, delete `migrations/`, run `npx prisma migration plan --name init`, then `npm run db:init`.
3. Install `@clerk/nextjs`. Ask me for the publishable key and secret key from the Clerk Dashboard, put them in `.env.local`, and add `.env.local` to `.gitignore`; never print them. Create `src/proxy.ts` with `clerkMiddleware()`, and update `src/app/layout.tsx` with `ClerkProvider`, `Show`, `SignInButton`, `SignUpButton`, and `UserButton` as in the guide.
4. Create the posts route at `src/app/api/posts/route.ts`, the `PostInputs` component, the home page, and the webhook route at `src/app/api/webhooks/clerk/route.ts` from the guide. Run `npx tsc --noEmit` and fix any errors.
5. Start `npm run dev`, confirm `GET /` renders "Sign in to post" and `POST /api/posts` returns 401 without a session, then stop and ask me to register the webhook endpoint in Clerk and give you the signing secret for `.env.local`.
6. Deploy: check `npx prisma auth whoami`; if I am not signed in, stop and ask me to run `npx prisma auth login`. Then run `npm run build` followed by `npx prisma deploy` and show me the live URL. If the deploy fails with `HostedStateBootstrapError`, re-run it with `--name <a unique name>`.
7. Ask me to register a second Clerk webhook endpoint for `<live URL>/api/webhooks/clerk` and give you its signing secret. Run `npx prisma project link clerk-nextjs-prisma`, add `CLERK_SECRET_KEY` and `CLERK_WEBHOOK_SIGNING_SECRET` to the production role with `npx prisma project env add <NAME> --role production` (value from the environment, never on the command line or in the chat), then run `npx prisma deploy` again and confirm the live URL still responds.

Run npx prisma@latest init once to install the Prisma ORM skills for your coding agent. Prompts that map to this guide:

  • "Handle Clerk's user.updated and user.deleted events in the webhook route so the User row stays in sync."
  • "Add a DELETE /api/posts/[id] route that only lets the author delete the post."

Next steps