Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

HTTPX · Class

Client

AsyncClient is HTTPX’s reusable asynchronous client for making HTTP requests. It provides connection pooling and related client features and can be shared between tasks.

How it works

AsyncClient provides asynchronous request methods together with connection pooling and reusable client configuration. Use it as an async context manager so its connections are closed automatically, or call aclose() explicitly when managing its lifetime yourself.

Syntax

httpx.AsyncClient(*, auth=None, params=None, headers=None, cookies=None, verify=True, cert=None, http1=True, http2=False, proxy=None, mounts=None, timeout=Timeout(timeout=5.0), follow_redirects=False, limits=Limits(max_connections=100, max_keepalive_connections=20, keepalive_expiry=5.0), max_redirects=20, event_hooks=None, base_url='', transport=None, trust_env=True, default_encoding='utf-8')

Returns

An asynchronous HTTP client instance; its request methods yield HTTPX Response objects when awaited.

Key points

  • AsyncClient is the asynchronous HTTPX client and includes pooling, HTTP/2 support, redirects, and cookie persistence.
  • A single client instance can be shared across concurrent asynchronous tasks.
  • The recommended usage pattern is an async context manager, which scopes the client's lifetime.
  • Send requests through asynchronous methods and await the returned response operation.
  • The request API is asynchronous across convenience methods and the general request/send methods.
  • Use AsyncHTTPTransport when supplying a transport created directly for an asynchronous client.

Common mistakes

  • Every request method is asynchronous, so await calls such as get(), post(), request(), and send().
  • A client created outside an async context manager must be closed explicitly with aclose().
  • Streaming uses an async context block rather than treating stream() as an ordinary awaited response.

Example: Send a request with AsyncClient

import asyncio
import httpx

async def main():
    async with httpx.AsyncClient() as client:
        response = await client.get("https://example.org")
        print(response.status_code)

asyncio.run(main())

Creates an asynchronous client, sends a GET request, and closes the client automatically when the context ends.

When to use it

Outbound requests from async applications

An async web application needs to call external HTTP services without blocking its event loop.

Reuse pooled connections across requests

Several requests target the same services during one operation or task group.

In-process ASGI testing

Tests need to send requests directly to an ASGI application.

Centralize request configuration

The application needs shared defaults such as headers, cookies, a base URL, or transport behavior.

Interview questions

QWhat is AsyncClient and how are requests made with it?

AsyncClient is HTTPX's asynchronous client for issuing requests with methods such as get(), post(), request(), and send(). Its methods return asynchronously, so callers must await them.

Follow-up: How should its lifetime be managed? Use async with httpx.AsyncClient() for automatic cleanup, or await aclose() when managing the instance manually.

QWhy use AsyncClient instead of individual top-level requests?

The client supports connection pooling, redirects, cookie persistence, and HTTP/2, and it can be shared between tasks. Reusing one instance avoids recreating client state for each request.

Follow-up: How would you scope it safely? Create it around the group of operations that should share the client and let the async context manager close it afterward.

QHow can AsyncClient be customized for transports or HTTP/2?

AsyncClient accepts client configuration such as a transport and an HTTP/2 setting. For direct transport construction in asynchronous code, use httpx.AsyncHTTPTransport.

Follow-up: What must custom asynchronous transports implement? They should implement the low-level transport API for sending one request and returning its response, commonly by subclassing AsyncBaseTransport.

  • AsyncHTTPTransport
  • AsyncBaseTransport
  • Client
  • Response
  • Request

Source: Developer Interface - HTTPX. Checked 2026-10-15.