Skip to content
Plugin
SWR logo
plugins: [pluginSwr()]
Officialv5.9.4kubb >=5.19.0node >=22.22

OpenAPI to SWR hook generator

@kubb/plugin-swr

Generates typed SWR hooks from your OpenAPI spec, so data fetching stays in sync with the API.

kubb add plugin-swr
swrreacthooksdata-fetchingcodegenopenapi

The SWR plugin generates a useSWR hook for each query and a useSWRMutation hook for each mutation in your OpenAPI spec, with their cache keys.

  • The plugin ships no HTTP client. Each hook calls a function from your client plugin.
  • The plugin does not generate useSWRInfinite or suspense hooks.

Installation

kubb add plugin-swr

kubb add installs the version the Kubb registry pins and adds pluginSwr() to kubb.config.ts. It needs a kubb.config.ts, so run kubb init first in a new project.

Setup

  • Add @kubb/plugin-ts, or pluginZod({ inferred: 'codec' }) with zod v4 in the app.
  • Add one client plugin: @kubb/plugin-fetch, @kubb/plugin-axios, or @kubb/plugin-client. Leave sdk unset on it.
  • Install swr 2.2 or a later v2 release in the app.
  • Set the base URL, auth, and validation on the client plugin. With @kubb/plugin-client, your client module owns them.

Options worth knowing

OptionWhat it does
clientPicks 'fetch', 'axios', or 'client' when several client plugins are registered.
query, mutationWhich HTTP methods become queries and mutations. false skips them.

Example

This config generates hooks that call the Fetch client.

kubb.config.ts
import { defineConfig } from 'kubb/config'
import { adapterOas } from '@kubb/adapter-oas'
import { pluginTs } from '@kubb/plugin-ts'
import { pluginFetch } from '@kubb/plugin-fetch'
import { pluginSwr } from '@kubb/plugin-swr'

export default defineConfig({
  input: './petStore.yaml',
  output: { path: './src/gen' },
  adapter: adapterOas({ integerType: 'number' }),
  plugins: [
    pluginTs(),
    pluginFetch(),
    pluginSwr(),
  ],
})

See Options for every option. Next, call operations.

Dependencies

No extension dependencies.

Optional dependencies

Extensions SWR works with when they are in the config.