> Index of every page on this site: [llms.txt](https://kubb.dev/llms.txt)

# SWR

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

::code-group{sync="install"}

```shell [kubb]
kubb add plugin-swr
```

```shell [bun]
bun add -d @kubb/plugin-swr
```

```shell [pnpm]
pnpm add -D @kubb/plugin-swr
```

```shell [npm]
npm install --save-dev @kubb/plugin-swr
```

```shell [yarn]
yarn add -D @kubb/plugin-swr
```

::

`kubb add` installs the version the [Kubb registry](/registry) pins and adds `pluginSwr()` to `kubb.config.ts`. It needs a `kubb.config.ts`, so run [`kubb init`](/docs/5.x/reference/commands/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`, `@kubb/plugin-ky`, 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

| 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.

```typescript [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](https://kubb.dev/extensions/kubb-labs/plugin-swr/options) for every option. Next, [call operations](https://kubb.dev/extensions/kubb-labs/plugin-swr/guides/calling-operations).

## Sitemap

See the full [sitemap](https://kubb.dev/sitemap.md) for all pages.
