Skip to main content
Version: 2.x 🚧

createAPIClient(...)

Qraft CLI generates a helper function createAPIClient(...), which creates a Qraft API client with the necessary requestFn, baseUrl and queryClient for React Hooks.

To use Qraft with a complete set of methods and React hooks, you need to provide queryClient, requestFn, and baseUrl. This setup allows you to use the API client for both React and Node.js.

import { createAPIClient } from './api'

const api = createAPIClient({
queryClient,
requestFn,
baseUrl,
})

Arguments

  1. options: QraftClientOptions - Required options to be used by the Qraft API client
    • options.queryClient - Required QueryClient instance to be used by Qraft Hooks.
    • options.requestFn - Required requestFn function to be used by Qraft Hooks.
    • options.baseUrl - Required base URL for the requestFn function.

Returns

  • Qraft API client with the necessary React Hooks for operations. It contains all the methods to interact with the API grouped in services.

Examples

src/fetch-queries.ts
import { requestFn } from '@openapi-qraft/react';
import { createAPIClient } from './api'; // generated by OpenAPI Qraft CLI

import { QueryClient, dehydrate } from '@tanstack/react-query';

const queryClient = new QueryClient();

// ⬇︎ Create a Qraft API client
const api = createAPIClient({
requestFn,
queryClient,
baseUrl: 'https://api.sandbox.monite.com/v1',
});

// ⬇︎ Fetch the `getPetById` query with the `queryClient`
const pet = await api.pet.getPetById.fetchQuery(
{ parameters: { path: { petId: 1 } } }
);

console.log(pet.name);

dehydrate(queryClient); // Dehydrate the `queryClient` to be used for SSR

// ⬇︎ Qraft API client usage in React
function PetDetailsComponent() {
const { data: pet } = api.pet.getPetById.useQuery({ path: { petId: 1 } });
return <div>{pet?.name}</div>;
}