api-client-patterns

Provide patterns for typed API clients with fetch wrappers and Zod validation.

Updated Feb 20, 2026
One-click install
npx skills add https://github.com/saajunaid/junai --skill api-client-patterns-saajunaid
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: api-client-patterns
Source: https://github.com/saajunaid/junai/tree/main/.github/skills/coding/api-client-patterns
Command: npx skills add https://github.com/saajunaid/junai --skill api-client-patterns-saajunaid

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Typed, maintainable API client patterns for consuming REST APIs and tRPC, enabling reliable integration between frontend and backend.

Core Features & Use Cases

  • Typed fetch wrappers with Zod validation
  • API client factory with auth injection
  • TanStack Query integration for data fetching and caching
  • End-to-end tRPC types and OpenAPI codegen support
  • Handles error envelopes and discriminated unions

Quick Start

Create a typed API client using the provided base URL and start calling your endpoints with automatic validation.

Frequently Asked Questions about api-client-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I build a type-safe API client in TypeScript?

Build a type-safe API client by using typed fetch wrappers, Zod for response validation, and client factories for auth injection. This approach ensures end-to-end type safety and robust error handling across your frontend and backend applications.

How do I validate API responses with Zod in a fetch wrapper?

Validate API responses by integrating Zod schemas directly within typed fetch wrappers. This mechanism parses and validates incoming data against your defined TypeScript types, preventing runtime errors from unexpected API response shapes.

Can I integrate tRPC with TanStack Query for data fetching?

Yes, you can integrate tRPC with TanStack Query. The patterns provide integration support for data fetching and caching, enabling fully typed, scalable data synchronization between your client and server without losing end-to-end type inference.

What is the best way to handle API error envelopes in TypeScript?

Handle API error envelopes using discriminated unions. This TypeScript pattern allows your API client to strictly type and manage different error states, ensuring robust and predictable error handling across your application.

Does this API client pattern support OpenAPI codegen?

Yes, the patterns support OpenAPI codegen to generate end-to-end types. This feature allows you to automatically generate TypeScript types from your OpenAPI specifications, keeping your API client composable and maintainable.

How do I inject authentication into a scalable API client?

Inject authentication by using an API client factory pattern. This creates a composable client where auth tokens are injected automatically, allowing your API client to scale securely across different frontend and backend environments.