Error Handling
The Scrappa API uses standard HTTP status codes and returns errors in a consistent JSON format for easy handling.
Error Response Format
All errors follow a consistent JSON structure:
{
"message": "Error description",
"error": "Optional error type"
}
HTTP Status Codes
Success
Request completed successfully
Bad Request
Invalid parameters or malformed request
Unauthenticated
Missing or invalid API key
Forbidden
No active subscription or out of credits
Not Found
Endpoint or resource not found
Validation Error
Request validation failed
Internal Server Error
Server error occurred
Bad Gateway
Invalid response from external service
Service Unavailable
Service temporarily unavailable
Common Error Examples
Unauthenticated
Missing or invalid API key
{
"message": "Unauthenticated"
}
Forbidden
No active subscription or out of credits
Validation Error
Invalid request parameters
{
"message": "Validation failed",
"errors": {
"q": ["The q field is required."],
"location": ["The location must be a valid location."]
}
}
Error Handling Best Practices
Example Error Handling
Here's how to properly handle errors in your application:
async function handleApiRequest(url, apiKey) {
try {
const response = await fetch(url, {
headers: {
'X-API-KEY': apiKey
}
});
if (!response.ok) {
const error = await response.json();
switch (response.status) {
case 401:
throw new Error('Invalid API key. Please check your credentials.');
case 403:
throw new Error('Access denied. Please check your subscription.');
case 422:
const validationErrors = error.errors || {};
throw new Error(`Validation failed: \${JSON.stringify(validationErrors)}`);
default:
throw new Error(error.message || 'An error occurred');
}
}
return await response.json();
} catch (error) {
console.error('API Error:', error);
throw error;
}
}
Pro Tip
Use the interactive Playground to test API calls and see error responses in real-time before implementing in your code.