1---2name: api3description: Consume, debug, and integrate REST APIs with best practices.4---56# API Integration Rules78## Request Gotchas9- Include `Content-Type: application/json` on POST/PUT/PATCH — omitting it causes silent 415 errors on many APIs10- Add `Accept: application/json` unless the API specifies a different format — some default to XML without it11- API keys in query params get logged in server access logs — prefer header-based auth when supported12- Tokens can expire mid-flight between request and response — handle 401 with single retry + refresh1314## Silent Failures15- Some APIs return HTTP 200 with error in response body — validate response schema, not just status code16- Watch for empty arrays vs null vs missing keys — each means something different per API17- Paginated endpoints may return 200 with empty page when offset exceeds total — check total count first1819## Retry and Resilience20- Use jittered exponential backoff: `delay = min(base * 2^attempt * (1 + random(0, 0.3)), max_delay)` with base=1s, max=30s21- Generally only retry on 429, 500, 502, 503, 504 — avoid retrying 400, 401, 403, 404 unless the API documents otherwise22- Read `Retry-After` header on 429 — it overrides your calculated backoff23- After 5+ consecutive failures to the same endpoint, back off entirely for 60s before retrying (circuit breaker)2425## Pagination Traps26- Cursor-based pagination can return duplicate items if data changes between pages — deduplicate by ID27- Some APIs change `total_count` between requests — snapshot it on first page28- If page returns fewer items than `per_page` but includes a `next` cursor, keep paginating — it's not necessarily the last page2930## Rate Limiting31- Track quota via `X-RateLimit-Remaining` header — throttle proactively before hitting 0, don't wait for 42932- Some APIs have hidden per-endpoint rate limits, not just global — monitor 429s per path33- Distribute requests evenly across the rate window instead of bursting at the start3435## Webhooks36- Implement idempotent handlers with event ID dedup — providers retry on timeout and you'll get duplicates37- Return 200 immediately, process asynchronously — webhook providers timeout at 5-30s38- Verify webhook signatures when the provider supports them — don't trust payload origin without cryptographic proof39- Log the raw webhook body before parsing — invaluable when the provider changes their schema without notice4041## Debugging Production Issues42- Log: method, URL, status code, response time, and `X-Request-Id` header for every API call43- APIs that work in dev but fail in prod: check IP allowlists, TLS version, SNI, and egress proxy settings44- When response data looks wrong, compare against the OpenAPI/Swagger spec — the spec is often more current than human-written docs