Overview

HTTPX is an HTTP client library for Python that provides a comprehensive set of features for making network requests. It distinguishes itself by supporting both synchronous and asynchronous programming paradigms, allowing developers to choose the appropriate model for their application's architecture. This dual capability makes HTTPX suitable for a broad spectrum of projects, from traditional script-based data fetching to high-concurrency web services built with frameworks like FastAPI or Django's async views.

A significant advantage of HTTPX is its native support for HTTP/2, which can offer performance improvements over HTTP/1.1 through features like multiplexing and header compression. This is particularly relevant for applications that communicate with modern APIs or microservices that leverage HTTP/2 for efficiency. The library maintains an API that is largely compatible with the popular Requests library, which reduces the learning curve for Python developers already familiar with that ecosystem. This design choice ensures that migrating existing code or adopting HTTPX in new projects feels intuitive.

HTTPX shines in scenarios where developers need fine-grained control over their HTTP requests, including custom headers, timeouts, redirects, and authentication. Its architecture allows for intercepting requests and responses, enabling advanced features like logging, caching, and retry mechanisms. For applications requiring secure communication, HTTPX handles SSL/TLS verification and offers options for client-side certificates. Its open-source nature means it benefits from community contributions and continuous development, addressing new web standards and security considerations.

The library's design integrates well with Python's asyncio ecosystem, making it a strong choice for building non-blocking I/O applications. This asynchronous capability is crucial for web servers, API gateways, and data processing pipelines that need to handle many concurrent connections without blocking the main thread. By supporting both HTTP/1.1 and HTTP/2, HTTPX allows developers to build future-proof applications that can adapt to evolving web protocols while maintaining compatibility with existing infrastructure. For developers building web applications, understanding the differences between HTTP/1.1 and HTTP/2 can inform design decisions, as detailed by web.dev's HTTP/2 overview.

Key features

  • Synchronous and Asynchronous API: Offers a consistent API for both blocking (requests.get()) and non-blocking (await requests.get()) HTTP operations, integrating seamlessly with Python's asyncio.
  • HTTP/2 Support: Built-in capability to communicate over HTTP/2 protocol, providing potential performance benefits like multiplexing and server push without additional configuration.
  • Requests-Compatible API: Designed to feel familiar to users of the Requests library, simplifying migration and adoption for many Python developers.
  • Full Type Hints: Provides comprehensive type annotations for better code completion, static analysis, and maintainability in modern Python development environments.
  • Client-Side Mounting: Allows mounting local WSGI or ASGI applications directly into the HTTPX client, enabling testing of web applications without a live network connection.
  • Flexible Authentication: Supports various authentication schemes, including basic authentication, digest authentication, and custom authentication flows.
  • Timeout Control: Granular control over connection, read, write, and overall request timeouts.
  • Redirect Handling: Automatic handling of HTTP redirects with configurable limits.
  • Proxy Support: Configuration options for using HTTP and SOCKS proxies.

Pricing

HTTPX is an open-source library distributed under the BSD 3-Clause License. It is free to use for both commercial and non-commercial projects, with no licensing fees or restrictions on its functionality. The project is maintained by a community of contributors.

Service/Feature Cost Notes
HTTPX Library Usage Free No licensing fees for standard or advanced features.
Community Support Free Available via GitHub issues and community forums.

Pricing as of 2026-05-05. For detailed licensing information, refer to the HTTPX GitHub repository.

Common integrations

  • Asyncio: HTTPX is built with asyncio in mind, making it a natural fit for any Python application leveraging asynchronous I/O. Its asynchronous client allows for non-blocking network requests, essential for high-performance applications.
  • ASGI Frameworks (e.g., FastAPI, Starlette): HTTPX clients can directly test ASGI applications by mounting them, which is valuable for integration testing without a live HTTP server. This capability is detailed in the HTTPX testing documentation.
  • Logging Libraries: HTTPX integrates with Python's standard logging module, allowing developers to capture and process request and response information for debugging and monitoring.
  • Authentication Libraries: Can be integrated with various authentication schemes, including OAuth and custom token-based authentication, by setting appropriate headers or using custom auth classes.
  • Data Serialization Libraries (e.g., json, msgpack): Easily sends and receives data in various formats, leveraging Python's built-in json module or external libraries for other formats.

Alternatives

  • Requests: A popular, synchronous-only HTTP library for Python, known for its user-friendly API, which HTTPX's API is designed to closely resemble.
  • aiohttp: An asynchronous HTTP client/server framework for asyncio, offering both client and server functionalities.
  • urllib3: A powerful, thread-safe HTTP client library that provides low-level HTTP features and is a dependency for many other HTTP clients, including Requests.

Getting started

To begin using HTTPX, you first need to install it via pip. Both synchronous and asynchronous examples are provided below to illustrate its flexibility.

pip install httpx

Here's a simple example demonstrating a synchronous GET request:

import httpx

# Synchronous GET request
def fetch_sync_data():
    try:
        response = httpx.get("https://httpbin.org/get")
        response.raise_for_status() # Raises an HTTPStatusError for bad responses (4xx or 5xx)
        print("Synchronous Request Successful!")
        print(f"Status Code: {response.status_code}")
        print(f"Response JSON: {response.json()}")
    except httpx.HTTPStatusError as e:
        print(f"HTTP error occurred: {e}")
    except httpx.RequestError as e:
        print(f"An error occurred while requesting {e.request.url}: {e}")

fetch_sync_data()

For asynchronous operations, HTTPX integrates with Python's asyncio. This example shows an asynchronous GET request:

import httpx
import asyncio

# Asynchronous GET request
async def fetch_async_data():
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get("https://httpbin.org/get")
            response.raise_for_status() # Raises an HTTPStatusError for bad responses (4xx or 5xx)
            print("Asynchronous Request Successful!")
            print(f"Status Code: {response.status_code}")
            print(f"Response JSON: {response.json()}")
        except httpx.HTTPStatusError as e:
            print(f"HTTP error occurred: {e}")
        except httpx.RequestError as e:
            print(f"An error occurred while requesting {e.request.url}: {e}")

# Run the asynchronous function
asyncio.run(fetch_async_data())

These examples illustrate the basic usage for both synchronous and asynchronous requests. For more advanced features like POST requests, custom headers, or authentication, refer to the official HTTPX documentation.