Error Handling
Learn how to handle API errors gracefully in your integration.
Error Response Format
When an error occurs, the API returns a JSON response with success: false and details about the error:
{
"success": false,
"message": "The given data was invalid.",
"errors": {
"email": ["The email field is required."],
"password": ["The password must be at least 8 characters."]
}
}HTTP Status Codes
The API uses standard HTTP status codes to indicate success or failure:
Request succeeded
Resource created successfully
Request succeeded, no content to return (e.g., DELETE)
Invalid request body or parameters
Missing or invalid authentication token
Authenticated but lacking required permissions
Requested resource does not exist
Validation failed for the request data
Rate limit exceeded
Server encountered an unexpected error
Validation Errors
Validation errors (400/422) include an errors object with field-specific error messages:
{
"success": false,
"message": "The given data was invalid.",
"errors": {
"email": [
"The email field is required.",
"The email must be a valid email address."
],
"date_of_birth": [
"The date of birth must be a date before today."
],
"phone": [
"The phone format is invalid."
]
}
}Each field may have multiple error messages. Display all messages to help users correct their input.
Error Handling Examples
Here's how to implement comprehensive error handling in your application:
Common Error Scenarios
Access tokens expire after 24 hours. Implement token refresh or re-authentication logic.
{
"success": false,
"message": "Unauthenticated."
}The user is authenticated but lacks permission for the requested action.
{
"success": false,
"message": "This action is unauthorized."
}The requested resource doesn't exist or has been deleted.
{
"success": false,
"message": "No query results for model [App\\Models\\People] with ID 999."
}Best Practices
Always Check success Field
Don't rely solely on HTTP status codes. Always check the success field in the response body.
Display User-Friendly Messages
Map API error messages to user-friendly text. Some error messages are technical and should be translated for end users.
Log Errors for Debugging
Log full error responses server-side for debugging, but only show relevant information to users.
Implement Retry Logic
For transient errors (429, 500, 503), implement retry logic with exponential backoff.
Next Steps
Learn about webhooks for real-time event notifications.
