04 / 22

What are the two main APIs in RTK Query? (createApi and fetchBaseQuery)

Difficulty: 4/10
createApi, fetchBaseQuery, caching

RTK Query uses two main APIs — createApi and fetchBaseQuery — that work together to simplify data fetching, caching, and synchronization in Redux Toolkit.

1. createApi
  1. 1

    createApi is the core function used to define an API service in RTK Query.

  2. 2

    It specifies the reducer path, base query, and a set of endpoints (queries and mutations).

  3. 3

    It automatically generates hooks like useGetPostsQuery or useAddPostMutation for React components.

2. fetchBaseQuery
  1. 1

    fetchBaseQuery is a small wrapper around the native fetch API.

  2. 2

    It serves as the default base query function used inside createApi.

  3. 3

    It handles common tasks such as setting base URLs, headers, and parsing JSON responses.

Example: Using createApi and fetchBaseQuery Together

In summary, fetchBaseQuery performs the network requests, while createApi defines how those requests integrate with Redux state and React components.

Scenario Questions

0-2 years experience

  1. 1Imagine you need to fetch a list of products in a React component using RTK Query. Walk me through how you'd set up the API slice with createApi and fetchBaseQuery, and which hook you'd use in the component.
  2. 2If you forget to export the generated hook from your api slice, what error would you see when trying to use it in a component?

2-5 years experience

  1. 1You added a new endpoint to an existing createApi slice, but the component still receives stale data. What could be causing this, and how would you fix it?
  2. 2During a code review, a teammate suggests replacing fetchBaseQuery with axios. What trade‑offs would you discuss, and how could you keep using fetchBaseQuery while adding custom request logic?
  3. 3Your app needs to include an Authorization header on every request. Show how you'd configure fetchBaseQuery to inject the token, and explain where you'd store the token.

5-8 years experience

  1. 1Our application has dozens of endpoints and we notice memory pressure from cached data. How would you adjust createApi's caching behavior to mitigate this while still benefiting from automatic cache invalidation?
  2. 2We need to support optimistic updates for a mutation that updates a user's profile. Describe how you'd implement this using createApi, and what considerations you have for rollback on error.
  3. 3Explain how you would structure multiple feature‑specific API slices that share a common base URL and error handling, using fetchBaseQuery and createApi.

8+ years experience

  1. 1The company is migrating from a legacy custom data‑fetching layer to RTK Query across multiple services. What architectural guidelines would you establish for using createApi and fetchBaseQuery to ensure consistency and ease of onboarding?
  2. 2Discuss the implications of using a single global createApi instance versus multiple per‑domain instances in a large monorepo. How does this affect code ownership, bundle size, and testing?
  3. 3We plan to introduce server‑side rendering with data prefetching. How would you adapt fetchBaseQuery and the generated endpoints to work both on the client and Node.js environments?

Follow-up Questions

  • How would you add custom headers to every request using fetchBaseQuery?
  • What happens if you call a generated hook before the api slice is added to the store?
  • Can you describe how cache invalidation works for a mutation endpoint?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.