Error Handling

Learn how to handle errors in APIVerve APIs, understand HTTP status codes, and implement proper error handling in your applications.

HTTP Status Codes

APIVerve uses conventional HTTP response codes to indicate the success or failure of an API request:

CodeStatusDescription
200OKRequest successful
400Bad RequestInvalid parameters or malformed request
401UnauthorizedInvalid or missing API key
403ForbiddenAPI key suspended or insufficient permissions
429Too Many RequestsRate limit exceeded
500Internal Server ErrorServer error, please try again

Error Response Format

All error responses follow a consistent format across all response types (JSON, XML, YAML):

JSON Error Response

JSON Error Response
400 Bad Request
{
  "status": "error",
  "error": "Description of what went wrong",
  "data": null
}

XML Error Response

XML Error Response
400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <status>error</status>
  <error>Description of what went wrong</error>
  <data></data>
</response>

YAML Error Response

YAML Error Response
400 Bad Request
status: error
error: "Description of what went wrong"
data: null

Common Error Scenarios

Authentication Errors

Missing API Key
401 Unauthorized
{
  "status": "error",
  "error": "API key is required",
  "data": null
}

Rate Limiting

Rate Limit Exceeded
429 Too Many Requests
{
  "status": "error",
  "error": "Rate limit exceeded. Try again in 60 seconds",
  "data": null
}

Parameter Validation

Invalid Parameters
400 Bad Request
{
  "status": "error",
  "error": "Required parameter 'url' is missing",
  "data": null
}

Error Handling Best Practices

Handling Errors Gracefully
  • Always check the HTTP status code first
  • Parse the error message from the response body
  • Implement retry logic for 500-level errors
  • Handle rate limits with exponential backoff
  • Log errors for debugging purposes

JavaScript Example

Error Handling in JavaScript
async function makeAPIRequest(endpoint, params) {
  try {
    const response = await fetch(`https://api.apiverve.com/v1/${endpoint}`, {
      headers: {
        'X-API-Key': 'your-api-key',
        'Content-Type': 'application/json'
      },
      method: 'POST',
      body: JSON.stringify(params)
    });

    const data = await response.json();

    if (!response.ok) {
      // Handle specific error cases
      switch (response.status) {
        case 401:
          throw new Error('Invalid API key');
        case 429:
          throw new Error('Rate limit exceeded. Please try again later');
        case 500:
          throw new Error('Server error. Please try again');
        default:
          throw new Error(data.error || 'Unknown error occurred');
      }
    }

    return data;
  } catch (error) {
    console.error('API Error:', error.message);
    throw error;
  }
}

What's Next?

Continue your journey with these recommended resources

Was this page helpful?

Help us improve our documentation