Error Handling

This guide explains how to handle errors in the Cloutfit API and provides examples of possible API exceptions.

Error Types

Cloutfit uses conventional HTTP response codes to indicate the success or failure of an API request. In general:

  • Codes in the 2xx range indicate success.
  • Codes in the 4xx range indicate an error that failed given the information provided (e.g., a required parameter was omitted, authentication failed, etc.).
  • Codes in the 5xx range indicate an error with Cloutfit's servers.

All error responses include a JSON object with an error message to help you understand what went wrong.

Error Response Format
{
  "error": {
    "code": "error_code",
    "message": "A human-readable message providing more details about the error.",
    "status": 400
  }
}

Common Error Codes

Here are some common error codes you might encounter when using the Cloutfit API:

Error CodeStatusDescription
authentication_required401No valid API key provided.
invalid_request400The request was unacceptable, often due to missing a required parameter.
not_found404The requested resource doesn't exist.
rate_limit_exceeded429Too many requests hit the API too quickly.
server_error500Something went wrong on Cloutfit's end.

Handling Errors

We recommend writing code that gracefully handles all possible API exceptions. Here are some examples of how to handle errors in different programming languages:

JavaScript
async function getInfluencer(influencerId) {
  try {
    const response = await fetch(`https://app.cloutfit.ai/v1/influencers/${influencerId}`, {
      headers: {
        'Authorization': `Bearer ${API_KEY}`
      }
    });
    
    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(`API Error: ${errorData.error.message}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('Error fetching influencer:', error);
    // Handle the error appropriately
  }
}

Expanding Responses

Many objects allow you to request additional information as an expanded response by using the expand request parameter. This parameter is available on all API requests, and applies to the response of that request only.

You can expand multiple objects at once by specifying a comma-separated list of objects to expand.

Request with Expansion
curl https://app.cloutfit.ai/v1/posts/post_1234567890?expand=influencer,products \
  -H "Authorization: Bearer YOUR_API_KEY"
Example Error Response
curl https://app.cloutfit.ai/v1/influencers/non_existent_id \
  -H "Authorization: Bearer YOUR_API_KEY"