Perch 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, a charge failed, etc.). Codes in the 5xx range indicate an error with Perch’s servers (these are rare, but they can happen).
HTTP status codes
The error will contain a helpful message and, in some cases (409 and 422), a fields object. The fields object will contain fields that failed some validation and an array of validation errors.
Field validation error codes
When validation fails on specific fields, you’ll receive one of these error codes:
Common error scenarios
Authentication errors
401 Unauthorized
- Missing
X-API-Key header
- Invalid or expired API key
- API key format is incorrect
403 Forbidden
- API key doesn’t have permission for the requested resource
- Trying to access a resource outside your firm’s scope
Client errors
404 Not Found
- Resource doesn’t exist (lead, plan, etc.)
- Typo in the endpoint URL
- Resource exists but you don’t have permission to access it
409 Conflict
- Duplicate lead with same email for your firm
- Idempotency key collision
- Resource state conflict
422 Unprocessable Entity
- Missing required fields
- Invalid field values or formats
- Business logic validation failures
Rate limiting
429 Too Many Requests
- Exceeded your hourly rate limit
- Too many requests in a short burst
- Implement exponential backoff and retry logic
For 429 errors, implement exponential backoff in your retry logic. Start with a 1-second delay and double it with each retry, up to a maximum of 60 seconds.
Best practices for error handling
- Always check status codes - Don’t assume requests succeeded without checking the HTTP status
- Parse error messages - The
message field often contains human-readable explanations
- Handle field errors - Use the
fields object to show specific validation errors to users
- Implement retry logic - For
429 and 5xx errors, implement smart retry mechanisms
- Log errors appropriately - Log enough detail for debugging but avoid logging sensitive data
- Graceful degradation - Handle API errors gracefully in your user interface
Example error handling