defensive-api-handling

Parse JSON, fetch with timeouts, retry, and validate API responses.

8|2|Updated Jan 15, 2026
One-click install
npx skills add https://github.com/bradtaylorsf/alphaagent-team --skill defensive-api-handling
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: defensive-api-handling
Source: https://github.com/bradtaylorsf/alphaagent-team/tree/main/plugins/aai-core/skills/defensive-api-handling
Command: npx skills add https://github.com/bradtaylorsf/alphaagent-team --skill defensive-api-handling

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill prevents application crashes caused by unexpected API responses, such as malformed JSON, HTML error pages, or non-200 status codes.

Core Features & Use Cases

  • Safe JSON Parsing: Robustly parses JSON responses, handling errors and incorrect content types.
  • Fetch with Timeout: Implements timeouts for API requests to prevent indefinite hangs.
  • Retry Logic: Automatically retries failed API requests with configurable delays.
  • Type-Safe Responses: Ensures API responses conform to expected structures using type guards.
  • Error Boundary: Wraps API calls in an error boundary to catch and log exceptions gracefully.
  • Use Case: When integrating with a third-party API, use this Skill to ensure your application remains stable even if the API returns an error or unexpected data format.

Quick Start

Use the defensive-api-handling skill to safely call the '/api/users' endpoint.

Frequently Asked Questions about defensive-api-handling

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

FAQPage Schema
How do I handle malformed JSON or HTML error pages from an API in TypeScript?

To handle malformed JSON or HTML error pages from an API in TypeScript, use safe JSON parsing functions that catch parsing exceptions and validate response content types before processing the data.

What is the best way to prevent fetch requests from hanging indefinitely?

The best way to prevent fetch requests from hanging indefinitely is to implement fetch with timeouts, which aborts the request and triggers error handling logic if the server does not respond within a specified timeframe.

How do I automatically retry failed API requests with configurable delays?

To automatically retry failed API requests with configurable delays, implement retry logic that re-executes the fetch call upon failure, applying a specified delay between each retry attempt to ensure resilience.

Can I validate API responses to ensure they match expected TypeScript structures?

Yes, you can validate API responses to ensure they match expected TypeScript structures by applying type-safe response validation, which uses type guards to verify that the returned data conforms to the required schema.

How does an error boundary work when wrapping API calls?

An error boundary wraps API calls to catch exceptions gracefully, preventing unexpected non-200 status codes or runtime errors from crashing the application while logging the exceptions for debugging.

Why does my application crash when a third-party API returns unexpected data formats?

Your application crashes when a third-party API returns unexpected data formats because standard parsing assumes valid JSON, so implementing defensive API handling with safe parsing and error boundaries is required to maintain stability.