---
title: "Integrating the SWR library with a type-safe API client"
description: "incident.io replaced its homegrown useFetchData hook with SWR (stale-while-revalidate) in its React dashboard, while keeping the type safety of its OpenAPI-generated TypeScript client."
url: "https://swe.quest/articles/integrating-the-swr-library-with-a-type-safe-api-client-1c04e2ee619b8cd1"
canonical: "https://incident.io/blog/integrating-the-swr-library"
source: "incident.io"
published: "2023-08-23"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["typescript","react","caching","frontend","type-systems"]
---

# Integrating the SWR library with a type-safe API client

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2023-08-23, 9 min read, How-to. Original: [https://incident.io/blog/integrating-the-swr-library](https://incident.io/blog/integrating-the-swr-library)

## Summary

incident.io replaced its homegrown useFetchData hook with SWR (stale-while-revalidate) in its React dashboard, while keeping the type safety of its OpenAPI-generated TypeScript client. Rather than code-generating a hook per endpoint, they wrote a single generic useAPI hook. It constrains the API name to client methods excluding BaseAPI utilities, then uses conditional types, Parameters and Awaited\<ReturnType\> to infer request and response types. Using \[api, request\] as the cache key gives consistent shared caching and prevents key collisions, so one line like useAPI("customFieldsList", undefined) is fully type-checked.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [TypeScript](https://swe.quest/tags/typescript.md) (Languages)
- [React](https://swe.quest/tags/react.md) (Web & mobile)
- [Caching](https://swe.quest/tags/caching.md) (Backend & APIs)
- [Frontend](https://swe.quest/tags/frontend.md) (Web & mobile)
- [Type systems](https://swe.quest/tags/type-systems.md) (Compilers & runtimes)

## Prerequisites

- React hooks ([easy posts on React](https://swe.quest/tags/react.md?difficulty=easy))
- TypeScript generics ([easy posts on TypeScript](https://swe.quest/tags/typescript.md?difficulty=easy))
- OpenAPI client generation

## Keywords

SWR, OpenAPI Generator, Next.js, React

## Related posts

- [How we built it: incident.io Status Pages](https://swe.quest/articles/how-we-built-it-incident-io-status-pages-37acc4f93c86c23d.md): incident.io · 2023-04-19 · Medium · 8 min read · Caching, React, Frontend, Serverless, CDN & edge
  incident.io built its Status Pages product in three months on Vercel and Next.js, using Incremental Static Regeneration with per-fetch revalidate times (e.g. 30s) so the backend serves only a couple of requests per minute regardless of traffic spikes.
- [Introducing @neon/sdk, our new TypeScript client for the Neon API](https://swe.quest/articles/introducing-neon-sdk-our-new-typescript-client-for-the-neon-api-29d9cb594b8c1451.md): Neon · 2026-07-13 · Medium · 6 min read · TypeScript, API design, PostgreSQL, Developer tools
  @neon/sdk is a fetch-based, zero-dependency, ESM-only TypeScript client for the Neon API, replacing the axios-based @neondatabase/api-client.
- [Using React Query with Next.js App Router and Supabase Cache Helpers](https://swe.quest/articles/using-react-query-with-next-js-app-router-and-supabase-cache-helpers-aaf0cb6afda9d12f.md): Supabase · 2024-01-12 · Medium · 7 min read · React, Frontend, Caching, TypeScript
  React Query can be paired with supabase-js in a Next.js App Router app by wrapping the root layout in a client-side QueryClientProvider and creating typed browser and server Supabase clients via @supabase/ssr.
- [Build a serverless API using Cloudflare Workers, Drizzle ORM, and Neon](https://swe.quest/articles/build-a-serverless-api-using-cloudflare-workers-drizzle-orm-and-neon-f83c472c9d399a17.md): Neon · 2023-05-30 · Medium · 9 min read · Serverless, PostgreSQL, TypeScript, Schema migrations
  A step-by-step setup for a serverless TypeScript API on Cloudflare Workers that uses Hono for routing, Drizzle ORM for schema definition and migrations, and Neon serverless Postgres for storage.
- [Enhancing the Mock Server: A User Interface Approach](https://swe.quest/articles/enhancing-the-mock-server-a-user-interface-approach-ff47adf3053feadf.md): Zalando Engineering · 2024-04-24 · Medium · 7 min read · Frontend, React, Testing, Developer productivity
  Zalando's team built a UI on top of mirage.js so product managers and designers can switch which mocked response each API endpoint returns on a staging build, without code changes or redeploys.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
