Response Format & Error Handling

Standardized API response structure and comprehensive error handling

Success Response Structure

{
  "success": true,
  "data": {
    // Response data object or array
  },
  "message": "Operation completed successfully",
  "timestamp": 1640995200,
  "api_version": "1.0"
}

Pagination Response

{
  "success": true,
  "data": {
    "results": [/* Array of items */],
    "pagination": {
      "page": 1,
      "page_size": 20,
      "total_pages": 15,
      "total_count": 300,
      "has_next": true,
      "has_previous": false,
      "next_url": "/api/v1/affiliate/affiliates/?page=2",
      "previous_url": null
    }
  }
}

Error Response Structure

{
  "success": false,
  "error": {
    "code": "ERROR_CODE",
    "message": "Human readable error message",
    "status": 400,
    "details": {
      "additional_info": "Optional additional details"
    }
  },
  "timestamp": 1640995200,
  "api_version": "1.0"
}

Common Error Codes

Code Description Status Solution
AUTHENTICATION_FAILED Invalid or missing API key/token 401 Check API key format and validity
PERMISSION_DENIED Insufficient permissions 403 Verify API key permissions
NOT_FOUND Resource not found 404 Check resource ID and existence
VALIDATION_ERROR Invalid input data 400 Review request data format
RATE_LIMIT_EXCEEDED Too many requests 429 Implement rate limiting, retry with backoff
BUSINESS_ERROR Business logic violation 400 Review business rules and constraints
INTERNAL_ERROR Server error 500 Contact support if persistent

Validation Error Example

{
  "success": false,
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Validation failed",
    "status": 400,
    "details": {
      "field_errors": {
        "email": ["This field is required", "Enter a valid email address"],
        "commission_rate": ["Must be between 0 and 100"]
      }
    }
  }
}