Api frontend integration

From rnavarych/alpha-engineer · View interactive version

Api frontend integration is an AI agent skill from the GitHub repository rnavarych/alpha-engineer. Integrate APIs with frontend applications using type-safe patterns: tRPC for end-to-end type safety, REST client generation (openapi-typescript, orval), TanStack Query / SWR for data fetching, optimistic updates, and error handling.

How to use this skill

Skill file

API-Frontend Integration

When to Use

Activate when connecting a backend API to a frontend application, setting up data-fetching patterns, implementing optimistic updates, or establishing type-safe API contracts.

Integration Strategy Selection

| Approach | Type Safety | Setup Cost | Best For |

|-----------------------|-------------|------------|----------------------------------|

| tRPC | Full E2E | Low | Monorepos, same-team ownership |

| OpenAPI codegen | Generated | Medium | REST APIs, multi-team |

| GraphQL + codegen | Generated | Medium | Complex relational data queries |

| Manual fetch + Zod | Runtime | Low | Simple APIs, third-party services|

tRPC Integration (Recommended for Monorepos)

  1. Define routers in packages/api/src/routers/ with input validation via Zod.
  2. Export the AppRouter type -- never the runtime object -- to the frontend.
  3. Create a tRPC client with httpBatchLink or httpSubscriptionLink.
  4. Wrap the app with trpc.Provider and QueryClientProvider.
  5. Use trpc.useQuery() and trpc.useMutation() -- types flow automatically.

REST Client Generation

Data Fetching Patterns (TanStack Query / SWR)

// Query with proper typing, stale time, and error handling
const { data, isLoading, error } = useQuery({
  queryKey: ['users', filters],
  queryFn: () => api.users.list(filters),
  staleTime: 5 * 60 * 1000,
  retry: 3,
  retryDelay: (attempt) => Math.min(1000 * 2 ** attempt, 10000),
});

Optimistic Updates

  1. Use onMutate to snapshot previous data and apply the optimistic change.
  2. Use onError to rollback to the snapshot on failure.
  3. Use onSettled to invalidate queries and refetch the authoritative state.
  4. Show a subtle indicator (toast, inline badge) so the user knows the change is pending.

Error Handling Strategy

Loading and Empty States

Common Pitfalls

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.