{"id":1200,"date":"2026-07-08T06:00:59","date_gmt":"2026-07-07T23:00:59","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/08\/how-to-build-a-full-stack-app-with-trpc-prisma-and-next-js\/"},"modified":"2026-07-08T06:00:59","modified_gmt":"2026-07-07T23:00:59","slug":"how-to-build-a-full-stack-app-with-trpc-prisma-and-next-js","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/08\/how-to-build-a-full-stack-app-with-trpc-prisma-and-next-js\/","title":{"rendered":"How to Build a Full-Stack App with tRPC, Prisma, and Next.js"},"content":{"rendered":"<h1>How to Build a Full-Stack App with tRPC, Prisma, and Next.js<\/h1>\n<p>Combining tRPC, Prisma, and Next.js gives you end-to-end type safety with minimal boilerplate. This tutorial shows you how to set up a tRPC server that uses Prisma to query your database directly from your Next.js API routes. You\u2019ll get autocompletion and type checking from database to frontend.<\/p>\n<p>First, initialize a Next.js project with TypeScript. Then install the core packages: <code>@trpc\/server<\/code>, <code>@trpc\/client<\/code>, <code>@trpc\/next<\/code>, <code>@prisma\/client<\/code>, and <code>zod<\/code> for validation. Run <code>npx prisma init<\/code> and configure your database URL in <code>.env<\/code>. Define a simple model, e.g., <code>User<\/code> with <code>id<\/code>, <code>name<\/code>, and <code>email<\/code>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20use%20Trpc%20with%20Prisma%20and%20Next\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>1. Create Your Prisma Client<\/h2>\n<p>In <code>lib\/prisma.ts<\/code>, instantiate and export a singleton PrismaClient. This prevents multiple connections during development. Example:<\/p>\n<pre><code>import { PrismaClient } from '@prisma\/client';\nconst prisma = new PrismaClient();\nexport default prisma;<\/code><\/pre>\n<h2>2. Build the tRPC Router<\/h2>\n<p>Create <code>server\/trpc.ts<\/code> to initialize tRPC with a context that includes the Prisma client. Then define your procedures. For instance, a query that returns all users:<\/p>\n<ul>\n<li>Import <code>initTRPC<\/code> and <code>prisma<\/code>.<\/li>\n<li>Use <code>t.procedure.query<\/code> to call <code>prisma.user.findMany()<\/code>.<\/li>\n<li>Add a mutation to create a user using <code>zod<\/code> for input validation.<\/li>\n<\/ul>\n<h2>3. Connect to Next.js API<\/h2>\n<p>In <code>pages\/api\/trpc\/[trpc].ts<\/code>, export a tRPC HTTP handler. Pass your router and create context. This single endpoint handles all tRPC requests:<\/p>\n<pre><code>export default trpcNext.createNextApiHandler({\n  router: appRouter,\n  createContext: () => ({}),\n});<\/code><\/pre>\n<h2>4. Use tRPC Client on Frontend<\/h2>\n<p>Wrap your app with <code>withTRPC<\/code> HOC from <code>@trpc\/next<\/code>. Inside a React component, call <code>trpc.user.getAll.useQuery()<\/code>. The data is typed perfectly. Mutations work similarly with <code>useMutation<\/code>.<\/p>\n<p>That\u2019s it! You now have a type\u2011safe pipeline from Prisma \u2192 tRPC \u2192 Next.js frontend. The server code stays minimal, and you can easily add more models and procedures without losing type safety.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Build a Full-Stack App with tRPC, Prisma, and Next.js Combining tRPC, Prisma, and Next.js gives you end-to-end type safety with minimal boilerplate. This tutorial shows you how to set up a tRPC server that uses Prisma to query your database directly from your Next.js API routes. You\u2019ll get autocompletion and type checking from &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-1200","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1200"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1200\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}