# Httpx2HttpClientAsync<!-- -->

Asynchronous HTTP client for the Apify API built on top of [HTTPX](https://github.com/pydantic/httpx2).

This client wraps `httpx.AsyncClient` and adds automatic retries with exponential backoff for rate-limited (HTTP 429) and server error (HTTP 5xx) responses.

HTTPX applies a request timeout to each socket operation rather than to the request as a whole, so a response whose body arrives slowly keeps resetting it and can outlast both the requested timeout and `timeout_max`. The default Impit client enforces the same value as a deadline for the whole request, body included.

Requires the `httpx2` extra: `pip install "apify-client[httpx2]"`. The `httpx2` package is Pydantic's maintained continuation of HTTPX, which this module imports under the `httpx` name.

### Hierarchy

* [HttpClientAsync](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientAsync.md)
  * *Httpx2HttpClientAsync*

## Index[**](#Index)

### Methods

* [**\_\_aenter\_\_](https://docs.apify.com/api/client/python/api/client/python/reference/class/Httpx2HttpClientAsync.md#__aenter__)
* [**\_\_aexit\_\_](https://docs.apify.com/api/client/python/api/client/python/reference/class/Httpx2HttpClientAsync.md#__aexit__)
* [**\_\_init\_\_](https://docs.apify.com/api/client/python/api/client/python/reference/class/Httpx2HttpClientAsync.md#__init__)
* [**aclose](https://docs.apify.com/api/client/python/api/client/python/reference/class/Httpx2HttpClientAsync.md#aclose)
* [**call](https://docs.apify.com/api/client/python/api/client/python/reference/class/Httpx2HttpClientAsync.md#call)
* [**is\_retryable\_transport\_error](https://docs.apify.com/api/client/python/api/client/python/reference/class/Httpx2HttpClientAsync.md#is_retryable_transport_error)
* [**is\_timeout\_error](https://docs.apify.com/api/client/python/api/client/python/reference/class/Httpx2HttpClientAsync.md#is_timeout_error)
* [**send\_request](https://docs.apify.com/api/client/python/api/client/python/reference/class/Httpx2HttpClientAsync.md#send_request)
* [**set\_default\_authorization](https://docs.apify.com/api/client/python/api/client/python/reference/class/Httpx2HttpClientAsync.md#set_default_authorization)

## Methods<!-- -->[**](#Methods)

### [**](#__aenter__)\_\_aenter\_\_

* **async **\_\_aenter\_\_**(): Self

- Inherited from [HttpClientAsync.\_\_aenter\_\_](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientAsync.md#__aenter__)

  Return this client and close it when the async context exits.

  ***

  #### Returns Self

### [**](#__aexit__)\_\_aexit\_\_

* **async **\_\_aexit\_\_**(exc\_type, exc\_value, traceback): None

- Inherited from [HttpClientAsync.\_\_aexit\_\_](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientAsync.md#__aexit__)

  Close resources owned by the asynchronous HTTP client.

  ***

  #### Parameters

  * ##### exc\_type: type\[BaseException] | None
  * ##### exc\_value: BaseException | None
  * ##### traceback: TracebackType | None

  #### Returns None

### [**](#__init__)\_\_init\_\_

* ****\_\_init\_\_**(\*, token, timeout\_short, timeout\_medium, timeout\_long, timeout\_max, max\_retries, min\_delay\_between\_retries, statistics, headers, http\_compressor): None

- Overrides [HttpClientAsync.\_\_init\_\_](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientAsync.md#__init__)

  Initialize the HTTPX-based asynchronous HTTP client.

  ***

  #### Parameters

  * ##### optionalkeyword-onlytoken: str | None = <!-- -->None

    Apify API token for authentication.

  * ##### optionalkeyword-onlytimeout\_short: timedelta = <!-- -->DEFAULT\_TIMEOUT\_SHORT

    Default timeout for short-duration API operations (simple CRUD operations, ...).

  * ##### optionalkeyword-onlytimeout\_medium: timedelta = <!-- -->DEFAULT\_TIMEOUT\_MEDIUM

    Default timeout for medium-duration API operations (batch operations, listing, ...).

  * ##### optionalkeyword-onlytimeout\_long: timedelta = <!-- -->DEFAULT\_TIMEOUT\_LONG

    Default timeout for long-duration API operations (long-polling, streaming, ...).

  * ##### optionalkeyword-onlytimeout\_max: timedelta = <!-- -->DEFAULT\_TIMEOUT\_MAX

    Maximum timeout cap for any single request attempt, including tier and per-call timeouts.

  * ##### optionalkeyword-onlymax\_retries: int = <!-- -->DEFAULT\_MAX\_RETRIES

    Maximum number of retry attempts for failed requests.

  * ##### optionalkeyword-onlymin\_delay\_between\_retries: timedelta = <!-- -->DEFAULT\_MIN\_DELAY\_BETWEEN\_RETRIES

    Minimum delay between retries (increases exponentially with each attempt).

  * ##### optionalkeyword-onlystatistics: [ClientStatistics](https://docs.apify.com/api/client/python/api/client/python/reference/class/ClientStatistics.md) | None = <!-- -->None

    Statistics tracker for API calls. Created automatically if not provided.

  * ##### optionalkeyword-onlyheaders: dict\[str, str] | None = <!-- -->None

    Additional HTTP headers to include in all requests.

  * ##### optionalkeyword-onlyhttp\_compressor: [HttpCompressor](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpCompressor.md) | None = <!-- -->None

    Compressor used to compress request bodies. Defaults to `GzipHttpCompressor`.

  #### Returns None

### [**](#aclose)aclose

* **async **aclose**(): None

- Overrides [HttpClientAsync.aclose](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientAsync.md#aclose)

  Close the underlying asynchronous HTTPX connection pool.

  ***

  #### Returns None

### [**](#call)call

* **async **call**(\*, method, url, headers, params, data, json, stream, timeout): [HttpResponse](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpResponse.md)

- Inherited from [HttpClientAsync.call](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientAsync.md#call)

  Make an HTTP request with automatic retry and exponential backoff.

  ***

  #### Parameters

  * ##### keyword-onlymethod: str

    HTTP method (GET, POST, PUT, DELETE, etc.).

  * ##### keyword-onlyurl: str

    Full URL to make the request to.

  * ##### optionalkeyword-onlyheaders: dict\[str, str] | None = <!-- -->None

    Additional headers to include.

  * ##### optionalkeyword-onlyparams: dict\[str, Any] | None = <!-- -->None

    Query parameters to append to the URL.

  * ##### optionalkeyword-onlydata: str | bytes | bytearray | None = <!-- -->None

    Raw request body data. Cannot be used together with json.

  * ##### optionalkeyword-onlyjson: JsonSerializable | None = <!-- -->None

    JSON-serializable data for the request body. Cannot be used together with data.

  * ##### optionalkeyword-onlystream: bool | None = <!-- -->None

    Whether to stream the response body.

  * ##### optionalkeyword-onlytimeout: [Timeout](https://docs.apify.com/api/client/python/api/client/python/reference.md#Timeout) = <!-- -->'medium'

    Timeout for the API HTTP request. Use `short`, `medium`, or `long` tier literals for preconfigured timeouts. A `timedelta` overrides it for this call (capped at `timeout_max`), and `no_timeout` disables the timeout entirely.

  #### Returns [HttpResponse](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpResponse.md)

  The HTTP response object.

### [**](#is_retryable_transport_error)is\_retryable\_transport\_error

* ****is\_retryable\_transport\_error**(exc): bool

- Overrides [HttpClientAsync.is\_retryable\_transport\_error](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientAsync.md#is_retryable_transport_error)

  Return whether an underlying HTTP-library exception is retryable.

  The default classifies nothing as retryable, so a transport that doesn't override it gives up on the first connection failure. Every transport adapter should map its own transient error types here.

  ***

  #### Parameters

  * ##### exc: Exception

  #### Returns bool

### [**](#is_timeout_error)is\_timeout\_error

* ****is\_timeout\_error**(exc): bool

- Overrides [HttpClientAsync.is\_timeout\_error](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientAsync.md#is_timeout_error)

  Return whether an exception represents a transport timeout.

  Recognizes Python's own `TimeoutError`. Transport adapters extend it with the timeout types their HTTP library defines.

  ***

  #### Parameters

  * ##### exc: Exception

  #### Returns bool

### [**](#send_request)send\_request

* **async **send\_request**(\*, method, url, headers, content, timeout, stream): [HttpResponse](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpResponse.md)

- Overrides [HttpClientAsync.send\_request](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientAsync.md#send_request)

  Send one request through the underlying HTTP library.

  Required by the inherited `call`, so a transport adapter must implement it. Overriding `call` itself bypasses it entirely. Let the HTTP library's exceptions propagate unwrapped: `call` classifies them through `is_retryable_transport_error` and `is_timeout_error`.

  ***

  #### Parameters

  * ##### keyword-onlymethod: str

    HTTP method (GET, POST, PUT, DELETE, etc.).

  * ##### keyword-onlyurl: str

    Full request URL, with the query parameters already encoded into it.

  * ##### keyword-onlyheaders: dict\[str, str]

    Final request headers, with the client's default headers already merged in.

  * ##### keyword-onlycontent: bytes | None

    Request body, already serialized and compressed, or None for a request without a body.

  * ##### keyword-onlytimeout: float | None

    Timeout for this attempt in seconds, or None for no timeout at all.

  * ##### keyword-onlystream: bool

    Whether to return the response with the body unread, so the caller can stream it.

  #### Returns [HttpResponse](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpResponse.md)

  The HTTP response object.

### [**](#set_default_authorization)set\_default\_authorization

* ****set\_default\_authorization**(token): None

- Inherited from [HttpClientBase.set\_default\_authorization](https://docs.apify.com/api/client/python/api/client/python/reference/class/HttpClientBase.md#set_default_authorization)

  Set the `Authorization` header from the token, unless an authorization header is already configured.

  ***

  #### Parameters

  * ##### token: str

    The Apify API token to set as the `Bearer` authorization.

  #### Returns None
