API Reference
The Expand API uses conventional HTTP response codes to indicate the success or failure of a request.
HTTP Status Codes
| Code | Description |
|---|
200 | Success - The request completed successfully |
400 | Bad Request - Invalid parameters or request body |
401 | Unauthorized - Invalid or missing API key |
429 | Too Many Requests - Rate limit exceeded |
500 | Internal Error - Server error, includes fetch failures |
503 | Service Unavailable - The service is temporarily unavailable |
All errors follow a consistent format with a _tag field identifying the error type:
{
"_tag": "ErrorType",
// Additional fields depend on error type
}
Error Types
Validation Errors (400)
Returned when the request body doesn't match the expected schema:
{
"_tag": "HttpApiDecodeError",
"message": "Invalid request parameters",
"issues": [
{
"_tag": "Missing",
"path": [
Common validation issues:
- Missing required
url field
- Invalid URL format (must be
http:// or https://)
- Invalid regex pattern in
include.links.includePatterns
- Empty
search.query
Authentication Errors (401)
Returned when authentication fails:
{
"_tag": "AuthFailed",
"reason": "InvalidApiKey",
"description": "The provided API key is not valid"
}
| Reason | Description |
|---|
InvalidApiKey | The API key is missing or invalid |
InvalidToken | The bearer token is invalid |
InvalidSession | The session has expired |
InvalidTenant | The organization was not found |
Rate Limit Errors (429)
Returned when you exceed your rate limit. See Rate Limiting for details.
{
"_tag": "TooManyRequests"
}
Fetch Errors (500)
Returned when content extraction fails:
{
"_tag": "FetchError",
"cause": "Navigation timeout: page took too long to load"
}
- Target page is unreachable or blocked
- Navigation timeout (page took too long to load)
- Content capture failed
- Screenshot capture failed
Service Errors (503)
Returned when the service is temporarily unavailable:
{
"_tag": "ServiceUnavailable"
}
This typically indicates a temporary issue. Retry your request after a short delay.
Handling Errors
With SDKs
import Expand, { ExpandError, APIError, RateLimitError } from 'expandai'
const client = new Expand({ apiKey: '{{API_KEY}}' })
try {
const result = await client.fetch({ url: 'https://example.com' })
} catch (error) {
if (error instanceof RateLimitError) {
// Handle rate limiting - wait and retry
console.log('Retry after:', error.retryAfter)
With cURL
When using cURL or raw HTTP, check the response status code and parse the JSON body for error details:
response=$(curl -s -w "\n%{http_code}" -X POST https://api.expand.ai/v1/fetch \
-H "x-expand-api-key: {{API_KEY}}" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}')
status_code=$(echo "$response" | tail -n 1)
body=$(echo "$response
Best Practices
- Always check status codes before processing responses
- Log error details including the
_tag field for debugging
- Implement retries for transient errors (
429, 503)
- Validate inputs before making requests to avoid
400 errors
- Handle errors gracefully in your application to provide good user experience
"url"
],
"message": "is missing"
}
]
}
} else if (error instanceof APIError) {
// Handle API errors (4xx, 5xx)
console.log('Status:', error.status)
console.log('Message:', error.message)
} else if (error instanceof ExpandError) {
// Handle SDK errors
console.log('Error:', error.message)
}
}
from expandai import Expand, ExpandError, APIError, RateLimitError
client = Expand(api_key="{{API_KEY}}")
try:
result = client.fetch(url="https://example.com")
except RateLimitError as e:
# Handle rate limiting - wait and retry
print(f"Retry after: {e.retry_after}")
except APIError as
"
| sed
'$d'
)
if [ "$status_code" -ne 200 ]; then
echo "Error ($status_code): $body"
fi
e:
# Handle API errors (4xx, 5xx)
print(f"Status: {e.status_code}, Message: {e.message}")
except ExpandError as e:
# Handle SDK errors
print(f"Error: {e.message}")