Overview
When integrating with Twilio’s REST APIs, it’s important to handle errors gracefully to ensure your application is robust, efficient, and resilient to transient issues. This article explains how to distinguish between retryable (transient) and non-retryable (terminal) errors, and provides guidance on implementing effective retry logic.
Why Use HTTP Status Codes for Retry Logic?
Twilio REST API responses include both HTTP status codes and Twilio-specific error codes. Because Twilio may introduce new error codes over time, it is more maintainable and future-proof to base your retry logic on HTTP status codes rather than enumerating every possible error code.
What You Need To Know
Retryable vs. Non-Retryable HTTP Status Codes
Retryable (Transient) Status Codes
These status codes indicate temporary issues that may resolve on their own. It is generally safe to retry requests that return these codes:
| HTTP Status | Meaning | Notes |
|---|---|---|
| 429 | Too Many Requests | Rate limiting; respect Retry-After header |
| 500 | Internal Server Error | |
| 502 | Bad Gateway | |
| 503 | Service Unavailable | |
| 504 | Gateway Timeout |
Non-Retryable (Terminal) Status Codes
These status codes indicate permanent issues that will not be resolved by retrying. These require code or configuration changes:
| HTTP Status | Meaning | Typical Cause |
|---|---|---|
| 400 | Bad Request | Invalid parameters |
| 401 | Unauthorized | Authentication failure |
| 403 | Forbidden | Permission denied |
| 404 | Not Found | Resource does not exist |
| 405 | Method Not Allowed | |
| 422 | Unprocessable Entity | Validation errors |
Recommended Retry Strategy
- Exponential Backoff: For retryable errors, use exponential backoff (e.g., wait 1s, then 2s, then 4s, etc.) up to a reasonable maximum.
- Maximum Retries: Set a maximum number of retries (commonly 3–5 attempts).
-
429 Handling: For HTTP 429, always respect the
Retry-Afterheader if present. - No Retries for 4xx (except 429): Avoid retrying for non-retryable status codes, as these indicate issues that must be fixed in your request or configuration.
Example Logic
if response.status_code in [429, 500, 502, 503, 504]:
# Retry with exponential backoff
elif response.status_code in [400, 401, 403, 404, 405, 422]:
# Do not retry; investigate and fix the request
else:
# Handle other status codes as appropriateConclusion
- Use HTTP status codes to determine retry logic for Twilio REST API errors.
- Retry only on transient errors (429, 500, 502, 503, 504) with exponential backoff and a maximum retry count.
- Do not retry on permanent errors (400, 401, 403, 404, 405, 422); these require code or configuration changes.
- Always respect the
Retry-Afterheader for rate limiting.
By following these best practices, you can build more reliable and efficient integrations with Twilio’s APIs.
Below you will find references to useful documents:
- Twilio Error and Warning Dictionary
- Common API Error Codes and How to Resolve Them
- API Response Error 429 "Too Many Requests"
If you have further questions or need clarification on specific error codes or scenarios, please refer to the resources above or reach out to Twilio Support.