Skip to content

Nuxt OpenAPI HyperfetchGenerate API Code for Nuxt

Point it at your OpenAPI spec and get fully typed useFetch composables, useAsyncData composables, or Nuxt server routes — ready to use, nothing to write by hand.

Nuxt OpenAPI Hyperfetch

What gets generated

Run the CLI and pick your generators:

bash
npx nxh generate

Composables (useFetch or useAsyncData):

vue
<script setup lang="ts">
// Fully typed — petId: number, data: Pet | null, error typed
const { data: pet, pending, error } = useFetchGetPetById(
  { petId: 123 },
  {
    onSuccess: (pet) => console.log('Loaded:', pet.name),
    onError: (err) => showToast(err.message, 'error'),
  }
)
</script>

Server routes (BFF mode):

typescript
// Generated: server/api/pet.post.ts
export default defineEventHandler(async (event): Promise<Pet> => {
  const auth = await getAuthContext(event)   // your auth logic

  const data = await $fetch(`${config.apiBaseUrl}/pet`, {
    headers: { Authorization: `Bearer ${config.apiSecret}` }, // key never exposed
  })

  return transformPet(data, event, auth)     // your transform logic
})

Your transformer stub (server/bff/transformers/pet.ts) is generated once and never overwritten — add your business logic there:

typescript
export async function transformPet<T = any>(
  data: T,
  event: H3Event,
  auth: AuthContext | null
): Promise<T> {
  return {
    ...(data as any),
    canEdit: auth?.permissions.includes('pet:write') ?? false,
  } as T
}

Why use this CLI?

You stop writing boilerplate

Every endpoint in your OpenAPI spec becomes a ready-to-use composable or server route. If your spec has 40 endpoints, you get 40 typed composables with one command.

Nuxt-native, but enhanced

Generated composables use Nuxt's own useFetch and useAsyncData under the hood — fully SSR-compatible, works in components and pages without any setup. On top of that, you get lifecycle callbacks (onRequest, onSuccess, onError, onFinish), pick to trim the response, global callback plugins, and request interception. All the SSR guarantees of Nuxt, with more control where you need it.

Raw responses when you need them

The useAsyncData generator exposes raw response access — read status codes, response headers, and the full HTTP response before it hits your component. Useful for pagination headers, ETag caching, or any API that communicates via headers.

Your API keys never reach the browser

In BFF mode, credentials live in Nuxt runtimeConfig and are only used in server routes. The client calls /api/pet, not your backend directly.

Your custom logic survives regeneration

Transformers and auth context are generated once. When you regenerate after a spec update, only the route files change — your business logic is untouched.

What's Next?

Released under the Apache-2.0 License.