Skip to main content

Error Handling

The ZenFlow API uses standard HTTP status codes and returns detailed error information in JSON format.

Error Response Format

All errors follow this structure:
{
  "success": false,
  "error": {
    "code": "error_code",
    "message": "Human-readable error message",
    "details": {} // Optional additional information
  }
}

HTTP Status Codes

CodeDescriptionWhen It Occurs
400Bad RequestInvalid request body or parameters
401UnauthorizedMissing or invalid API key
403ForbiddenValid key but insufficient permissions
404Not FoundResource doesn’t exist
409ConflictResource already exists
422UnprocessableValidation failed
429Too Many RequestsRate limit exceeded
500Internal ErrorServer-side error

Common Error Codes

Authentication Errors

CodeMessageResolution
missing_api_keyAPI key is requiredAdd X-API-Key header
invalid_api_keyAPI key is invalidCheck your API key
expired_api_keyAPI key has expiredCreate a new API key
revoked_api_keyAPI key was revokedCreate a new API key
{
  "success": false,
  "error": {
    "code": "invalid_api_key",
    "message": "The API key provided is invalid or has been revoked"
  }
}

Authorization Errors

CodeMessageResolution
insufficient_scopeMissing required scopeUse key with proper scopes
ip_not_allowedIP not in whitelistAdd your IP to whitelist
{
  "success": false,
  "error": {
    "code": "insufficient_scope",
    "message": "This API key does not have the required scope: write:orders"
  }
}

Validation Errors

CodeMessageResolution
validation_errorInvalid field valueCheck the details field
invalid_idID format is wrongUse correct ID format
missing_fieldRequired field missingInclude required fields
{
  "success": false,
  "error": {
    "code": "validation_error",
    "message": "Invalid order data",
    "details": [
      {
        "field": "assembly_date",
        "message": "Must be a valid date in YYYY-MM-DD format"
      },
      {
        "field": "order_detail",
        "message": "At least one item is required"
      }
    ]
  }
}

Resource Errors

CodeMessageResolution
not_foundResource not foundCheck the resource ID
already_existsResource already existsUse a different identifier
{
  "success": false,
  "error": {
    "code": "not_found",
    "message": "Order not found"
  }
}

Rate Limiting

{
  "success": false,
  "error": {
    "code": "rate_limit_exceeded",
    "message": "Rate limit exceeded. Try again in 30 seconds."
  }
}
See Rate Limits for handling strategies.

Handling Errors

JavaScript/TypeScript

async function createOrder(orderData) {
  try {
    const response = await fetch("/api/v1/orders", {
      method: "POST",
      headers: {
        "X-API-Key": API_KEY,
        "Content-Type": "application/json",
      },
      body: JSON.stringify(orderData),
    });

    const result = await response.json();

    if (!result.success) {
      switch (result.error.code) {
        case "validation_error":
          // Show validation errors to user
          console.error("Validation failed:", result.error.details);
          break;
        case "already_exists":
          // Handle duplicate
          console.error("Order already exists");
          break;
        case "rate_limit_exceeded":
          // Retry after delay
          await delay(30000);
          return createOrder(orderData);
        default:
          console.error("API error:", result.error.message);
      }
      throw new Error(result.error.message);
    }

    return result.data;
  } catch (error) {
    if (error.name === "TypeError") {
      // Network error
      console.error("Network error");
    }
    throw error;
  }
}

Python

import requests

def create_order(order_data):
    try:
        response = requests.post(
            'https://api.zenflow.com.ar/api/v1/orders',
            headers={'X-API-Key': API_KEY},
            json=order_data
        )

        result = response.json()

        if not result.get('success'):
            error = result.get('error', {})
            code = error.get('code')

            if code == 'validation_error':
                print(f"Validation failed: {error.get('details')}")
            elif code == 'rate_limit_exceeded':
                time.sleep(30)
                return create_order(order_data)
            else:
                print(f"API error: {error.get('message')}")

            raise Exception(error.get('message'))

        return result.get('data')

    except requests.exceptions.RequestException as e:
        print(f"Network error: {e}")
        raise

Best Practices

Check success Field

Always check the success field in responses

Log Error Codes

Log error codes for debugging and monitoring

Handle Retries

Implement retry logic for transient errors

User Messages

Show user-friendly messages for validation errors

Retry Strategy

Retry these errors with exponential backoff:
  • 429 Rate limit exceeded
  • 500 Internal server error
  • 503 Service unavailable
  • Network timeouts
Don’t retry these errors:
  • 400 Bad request (fix the request first)
  • 401 Unauthorized (fix authentication)
  • 403 Forbidden (check permissions)
  • 404 Not found (resource doesn’t exist)

Getting Help

If you encounter persistent errors:
  1. Check the error code and message
  2. Review the API documentation
  3. Check service status
  4. Contact [email protected] with:
    • Error code and message
    • Request details (endpoint, method)
    • Timestamp of the error