Error Handling
All API responses follow a consistent format. Understanding error responses helps you build robust integrations.
Response Format
Success Response
All successful API responses follow this format:
{
"success": true,
"data": {
// Response data here
}
}
Error Response
All error responses follow this format:
{
"success": false,
"error": {
"code": "ERROR_CODE",
"message": "Human-readable error message"
}
}
HTTP Status Codes
Response Fields
| Code | Description | Common Causes |
|---|---|---|
200 | Success | Request completed successfully |
201 | Created | Resource created successfully |
400 | Bad Request | Invalid input, missing required fields, validation errors |
401 | Unauthorized | Invalid or missing API key |
403 | Forbidden | Insufficient scopes for the requested operation |
404 | Not Found | Resource doesn't exist or has been deleted |
413 | Payload Too Large | Request body exceeds size limit |
429 | Too Many Requests | Rate limit exceeded |
500 | Internal Server Error | Server error (should be retried) |
Error Codes
Common error codes you may encounter:
Response Fields
| Error Code | Description | HTTP Status |
|---|---|---|
INVALID_API_KEY | API key is invalid or missing | 401 |
INSUFFICIENT_SCOPES | API key doesn't have required scopes | 403 |
RESOURCE_NOT_FOUND | Requested resource doesn't exist | 404 |
VALIDATION_ERROR | Request validation failed | 400 |
RATE_LIMIT_EXCEEDED | Rate limit exceeded | 429 |
INTERNAL_ERROR | Server error | 500 |
Handling Errors
Client Errors (4xx)
Client errors indicate issues with your request that you can fix:
if (!response.ok) {
const error = await response.json();
if (response.status === 400) {
// Validation error - check your request
console.error('Validation error:', error.error.message);
} else if (response.status === 401) {
// Authentication error - check your API key
console.error('Authentication error:', error.error.message);
} else if (response.status === 403) {
// Scope error - check your API key scopes
console.error('Scope error:', error.error.message);
} else if (response.status === 404) {
// Resource not found
console.error('Resource not found:', error.error.message);
}
}
Server Errors (5xx)
Server errors are typically transient and should be retried:
if (response.status >= 500) {
// Retry with exponential backoff
const maxRetries = 3;
for (let i = 0; i < maxRetries; i++) {
await new Promise(resolve => setTimeout(resolve, Math.pow(2, i) * 1000));
const retryResponse = await fetch(url, options);
if (retryResponse.ok) {
return retryResponse;
}
}
}
Validation Errors
When you receive a 400 Bad Request with VALIDATION_ERROR, the error message will indicate which fields are invalid:
{
"success": false,
"error": {
"code": "VALIDATION_ERROR",
"message": "Invalid email address format",
"field": "email"
}
}
Best Practices
- Always check response status before parsing JSON
- Handle specific error codes appropriately
- Implement retry logic for 5xx errors and rate limits
- Log errors with context for debugging
- Never log API keys in error messages or logs
- Validate input before sending requests to avoid validation errors
For rate limit errors (429), check the retryAfter field in the error response to know how long to wait before retrying.