> ## Documentation Index
> Fetch the complete documentation index at: https://docs.whatsable.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Error Handling

> Handle errors when using WhatsAble APIs: HTTP status codes, error format, and retry strategies.

# Error Handling

This guide explains how to handle errors when using WhatsAble APIs.

## HTTP Status Codes

All WhatsAble APIs use standard HTTP status codes:

* `200 OK`: Request successful
* `400 Bad Request`: Invalid request parameters
* `401 Unauthorized`: Invalid or missing API key
* `403 Forbidden`: Insufficient permissions
* `404 Not Found`: Resource not found
* `429 Too Many Requests`: Rate limit exceeded
* `500 Internal Server Error`: Server-side error

## Error Response Format

All error responses follow this format:

```json theme={null}
{
  "error": {
    "code": "error_code",
    "message": "Human-readable error message"
  }
}
```

## Common Error Codes

### Authentication Errors

* `invalid_api_key`: API key is invalid or expired
* `missing_api_key`: API key is missing from request

### Request Errors

* `invalid_phone_number`: Phone number format is incorrect
* `invalid_template`: Template ID is invalid or not found
* `missing_required_field`: Required field is missing
* `invalid_variables`: Template variables are invalid

### Rate Limit Errors

* `rate_limit_exceeded`: Too many requests in a time period
* `quota_exceeded`: Monthly quota exceeded

## Best Practices

1. **Always Check Status Codes**: Don't assume success
2. **Implement Retry Logic**: For 429 and 500 errors
3. **Log Errors**: For debugging and monitoring
4. **Handle Timeouts**: Set appropriate timeout values
5. **Validate Input**: Before making API calls

## Example Error Handling

```javascript theme={null}
async function sendMessage() {
  try {
    const response = await fetch('https://api.insightssystem.com/api:hFrjh8a1/send_template_message_by_api', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer YOUR_TOKEN'
      },
      body: JSON.stringify({
        template: 'template_id',
        variables: {},
        phone_number: '+1234567890'
      })
    });

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.error.message);
    }

    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error sending message:', error);
    // Handle error appropriately
  }
}
```
