Skip to main content

Error Handling

The Char AI API uses conventional HTTP status codes and returns structured error responses.

HTTP Status Codes

CodeMeaning
200Success
201Created
204No Content
400Bad Request
401Unauthorized
403Forbidden
404Not Found
422Validation Error
429Rate Limited
500Server Error

Error Response Format

All errors follow a consistent structure:
{
  "code": "VALIDATION_ERROR",
  "status": 422,
  "message": "Invalid request parameters",
  "errors": [
    {
      "field": "allowed_origins",
      "message": "Must be an array of valid URLs"
    }
  ]
}

Common Error Codes

UNAUTHORIZED
{
  "code": "UNAUTHORIZED",
  "status": 401,
  "message": "Missing or invalid authentication token"
}
Check that your JWT token is valid and not expired.

Rate Limiting

When rate limited, you’ll receive:
{
  "code": "RATE_LIMITED",
  "status": 429,
  "message": "Too many requests",
  "retry_after": 60
}
Respect the retry_after value and implement exponential backoff.

Handling Errors

try {
  const response = await fetch('/api/sites', {
    headers: { Authorization: `Bearer ${token}` }
  });

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

  return response.json();
} catch (error) {
  console.error('API Error:', error.message);
}