Start with the answer: give one owner control of HTTPX routing
httpx proxy example field
Evidence to retain
Routing owner
Explicit client, mounts, or managed environment
Environment behavior
Record whether trust_env is enabled
Failure stage
Proxy, connect, read, or HTTP status
Verification record
Mode, redacted route, status, returned address
# How to Use an HTTPX Proxy: Routing, Auth, and Error Checks
HTTPX offers three practical routing patterns: one explicit proxy for a client, separate transports mounted by URL scheme, or proxy variables supplied by the runtime environment. The correct choice depends on who owns the routing rule and how much of the application it should affect.
The examples below use fictional hostnames and environment variables. They assume an authorized diagnostic endpoint and a proxy address that has already been assigned. They do not establish that any product supports a particular protocol.
Pick one of three routing patterns
Pattern A: one explicit client proxy
Choose this when every request made by a dedicated client should follow one route. Setting
trust_env=False
prevents machine-level proxy variables from silently overriding the test design.
Frequently Asked Questions
Should I use `proxy=` or `mounts=`?
Use `proxy=` for one route across a dedicated client. Use mounts when routing intentionally differs by destination scheme or domain and the extra complexity is documented.
Why does the HTTPS mount sometimes use an `http://` proxy URL?
The mount key describes the destination scheme. The proxy URL describes how the client reaches the proxy. Many HTTPS requests use an HTTP proxy plus tunneling, so the values do not have to match.
Why did HTTPX use a proxy that is not in my code?
HTTPX reads supported proxy environment variables by default. Set `trust_env=False` for an isolated explicit test, or document the environment as the source of truth.
Does a successful IP check prove future access?
import os
from urllib.parse import quote
import httpx
host = os.environ["APP_PROXY_HOST"]
port = os.environ["APP_PROXY_PORT"]
user = quote(os.environ["APP_PROXY_USER"], safe="")
password = quote(os.environ["APP_PROXY_PASSWORD"], safe="")
check_url = os.environ["AUTHORIZED_IP_CHECK_URL"]
timeout = httpx.Timeout(20.0, connect=6.0)
with httpx.Client(
proxy=proxy_url,
timeout=timeout,
trust_env=False,
) as client:
response = client.get(check_url)
response.raise_for_status()
print(response.text)
URL-encoding the username and password prevents reserved characters from breaking the URL structure. It does not make the value safe to log. Keep the assembled URL in memory and redact it from exceptions and telemetry.
Pattern B: route by request scheme
Use mounts only when HTTP and HTTPS destinations intentionally require different transports. HTTPX documentation notes a common point of confusion: the proxy URL for an
https://
destination often still begins with
http://
, because the client connects to an HTTP proxy and establishes a tunnel.
with httpx.Client(mounts=mounts, trust_env=False) as client:
response = client.get(check_url)
response.raise_for_status()
Do not change the proxy URL scheme merely because the destination uses HTTPS. Use the scheme specified for the assigned endpoint.
Pattern C: let the environment own routing
HTTPX trusts environment variables by default.
HTTP_PROXY
,
HTTPS_PROXY
, and
ALL_PROXY
can select a route, while
NO_PROXY
identifies destinations that should connect directly.
This pattern is suitable for a controlled deployment where operations owns the environment. It is less suitable for a self-contained test because the source code no longer shows the effective path. Record the effective variables securely before comparing results, and never print values that contain credentials.
Add async code only after the route works
The routing concepts are the same for
AsyncClient
, but asynchronous execution makes logs and failures arrive out of order. Confirm one synchronous request first, then move to an async client if the application requires it.
An async client should be reused for a related batch rather than created for every URL. Keep concurrency conservative and within the target site's rules.
Turn errors into routing evidence
HTTPX exception types help identify the failing layer:
ProxyError
points to negotiation with the configured proxy.
ConnectError
means HTTPX could not establish a connection, but it does not by itself prove that proxy negotiation was the cause.
ConnectTimeout
means the connection stage exceeded the limit.
ReadTimeout
means the connection existed but the response did not arrive within the read window.
Log the exception class, sanitized host label, target class, and timestamp. Do not log the full proxy URL, authorization data, response cookies, or page content unless there is an approved retention reason.
Verification checklist
Confirm the expected host, port, and endpoint scheme from the assignment record.
Run one direct baseline only if policy allows it, then one proxied request.
Compare the observed address and response status with the expected route.
Check that
NO_PROXY
did not bypass the diagnostic host.
Close the client and remove temporary environment variables.
Repeat only when the first result is explainable.
Product boundary
This tutorial covers HTTPX configuration, not product availability. IPIPD currently identifies static IP and VPS as separate product categories; VPS is a server product rather than a proxy type. Use only the current endpoint documentation supplied for your order. Do not infer protocol support, residential or ISP classification, speed, access, or account outcomes from an HTTPX example.
Quick reference: how to choose and verify one HTTPX routing mode
for one route across a dedicated client. Use mounts when routing intentionally differs by destination scheme or domain and the extra complexity is documented.
Why does the HTTPS mount sometimes use an
http://
proxy URL?
The mount key describes the destination scheme. The proxy URL describes how the client reaches the proxy. Many HTTPS requests use an HTTP proxy plus tunneling, so the values do not have to match.
Why did HTTPX use a proxy that is not in my code?
HTTPX reads supported proxy environment variables by default. Set
trust_env=False
for an isolated explicit test, or document the environment as the source of truth.
Does a successful IP check prove future access?
No. It proves only that one request completed through the observed route. It does not guarantee availability, target acceptance, account safety, or business results.
No. It proves only that one request completed through the observed route. It does not guarantee availability, target acceptance, account safety, or business results.