Error Handling

Understanding API error codes and how to handle them.

Error Response Format

All errors follow a consistent format:

{
  "error": {
    "code": "invalid_request",
    "message": "The 'message' field is required.",
    "param": "message"
  },
  "meta": {
    "request_id": "req_abc123"
  }
}

HTTP Status Codes

StatusDescription
200Success
400Bad Request — Invalid parameters
401Unauthorized — Invalid or missing API key
403Forbidden — Insufficient permissions
404Not Found — Resource doesn't exist
429Too Many Requests — Rate limit exceeded
500Internal Server Error — Something went wrong

Error Codes

CodeStatusDescription
invalid_request400Request body is malformed or missing required fields
invalid_api_key401API key is invalid or expired
insufficient_permissions403API key lacks required permissions
not_found404The requested resource was not found
rate_limit_exceeded429Too many requests in a given time period
internal_error500An unexpected error occurred

Retry Strategy

For 429 and 5xx errors, implement exponential backoff:

async function fetchWithRetry(url, options, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    const response = await fetch(url, options);

    if (response.ok) return response;

    if (response.status === 429 || response.status >= 500) {
      const delay = Math.pow(2, i) * 1000;
      await new Promise(r => setTimeout(r, delay));
      continue;
    }

    throw new Error(`API error: ${response.status}`);
  }
  throw new Error('Max retries exceeded');
}

On this page