Skip to content

Basic Usage

1. Create the config

kubb.config.ts drives everything Kubb does. The minimum config points at your spec and an output directory:

kubb.config.ts
typescript
import {  } from 'kubb'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen', : true },
})

input.path accepts a local file path or a URL. output.clean: true wipes the output directory before each run.

2. Pick your plugins

Each output format is its own plugin. Add only the ones you need:

typescript
import {  } from 'kubb'
import {  } from '@kubb/plugin-ts'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen', : true },
  : [({ : { : 'models' } })],
})
typescript
import {  } from 'kubb'
import {  } from '@kubb/plugin-ts'
import {  } from '@kubb/plugin-client'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen', : true },
  : [({ : { : 'models' } }), ({ : { : 'clients' } })],
})
typescript
import {  } from 'kubb'
import {  } from '@kubb/plugin-ts'
import {  } from '@kubb/plugin-client'
import {  } from '@kubb/plugin-react-query'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen', : true },
  : [({ : { : 'models' } }), ({ : { : 'clients' } }), ({ : { : 'hooks' } })],
})
typescript
import {  } from 'kubb'
import {  } from '@kubb/plugin-ts'
import {  } from '@kubb/plugin-client'
import {  } from '@kubb/plugin-react-query'
import {  } from '@kubb/plugin-zod'
import {  } from '@kubb/plugin-msw'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen', : true },
  : [
    ({ : { : 'models' } }),
    ({ : { : 'clients' } }),
    ({ : { : 'hooks' } }),
    ({ : { : 'schemas' } }),
    ({ : { : 'mocks' } }),
  ],
})
Plugin Package Generates
pluginTs @kubb/plugin-ts TypeScript types and interfaces
pluginClient @kubb/plugin-client Fetch-based HTTP client functions
pluginReactQuery @kubb/plugin-react-query TanStack Query hooks
pluginZod @kubb/plugin-zod Zod validation schemas
pluginMsw @kubb/plugin-msw MSW request handlers

NOTE

pluginClient, pluginReactQuery, and pluginMsw each require pluginTs in the same config.

See the plugins catalogue for the full list.

3. Run generate

kubb generate

Kubb creates one folder per plugin under output.path. Re-run after every spec change. See kubb generate for flags like --watch and --reporter.

4. Use the generated code

Import paths follow the output.path values you set for each plugin:

typescript
import type {  } from './gen/models/Pet'

const :  = { : 1, : 'Cat' }
typescript
import { getPetById } from './gen/clients/getPetById'

const pet = await getPetById({ pathParams: { petId: 1 } })
typescript
import { useGetPetById } from './gen/hooks/useGetPetById'

function Pet({ id }: { id: number }) {
  const { data, isLoading } = useGetPetById({ pathParams: { petId: id } })
  if (isLoading) return null
  return <span>{data?.name}</span>
}
typescript
import { petSchema } from './gen/schemas/petSchema'

const result = petSchema.safeParse(unknown)
typescript
import { setupServer } from 'msw/node'
import { getPetByIdHandler } from './gen/mocks/getPetByIdHandler'

const server = setupServer(getPetByIdHandler())
server.listen()

5. Keep it in sync

  • Manual: run npm run generate whenever the spec changes and commit the output.
  • Bundler integration: use unplugin-kubb to run generation as part of Vite, Rollup, Webpack, and others.