> ## Documentation Index
> Fetch the complete documentation index at: https://developers.telnyx.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python SDK errors, retries, and timeouts

> Exception hierarchy, automatic retry behavior, and timeout configuration in the Telnyx Python SDK.

## Handling errors

When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `telnyx.APIConnectionError` is raised.

When the API returns a non-success status code (that is, 4xx or 5xx
response), a subclass of `telnyx.APIStatusError` is raised, containing `status_code` and `response` properties.

All errors inherit from `telnyx.APIError`.

```python theme={null}
import telnyx
from telnyx import Telnyx

client = Telnyx()

try:
    client.number_orders.create(
        phone_numbers=[{"phone_number": "+15558675309"}],
    )
except telnyx.APIConnectionError as e:
    print("The server could not be reached")
    print(e.__cause__)  # an underlying Exception, likely raised within httpx.
except telnyx.RateLimitError as e:
    print("A 429 status code was received; we should back off a bit.")
except telnyx.APIStatusError as e:
    print("Another non-200-range status code was received")
    print(e.status_code)
    print(e.response)
```

Error codes are as follows:

| Status Code | Error Type                 |
| ----------- | -------------------------- |
| 400         | `BadRequestError`          |
| 401         | `AuthenticationError`      |
| 403         | `PermissionDeniedError`    |
| 404         | `NotFoundError`            |
| 422         | `UnprocessableEntityError` |
| 429         | `RateLimitError`           |
| >=500       | `InternalServerError`      |
| N/A         | `APIConnectionError`       |

### Retries

Certain errors are automatically retried 2 times by default, with a short exponential backoff.
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
429 Rate Limit, and >=500 Internal errors are all retried by default.

You can use the `max_retries` option to configure or disable retry settings:

```python theme={null}
from telnyx import Telnyx

# Configure the default for all requests:
client = Telnyx(
    # default is 2
    max_retries=0,
)

# Or, configure per-request:
client.with_options(max_retries=5).number_orders.create(
    phone_numbers=[{"phone_number": "+15558675309"}],
)
```

### Timeouts

By default requests time out after 1 minute. You can configure this with a `timeout` option,
which accepts a float or an [`httpx.Timeout`](https://www.python-httpx.org/advanced/timeouts/#fine-tuning-the-configuration) object:

```python theme={null}
from telnyx import Telnyx

# Configure the default for all requests:
client = Telnyx(
    # 20 seconds (default is 1 minute)
    timeout=20.0,
)

# More granular control:
client = Telnyx(
    timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0),
)

# Override per-request:
client.with_options(timeout=5.0).number_orders.create(
    phone_numbers=[{"phone_number": "+15558675309"}],
)
```

On timeout, an `APITimeoutError` is thrown.

Note that requests that time out are [retried twice by default](#retries).
