API Errors
Learn about the different types of errors you might encounter when using the AltTextAI API and how to handle them.
Error Response Format
All API errors follow a consistent format, making it easy to handle errors in your application:
{
"error": "Error message description",
"results": [ // Optional, included for batch operations
{
"filename": "image1.jpg",
"error": "Error processing this image",
"success": false
}
]
}
HTTP Status Codes
The API uses standard HTTP status codes to indicate the success or failure of requests:
Status Code | Description | Common Causes |
---|---|---|
200 OK | Request successful | Request completed successfully |
400 Bad Request | Invalid request | Missing required fields, invalid image format |
401 Unauthorized | Authentication failed | Invalid or missing API key |
403 Forbidden | Permission denied | Insufficient credits, rate limit exceeded |
404 Not Found | Resource not found | Invalid endpoint, image URL not accessible |
500 Internal Server Error | Server error | Unexpected server error, AI model error |
Common Error Messages
Authentication Errors
{
"error": "No Authorization header found. Please include your API key in the Authorization header as 'Bearer YOUR_API_KEY'"
}
{
"error": "Invalid or inactive API key"
}
Credit-Related Errors
{
"error": "No credits remaining"
}
{
"error": "Insufficient credits for batch processing"
}
Image Processing Errors
{
"error": "No images provided. Please provide either file uploads, URLs, or base64 encoded images."
}
{
"error": "Invalid image format"
}
{
"error": "Image URL not accessible"
}
{
"error": "Image size exceeds maximum limit"
}
Error Handling Best Practices
try {
const response = await fetch('https://autoalt.app/api/v1/generate', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (!response.ok) {
const errorData = await response.json();
// Handle specific error cases
switch (response.status) {
case 401:
// Handle authentication error
break;
case 403:
// Handle insufficient credits
break;
default:
// Handle other errors
}
throw new Error(errorData.error);
}
const result = await response.json();
// Process successful response
} catch (error) {
console.error('API Error:', error.message);
}
- Always check the response status code
- Parse the error message from the response body
- Implement appropriate retry logic for transient errors
- Log errors for debugging purposes
- Handle batch processing errors individually
- Provide meaningful error messages to your users