Skip to content
Client logo

Client

Officialv6.2.0

@kubb/plugin-client

kubb add plugin-client

Call operations

Call a generated function with one grouped options object. It returns your client's promise, shaped by your RequestResult type.

import { addPet, getPetById } from './gen/clients'

const created = await addPet({ body: { name: 'Odie', photoUrls: [] } })
console.log(created.data, created.response.status)

const pet = await getPetById({ path: { petId: 1 } })

The examples set integerType: 'number' on the adapter. Without it, petId is a bigint and takes 1n.

KeyHolds
path, query, headers, cookies, bodyThe request values, typed from the spec.
throwOnErrorOverrides throwOnErrorDefault for this call.
clientAnother client function for this call, such as a signed or per-tenant client.
Any other keyPassed to your client as is, such as signal.

Path keys keep their spec names unless you set pathParamsCasing. See Path parameter names.

Handle errors

Your client decides what throwOnError does. With the client from scratch, a non-2xx status throws, and throwOnError: false returns the error as a value:

const result = await getPetById({ path: { petId: 1 }, throwOnError: false })

if (result.error === undefined) console.log('pet', result.data)
else console.log('failed', result.response.status)

Next, validate requests and responses.

Less common cases

Path parameter names

When the spec names a path parameter in kebab or snake case, such as {pet-id}, the path key keeps that name: pass path: { 'pet-id': 1 }.

Set pathParamsCasing: 'camelcase' on the adapter (@kubb/adapter-oas 5.20.0 or later) to pass path: { petId: 1 } instead. The HTTP request stays the same.