Skip to main content

Error Handling

The Case AI API uses conventional HTTP response codes and returns detailed error information in JSON format.

HTTP Status Codes

CodeDescription
200Success
201Created
400Bad Request - Invalid parameters
401Unauthorized - Invalid or missing API key
403Forbidden - Access denied
404Not Found - Resource doesn’t exist
429Too Many Requests - Rate limit exceeded
500Internal Server Error

Error Response Format

All errors return a consistent JSON structure:
{
  "error": {
    "code": "insufficient_inventory",
    "message": "Only 5 cases available for product prod_abc123",
    "details": {
      "product_id": "prod_abc123",
      "requested": 10,
      "available": 5
    }
  }
}

Error Codes

Authentication Errors

CodeDescription
unauthorizedInvalid or missing API key
forbiddenNot authorized for this resource

Validation Errors

CodeDescription
invalid_requestMalformed request body
missing_fieldRequired field is missing
invalid_fieldField value is invalid

Business Logic Errors

CodeDescription
insufficient_inventoryNot enough stock available
minimum_not_metBelow minimum order quantity
license_invalidBuyer license not valid
supplier_unavailableSupplier not accepting orders

Not Found Errors

CodeDescription
product_not_foundProduct doesn’t exist or is inactive
order_not_foundOrder doesn’t exist
supplier_not_foundSupplier doesn’t exist or isn’t verified
webhook_not_foundWebhook doesn’t exist

Rate Limiting

CodeDescription
rate_limit_exceededToo many requests

Handling Errors

async function createOrder(orderData) {
  const response = await fetch('https://api.trycase.ai/api/v1/orders', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(orderData),
  });

  if (!response.ok) {
    const { error } = await response.json();

    switch (error.code) {
      case 'insufficient_inventory':
        console.log(`Only ${error.details.available} cases available`);
        break;
      case 'minimum_not_met':
        console.log('Order does not meet minimum quantity');
        break;
      default:
        console.log(`Error: ${error.message}`);
    }
    return null;
  }

  return response.json();
}