Beta You're reading the docs for Kubb v5, which is currently in beta. View the stable v4 docs
Skip to content
Official v5.0.0-beta.73 MIT kubb >=5.0.0 node >=22

@kubb/plugin-axios

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

api-clientaxioshttp-clientcodegenopenapivalidator
Downloads
1.5k / mo
Stars
3
Bundle size
321.6 kB
Updated
2w ago

@kubb/plugin-axios

@kubb/plugin-axios turns each OpenAPI operation into a typed async function that calls your API with axios. The path, query parameters, request body, response, and error shape all come from the spec, so a call stays in sync with the API it targets.

From your spec, the generated client gives you:

It builds on @kubb/plugin-ts for the types, so add that to your config, and axios is a runtime dependency to install next to your app.

Each function takes one grouped options object ({ path, query, headers, body }) and returns a RequestResult of { status, data, error, contentType, request, response }, bundled into .kubb/client.ts. See error handling for throwOnError and the status-keyed result union.

The bundled client also exposes getUrl, which builds an operation's final URL without sending the request, useful for cache keys, prefetch, and links:

ts
import { client } from './.kubb/client'

const url = client.getUrl({ url: '/pet/{petId}', path: { petId: 1 }, query: { status: ['available'] } })
// '/pet/1?status=available'

For a native axios field the runtime does not set (timeout, proxy, maxRedirects, decompress, onUploadProgress), pass options, on the client or per call, where a per-call value wins:

ts
import { client } from './.kubb/client'
import { uploadFile } from './uploadFile'

client.setConfig({ options: { timeout: 10_000 } })

await uploadFile({ path: { petId: 1 }, body, options: { timeout: 2_000, onUploadProgress: (e) => console.log(e.loaded) } })

For cross-cutting concerns like retries and interceptors, reach for a custom transport instead.

Installation

shell
bun add -d @kubb/plugin-axios@beta
shell
pnpm add -D @kubb/plugin-axios@beta
shell
npm install --save-dev @kubb/plugin-axios@beta
shell
yarn add -D @kubb/plugin-axios@beta

Dependencies

This plugin needs @kubb/plugin-ts in your config. Kubb runs it before plugin-axios so the functions can import the generated types. When validator is set to 'zod', also add @kubb/plugin-zod.

Example

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

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen' },
  : [
    (),
    ({
      : { : 'clients', : { : 'named' } },
      : 'https://petstore.swagger.io/v2',
      : {
        : 'tag',
        : ({  }) => `${}Service`,
      },
    }),
  ],
})

See also