Skip to content
Plugin
Fetch
plugins: [pluginFetch()]
Officialv5.5.11kubb >=5.8.3node >=22

Fetch

@kubb/plugin-fetch

Generates a type-safe Fetch API client from your OpenAPI spec, one async function per operation, so each call stays in sync with the API.

kubb add plugin-fetch
api-clientfetchhttp-clientcodegenopenapivalidator

The Fetch plugin turns each operation in your OpenAPI spec into a typed async function that sends the request with the native Fetch API. Each function takes grouped path, query, headers, and body parameters and returns a result keyed by status code.

The query plugins (@kubb/plugin-react-query, @kubb/plugin-vue-query, and @kubb/plugin-swr) and @kubb/plugin-mcp call these functions. The plugin also copies its runtime files (client.ts, serializers.ts, and standardSchema.ts) to .kubb/ in the root output folder.

Installation

bun add -d @kubb/plugin-fetch

Or run kubb add plugin-fetch to install the version the Kubb registry pins and add pluginFetch() to kubb.config.ts.

Setup

  • Add @kubb/plugin-ts for the operation types, or @kubb/plugin-zod with inferred: true.
  • Add @kubb/plugin-zod when you set validator, and install zod v4. Without the plugin, validator does nothing.
  • No HTTP client package is needed. The generated code calls the global fetch.

Options worth knowing

  • baseURL is prepended to every request. The spec's servers URL is not used, so without baseURL you set one at runtime with client.setConfig({ baseURL }).
  • validator: 'zod' validates response bodies with the schemas from @kubb/plugin-zod. Use { request: 'zod', response: 'zod' } to also validate request bodies.
  • throwOnErrorDefault: false returns non-2xx responses as values instead of throwing. A single call can still pass throwOnError.
  • returnType: 'data' makes a call resolve to the success body instead of the full result. With throwOnError: false, the call still returns the full result.
  • sdk generates a class-based SDK, grouped by tag or flat, instead of standalone functions. The query plugins and @kubb/plugin-mcp need the standalone functions, so leave sdk unset when you use them.

See Options for every option and its default.

Example

The config below generates a Fetch client that sends every request to the Petstore API.

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

export default defineConfig({
  input: './petStore.yaml',
  output: { path: './src/gen' },
  plugins: [
    pluginTs(),
    pluginFetch({ baseURL: 'https://petstore.swagger.io/v2' }),
  ],
})

Dependencies

Extensions that must be in the same config as Fetch.