nuxt-endpoints
nuxt-endpoints

Typed APIs, generated clients, and OpenAPI for Nuxt server routes — from one endpoint definition

Nuxt Endpoints

npm version npm downloads License Nuxt

Validate Nuxt server requests, type responses by HTTP status, and generate OpenAPI 3.1 from one endpoint definition. Start with an inferred handler return and add response schemas when you need them.

Documentation · Type playground

Quick start

Install the module and a schema library (this example uses Zod).

npx nuxt module add nuxt-endpoints
npm install zod

On Nuxt 4.6 and 4.7, enable native fetch typing for useFetch and $fetch.

export default defineNuxtConfig({
  modules: ['nuxt-endpoints'],
  experimental: { routeTypedFetch: true },
  endpoints: { client: { nativeFetch: true } },
})
// server/api/users/[id].get.ts
import { z } from 'zod'
export default defineEndpoint({
  summary: 'Get a user',
  validate: {
    params: z.object({ id: z.coerce.number() }),
  },
  responses: {
    200: z.object({ id: z.number(), name: z.string() }),
    404: z.object({ message: z.string() }),
  },
  handler: (event) => {
    const id = event.validated.params.id
    if (id === 1) return { id, name: 'Tom' }
    return event.respond(404, { message: 'User not found' })
  },
})

Call the route with Nuxt’s useFetch. The HTTP status narrows the error body.

<script setup lang="ts">
const { data, error } = await useFetch('/api/users/2')

if (error.value?.status === 404) console.log(error.value.data?.message)
else if (data.value) console.log(data.value.name)
</script>

For Nuxt 4.5 client options and Nuxt 4.7 support with Nitro 2 or experimental Nitro 3, see Compatibility.

Contributing

See CONTRIBUTING.md for local setup, checks, and guidelines.

License

MIT. See Third-Party Notices for attributions that apply to bundled documentation assets.