Skip to content
Plugin
React Query logo
plugins: [pluginReactQuery()]
Officialv5.11.0kubb >=5.19.0node >=22.22

OpenAPI to React Query generator

@kubb/plugin-react-query

Generates typed TanStack Query helpers for React from your OpenAPI spec: queryOptions and mutationOptions factories with cache keys, plus optional hooks.

kubb add plugin-react-query
react-querytanstack-queryreacthooksdata-fetchingcodegenopenapi

The React Query plugin generates TanStack Query queryOptions and mutationOptions factories with cache keys from your OpenAPI spec, plus optional hooks.

  • hooks: true adds useQuery and useMutation hooks, with suspense and infinite variants on request.
  • The plugin ships no HTTP client. Each helper calls a function from your client plugin.

Installation

kubb add plugin-react-query

kubb add installs the version the Kubb registry pins and adds pluginReactQuery() 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, @kubb/plugin-ky, or @kubb/plugin-client. Leave sdk unset on it.
  • Install @tanstack/react-query 5.82 or a later v5 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', 'ky', or 'client' when several client plugins are registered.
hooksAdds the use* hooks. infinite, suspense, and customOptions need it.
query, mutationWhich HTTP methods become queries and mutations. false skips them.
infiniteAdds infinite queries to operations with the infinite.queryParam parameter, 'id' by default.
suspense{} adds useSuspenseQuery hooks.
customOptionsRoutes every hook through your function for shared options.

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 { pluginReactQuery } from '@kubb/plugin-react-query'

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

See Options for every option. Next, call operations.

Dependencies

No extension dependencies.

Optional dependencies

Extensions React Query works with when they are in the config.