# Working with REST APIs in Production

> Structure an API layer with typed clients, authentication, error handling, pagination and CORS awareness. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/frontend/api-integration
Last updated: 2026-11-28

Structure an API layer with typed clients, authentication, error handling, pagination and CORS awareness.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [Server State with TanStack Query](https://www.internhack.in/roadmaps/tracks/frontend/server-state), [TypeScript Fundamentals](https://www.internhack.in/roadmaps/tracks/frontend/typescript-basics)

Calling `fetch` in a component is fine for a demo. Real apps need a consistent way to talk to a backend so auth, errors and types are handled once, not in every component.

### Build an API layer

- A small **client** module wraps fetch (or a library like `ky` or `axios`): it sets the base URL from an environment variable, adds auth headers or sends cookies with `credentials: "include"`, parses JSON and throws a typed error for non-2xx responses (remember: `fetch` does not reject on 404 or 500).
- One function per endpoint: `getProducts(params)`, `createOrder(body)`. Components and query hooks call these, never raw URLs.
- **Validate responses at the boundary** with a schema (Zod) or generate types from an **OpenAPI** spec so frontend and backend agree.

### Everyday concerns

- **REST conventions**: resources, HTTP methods, status codes and query parameters for filtering, sorting and pagination (offset or cursor based).
- **Errors**: distinguish network failures, validation errors (show them next to fields), auth errors (redirect to login) and server errors (friendly message plus retry).
- **Auth flows**: refresh an expired session transparently once, then log out if refresh fails.
- **CORS**: the browser blocks cross-origin responses unless the server allows your origin. It is fixed on the server or with a dev proxy, never by a frontend flag.
- **Mocking**: use MSW (Mock Service Worker) to develop and test against fake endpoints before the backend is ready.

### Good enough to move on

You have a typed API module with central error handling, environment-based base URL and auth, used through query hooks, plus mocks for local development.

## Key concepts

- **REST resources, methods and status codes**
- **A typed API client module**
- **Central error handling**
- **Auth headers, cookies and token refresh**
- **Pagination and filtering parameters**
- **CORS from the frontend's perspective**
- **OpenAPI type generation**
- **Mocking with MSW**

## Mini project

Create an api/ folder for a public API (or a mock with MSW): a base client with typed errors, endpoint functions validated with Zod, a 401 handler that redirects to login, and query hooks that use them.

## Check yourself

Your fetch call to /api/orders returns 500 but no catch block runs. Why, and where should you fix it?

## Free resources

- [Using the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) (MDN)
- [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) (MDN)
- [Mock Service Worker](https://mswjs.io/docs/) (MSW)

Previous: [Server State with TanStack Query](https://www.internhack.in/roadmaps/tracks/frontend/server-state) · Next: [GraphQL on the Frontend](https://www.internhack.in/roadmaps/tracks/frontend/graphql-clients)

## More in Routing, State and Data

- [Client-Side Routing](https://www.internhack.in/roadmaps/tracks/frontend/client-routing)
- [State Management Principles](https://www.internhack.in/roadmaps/tracks/frontend/state-management)
- [Client State Libraries: Zustand, Redux Toolkit and Jotai](https://www.internhack.in/roadmaps/tracks/frontend/state-libraries)
- [Server State with TanStack Query](https://www.internhack.in/roadmaps/tracks/frontend/server-state)
- [Working with REST APIs in Production](https://www.internhack.in/roadmaps/tracks/frontend/api-integration)
- [GraphQL on the Frontend](https://www.internhack.in/roadmaps/tracks/frontend/graphql-clients)
- [Forms in React with React Hook Form](https://www.internhack.in/roadmaps/tracks/frontend/react-forms)
- [Schema Validation with Zod](https://www.internhack.in/roadmaps/tracks/frontend/schema-validation)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Frontend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/frontend)
