agentleFS
manish1803manish1803/nextjs-fullstack-skills/skills/02-fullstack-backend/rest-api-integration/SKILL.md

rest-api-integration

Skill2 starsChanged 4 months agoMITView on GitHub

Integrate third-party or internal REST APIs correctly from the frontend and server — typed fetch clients, error handling, retries, caching, and pagination. Use whenever the user is calling an external API, building an API client/SDK wrapper, integrating a third-party service, or asking how to structure API calls in a Next.js app.

  • Reads credentials

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Client-side fetch: only for public, unauthenticated endpoints or endpoints proxied through your own backend that don't need a secret.

    line 10
  2. Very specific:

    Prefer cursor-based pagination (?cursor=...) over offset-based for anything with frequently-changing data (avoids skipped/duplicated items).

    line 40
  3. Very specific:

    No response validation — trusting as type casts on external API responses

    line 49

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

REST API Integration Skill

Where API calls belong

  • Server-side by default (Server Component fetch, Server Action, or route handler) for anything using a secret API key — never expose third-party API keys to the client.
  • Client-side fetch only for public, unauthenticated endpoints or endpoints proxied through your own backend that don't need a secret.

Typed client pattern

Wrap every external API in a small typed client instead of calling fetch ad hoc across the codebase:

// lib/api/stripe-client.ts (example shape, applies to any REST API)
async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
  const res = await fetch(`${BASE_URL}${path}`, {
    ...init,
    headers: { Authorization: `Bearer ${process.env.API_SECRET}`, ...init?.headers },
  });
  if (!res.ok) throw new ApiError(res.status, await res.text());
  return res.json();
}

Validate the response shape with zod at the boundary (schema.parse(await res.json())) rather than trusting as Type casts — third-party APIs change without warning.

Caching & revalidation (Next.js fetch)

fetch(url, { next: { revalidate: 60, tags: ['products'] } }); // ISR-style caching
fetch(url, { cache: 'no-store' }); // always fresh, e.g. user-specific data
revalidateTag('products'); // call after a mutation to bust the cache on demand

Error handling & retries

  • Distinguish error types: network failure, 4xx (client error — don't retry, surface to user), 5xx/timeout (transient — retry with backoff).
  • Simple backoff: retry up to 3 times with exponential delay (e.g. 300ms, 900ms, 2700ms) only on 5xx/network errors, never on 4xx.
  • Surface actionable errors to the UI (not raw stack traces) — map API error codes to user-facing messages centrally, not per call site.

Pagination

  • Prefer cursor-based pagination (?cursor=...) over offset-based for anything with frequently-changing data (avoids skipped/duplicated items).
  • For infinite scroll in React, use a library-agnostic pattern: fetch next cursor, append to state, disable further fetch when hasMore is false.

Rate limits

  • Respect Retry-After headers; queue/backoff rather than hammering a rate-limited endpoint.
  • For high-volume server-to-server integrations, add a simple in-memory or Redis-backed rate limiter on your own proxy route if the third party doesn't provide one.

Anti-patterns to flag

  • Third-party secret API keys used directly in client components (NEXT_PUBLIC_* misuse)
  • No response validation — trusting as type casts on external API responses
  • Retrying 4xx errors (client errors won't resolve by retrying)
  • Duplicated fetch logic copy-pasted across components instead of a shared client

Example prompt this skill should trigger on

"Integrate the [X] third-party API to pull product data into our dashboard."

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.