Error Handling
How to handle errors from the Travelmode API.
Error Response Format
All errors return a JSON object with an error field:
{
"error": "Description of what went wrong"
}
Some endpoints include additional details:
{
"error": "Validation failed",
"details": {
"name": "Name is required",
"startDate": "Invalid date format"
}
}
Common Error Codes
400 Bad Request
Invalid request parameters or body.
{
"error": "Invalid trip ID format"
}
Solution: Check your request parameters match the expected format.
401 Unauthorized
Missing or invalid API key.
{
"error": "Invalid API key"
}
Solutions:
- Ensure you're including the Authorization header
- Verify your API key is correct and not expired
- Check the key hasn't been revoked
403 Forbidden
Valid API key but insufficient permissions.
{
"error": "Insufficient permissions for this action"
}
Solutions:
- Check your API key has the required scopes
- Verify you have access to the requested resource
404 Not Found
Resource doesn't exist.
{
"error": "Trip not found"
}
Solutions:
- Verify the resource ID is correct
- Check the resource hasn't been deleted
409 Conflict
Resource conflict, often a revision mismatch.
{
"error": "Revision conflict",
"currentRevision": 45,
"expectedRevision": 42
}
Solution: Fetch the latest version, merge changes, and retry.
429 Too Many Requests
Rate limit exceeded.
{
"error": "Rate limit exceeded"
}
Solution: Wait for the time specified in the Retry-After header.
500 Internal Server Error
Something went wrong on our end.
{
"error": "Internal server error"
}
Solution: Retry after a moment. If the issue persists, contact support.
Error Handling Example
Python:
import requests
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
elif response.status_code == 401:
print("Check your API key")
elif response.status_code == 429:
retry_after = int(response.headers.get('Retry-After', 60))
time.sleep(retry_after)
else:
error = response.json().get('error', 'Unknown error')
print(f"Error: {error}")
JavaScript:
const response = await fetch(url, { headers });
if (!response.ok) {
const { error } = await response.json();
switch (response.status) {
case 401:
console.error('Invalid API key');
break;
case 429:
const retryAfter = response.headers.get('Retry-After');
console.error(`Rate limited. Retry in ${retryAfter}s`);
break;
default:
console.error(`Error: ${error}`);
}
}