Plugin
plugins: [pluginSwr()]Officialv5.10.0kubb >=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
- Downloads72.3K/ monthnpm, last 30 days
- GitHub stars14kubb-labs/plugins
- Unpacked size471.4kBv5.10.0 on npm
- Latest release17h agov5.10.0 · Nov 22, 2026
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
useSWRInfiniteor suspense hooks.
Installation
kubb add plugin-swrbun add -d @kubb/plugin-swrpnpm add -D @kubb/plugin-swrnpm install --save-dev @kubb/plugin-swryarn add -D @kubb/plugin-swrkubb 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, orpluginZod({ inferred: 'codec' })withzodv4 in the app. - Add one client plugin:
@kubb/plugin-fetch,@kubb/plugin-axios,@kubb/plugin-ky, or@kubb/plugin-client. Leavesdkunset on it. - Install
swr2.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
| Option | What it does |
|---|---|
client | Picks 'fetch', 'axios', 'ky', or 'client' when several client plugins are registered. |
query, mutation | Which 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.
