Skip to main content
Web DevelopmentTutorials

Building Type-Safe APIs with Nuxt 3 and Prisma

Learn how to build end-to-end type-safe APIs using Nuxt 3 server routes and Prisma ORM for a seamless developer experience.

Mbeah Essilfie

Mbeah Essilfie

July 23, 2026 at 06:02 AM

8 min read 1919 views
Building Type-Safe APIs with Nuxt 3 and Prisma

Why Type Safety Matters

In modern full-stack development, type safety isn't a luxury — it's a necessity. When your frontend and backend share the same type definitions, entire classes of bugs simply vanish.

Type-safe APIs eliminate runtime type errors and give you autocomplete across your entire stack.

Setting Up Prisma in Nuxt 3

First, install the dependencies:

pnpm add prisma @prisma/client
pnpm add -D tsx

Define your schema:

model Post {
  id        String   @id @default(uuid())
  title     String
  content   String
  slug      String   @unique
  createdAt DateTime @default(now())
  author    User     @relation(fields: [authorId], references: [id])
  authorId  String
}

Creating Type-Safe Server Routes

Nuxt 3's server routes automatically infer return types. Combined with Prisma's generated types, you get full type safety:

// server/api/posts/index.get.ts
import prisma from '~/server/db'

export default defineEventHandler(async (event) => {
  const posts = await prisma.post.findMany({
    include: { author: { select: { name: true, avatarUrl: true } } },
    orderBy: { createdAt: 'desc' },
  })

  return { data: posts }
})

On the frontend, useFetch automatically knows the shape of the response:

<script setup lang="ts">
const { data } = await useFetch('/api/posts')
// data.value?.data is fully typed as Post[]
</script>

Validation with Zod

Add runtime validation that also provides types:

import { z } from 'zod'

const CreatePostSchema = z.object({
  title: z.string().min(3).max(200),
  content: z.string().min(10),
  slug: z.string().regex(/^[a-z0-9-]+$/),
})

type CreatePostInput = z.infer<typeof CreatePostSchema>
With this setup, types flow from your database schema through your API to your components — no manual type definitions needed.

Key Takeaways

  • Prisma generates types from your schema automatically
  • Nuxt 3 server routes provide inferred return types to useFetch
  • Zod bridges runtime validation and compile-time types
  • This stack eliminates the "type gap" between frontend and backend
TypeScriptNuxtPrisma
Mbeah Essilfie

Written by Mbeah Essilfie

Fullstack Software Developer

Read next

The Complete Guide to Vue 3 Composables
12 min read

The Complete Guide to Vue 3 Composables

Understand the power of Vue 3's Composition API through practical, real-world composable patterns that you can use today.

Mbeah EssilfieMbeah Essilfie
987