Skip to content
How-to guides

Configure generation

Configure common Kubb stacks, formatting, linting, and post-generate commands.

Use these configurations in a project with Kubb installed and an OpenAPI specification. Replace ./petStore.yaml with your specification path, install the packages for your stack, then run npx kubb generate. Each plugin page shows its single-plugin configuration. The stacks below combine several plugins.

The examples write to ./src/gen. With clean: true, Kubb removes that directory before generation, so keep handwritten files elsewhere.

Pick a stack

Types, an Axios client, and TanStack Query hooks for React.

Terminal
npm install -D kubb typescript @kubb/plugin-ts @kubb/plugin-axios @kubb/plugin-react-query
npm install axios react @tanstack/react-query
kubb.config.ts
import { defineConfig } from 'kubb/config'
import { pluginTs } from '@kubb/plugin-ts'
import { pluginAxios } from '@kubb/plugin-axios'
import { pluginReactQuery } from '@kubb/plugin-react-query'

export default defineConfig({
  input: './petStore.yaml',
  output: { path: './src/gen', clean: true },
  plugins: [pluginTs(), pluginAxios(), pluginReactQuery({ hooks: true })],
})

To call the API with the global fetch instead of Axios, replace pluginAxios with pluginFetch from @kubb/plugin-fetch.

Format, lint, and run commands after generation

Set format and lint to a tool name, or to 'auto' to pick whichever tool is installed. output.postGenerate runs shell commands once the files are formatted and linted. Install the tools first, for example npm install -D @biomejs/biome oxlint.

kubb.config.ts
import { defineConfig } from 'kubb/config'
import { pluginTs } from '@kubb/plugin-ts'

export default defineConfig({
  input: './petStore.yaml',
  output: {
    path: './src/gen',
    clean: true,
    format: 'biome',
    lint: 'oxlint',
    postGenerate: ['npm run typecheck'],
  },
  plugins: [pluginTs()],
})

Read the spec from a git repository

Point input at a file in a git repository when the spec lives in another repo, such as the API's own. Write it as git+<repo>#<ref>:<path>, and pin <ref> to a tag or commit so every run generates from the same spec:

kubb.config.ts
import { defineConfig } from 'kubb/config'

export default defineConfig({
  input: 'git+https://github.com/org/api.git#v2.1.0:specs/openapi.yaml',
  output: { path: './src/gen' },
})

Kubb needs git on the PATH and uses your own git credentials, so a private repository works with the credential helper or SSH key you already use. In CI, give the job read access to that repository. See input for the full format.

Watch mode, several specifications, and scripts

  • Run kubb generate --watch to regenerate when the local spec file changes. A URL or git input is generated once, so rerun Kubb after the remote spec changes. Pass a function to defineConfig to read watch and keep clean off during incremental runs. See Config formats.
  • Pass an array to defineConfig to generate from several specifications in one run, each with its own plugins and output directory. Set a name per entry so the CLI output tells them apart.
  • Drive Kubb from a script with createKubb when you orchestrate builds or inspect diagnostics.
  • Run kubb validate before generation in CI to fail on an invalid specification.