⚡ Instantly generate typed composables and API types from a Swagger/OpenAPI spec for Nuxt projects.
nuxt-generate-api
is a CLI tool that takes a swagger.json
file and generates:
- Fully-typed composables:
composables/useApi.ts
- API type definitions:
types/api.types.ts
- A typed fetch wrapper:
composables/useApiService.ts
Built for Nuxt 3, using $fetch
, useRuntimeConfig
, and TypeScript.
npm install -D nuxt-generate-api
Or use via npx
:
npx nuxt-generate-api
- Download your Swagger spec:
💡 Tip: If using Swagger UI, open DevTools → Network → refresh → find a
.json
file likeopenapi.json
, right-click → "Save as" →swagger.json
-
Place the
swagger.json
file in the root of your Nuxt project. -
Run:
npx nuxt-generate-api
This will generate:
/types/api.types.ts ← All request & response types
/composables/useApi.ts ← Typed composables (e.g., createUser, getPosts)
/composables/useApiService.ts ← Reusable fetch helper
- Types generated from
components.schemas
- Request/response interfaces per endpoint
- Auto-generated composables for each endpoint
- Uses
useApiService<T>()
under the hood
- A wrapper around
$fetch
with built-in support for:- Query params
- Request body
- Runtime API base URL from
useRuntimeConfig
// In your Nuxt 3 component or composable
const { createUser, getPosts } = useApi();
const result = await createUser({
body: {
name: 'John Doe',
email: 'john@example.com'
}
});
- Node.js v16+
- Nuxt 3 (uses
useRuntimeConfig
and$fetch
) - A valid
swagger.json
file
- [ ] CLI arguments (
--input
,--output
) - [ ] Support OpenAPI 3.1
- [ ] Optional
zod
validation output - [ ] GitHub Action support for CI type generation
MIT
Created by Fayzulla Rahmatullayev
Inspired by openapi-typescript
, swagger-typescript-api
, and Nuxt
DX best practices.