Skip to content
Backend & APIChapter 27 / 57Intermediate6 min read

One Fetch Client

Build one API client wrapper on top of fetch that handles auth, headers, response parsing and errors, so components never call fetch directly.

On this page

1. What is this practice?

fetch is a low level tool. On every call you have to remember the Content-Type header, JSON.stringify, the res.ok check, how to parse the error body, and what to do on a 401. A client wrapper writes all of this once.

2. Why does it matter?

fetch does not throw on 404 or 500. It only throws when the network fails. A developer who does not know this writes await res.json() and renders the error body as if it were data.

Now imagine 30 components each doing their own fetch. The session expires. Some components send the user to login, some show an empty list, some crash. Later you need a new header on every request, and you have to change 30 files and miss one.

Every component has its own fetchOne client
res.ok is checked sometimes and skipped other timesEvery response is checked in one place
Each screen behaves differently on 401One path on 401: clear state and sign in
A new header means editing 30 filesA new header is one line
The error shape is guessed differently everywhereEvery call returns a typed result
URLs and JSON noise inside the componentThe component just calls listProjects()

3. When to follow it

LevelHow importantWhy
Learning projectGood to have, a small wrapper is enoughThe benefit shows up after two or three calls
Production codeRequiredAuth and error handling should have exactly one implementation
Senior levelDesign the client and the retry policyWhen to retry, when not to, timeouts, tracing headers

When to learn it: right after you understand the Fetch API and Async / Await.

4. Bad vs good code

Bad: fetch inside the component

tsx
"use client";

import { useEffect, useState } from "react";

export function ProjectList() {
  const [projects, setProjects] = useState([]);

  useEffect(() => {
    fetch("/api/projects")
      .then((res) => res.json())
      .then((data) => setProjects(data.projects));
  }, []);

  return <ul>{projects.map((p: any) => <li key={p.id}>{p.name}</li>)}</ul>;
}

There is no res.ok check, so on an error data.projects is undefined and map crashes. The type is any. There is no plan for 401.

Good: one client, typed functions

File lib/api/client.ts:

ts
import type { ApiResult, RequestOptions } from "./client.types";

export async function apiRequest<T>(path: string, options: RequestOptions = {}): Promise<ApiResult<T>> {
  const response = await fetch(path, {
    method: options.method ?? "GET",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(options.body),
    credentials: "same-origin",
  });

  if (response.status === 401) {
    window.location.assign("/sign-in");
    return { ok: false, status: 401, error: "Unauthorized" };
  }

  if (!response.ok) {
    const failure: { error: string } = await response.json();
    return { ok: false, status: response.status, error: failure.error };
  }

  return { ok: true, data: (await response.json()) as T };
}

File lib/api/client.types.ts:

ts
export type RequestOptions = {
  method?: "GET" | "POST" | "PATCH" | "DELETE";
  body?: unknown;
};

export type ApiResult<T> =
  | { ok: true; data: T }
  | { ok: false; status: number; error: string };

A small typed function for each endpoint, taking its types from the contract:

ts
export function listProjects() {
  return apiRequest<ListProjectsResponse>("/api/projects");
}

The component now just uses it, and handles both outcomes cleanly:

tsx
const result = await listProjects();
if (!result.ok) {
  setError(result.error);
  return;
}
setProjects(result.data.projects);

What is the difference?

ThingBad codeGood code
res.okForgottenChecked once in the client
TypesanyApiResult<ListProjectsResponse>
401NothingSend to sign in, no retry
ErrorCrash{ ok: false, error } that the UI can show
ChangeEvery componentOnly client.ts

as T is fine here because this is our own API, tied to a shared contract. If the response comes from a third party API, parse it with Zod.

5. How to apply it

Steps to build the client
One apiRequest function in lib/api/client.ts
Auth, headers and timeout live here
Turn the response into an ok or error result
A typed function per endpoint, using the contract types
Components only call these functions

Add a lint rule that forbids fetch in components, so the rule enforces itself.

6. Exceptions and common mistakes

Next.js Server Components do not need to fetch their own API. They can call the service function directly, like await listProjectsForUser(user.id). The client wrapper is for components that run in the browser, or for a mobile app. Third party SDKs (payments, maps) bring their own client, so you do not need to wrap them.

Mistake: Assuming fetch will throw on an error

Why? fetch resolves on 404 and 500 too, so the error body becomes data and ends up in the UI.

Better: Check response.ok in the client and return the error as a separate result.

Mistake: Retrying the request on 401

Why? With an expired session this turns into a retry loop and puts useless load on the server.

Better: On 401, clear local state and send the user to sign in.

Mistake: Keeping the token in localStorage and attaching it by hand everywhere

Why? Every component needs to know about the token, and localStorage can be read through XSS.

Better: Use an httpOnly cookie on the web and let the client only handle the credentials setting.

7. What to learn before and after

Final quick revision

CheckQuestion
One clientDo all backend calls go through apiRequest?
res.okDoes every non 2xx response become an error result?
TypedDoes every call get its return type from the contract?
401Is there one path for 401, with no retry?
ComponentsIs there a direct fetch in any component?