Use proxy= on an HTTPX Client or AsyncClient, keep TLS verification enabled, and verify a request from that same client. HTTPX 0.28.1 is the version used in this guide; examples using an older proxies= argument should be checked against their own release.
Reviewed and locally tested: October 12, 2026. Version and route observations below describe the stated test environment. A working local route does not establish a provider’s public exit, country, anonymity, speed, or uptime.
Install the client and choose the protocol
python -m pip install "httpx==0.28.1"
For a SOCKS endpoint, install httpx[socks]==0.28.1 instead. Our SOCKS test also used socksio 1.0.0. Pin the tested version for a reproducible diagnostic, then assess dependency updates under your application’s normal maintenance process.
The proxy URL describes the connection to the proxy. An HTTPS destination can travel through an http:// proxy using CONNECT; changing the destination to HTTPS does not automatically make the proxy endpoint https://. Match the supplied endpoint protocol. See HTTPX proxy documentation and the protocol comparison.
Make one explicit synchronous request
import os, ssl
import httpx
def tls_context(ca_file=None):
return ssl.create_default_context(cafile=ca_file)
def httpx_request(url, proxy_url=None, ca_file=None):
with httpx.Client(proxy=proxy_url, trust_env=False, timeout=5.0,
verify=tls_context(ca_file), follow_redirects=False) as client:
return client.get(url)
response = httpx_request(os.environ["CHECK_URL"], os.environ["PROXY_URL"])
print({"status": response.status_code})
Provide CHECK_URL and PROXY_URL through your process environment or secret manager. Choose a trusted HTTPS endpoint whose server-side observation you can inspect. Status 200 proves an HTTP response, while a destination log or observation endpoint establishes the route. Redirects are disabled so the diagnostic stays on the selected endpoint.
trust_env=False isolates this client from inherited proxy variables. The direct baseline uses the same function with proxy_url=None. The five-second setting bounds network inactivity; it is not a total wall-clock deadline for an entire multi-request job. See HTTPX timeout types.
Keep authentication outside code and logs
When an endpoint requires username/password authentication, its URL can contain percent-encoded credentials. Build that value in memory, and avoid logging the URL, exception request objects, or environment dumps. Encoding changes URL syntax; it does not encrypt the credentials.
from urllib.parse import quote
user = quote(os.environ["PROXY_USER"], safe="")
password = quote(os.environ["PROXY_PASSWORD"], safe="")
proxy_url = f"http://{user}:{password}@proxy.example:8080"
Replace the example host with the endpoint supplied for your account. An HTTP proxy connection does not itself encrypt Basic proxy authentication. Use a trusted path and the transport your provider supports. A proxy 407 is distinct from a destination 401 or 403; confirm which server rejected the request before changing credentials. Review proxy authentication methods.
Reuse an AsyncClient for async requests
import asyncio
async def httpx_async_request(url, proxy_url=None, ca_file=None):
async with httpx.AsyncClient(proxy=proxy_url, trust_env=False, timeout=5.0,
verify=tls_context(ca_file), follow_redirects=False) as client:
return await client.get(url)
response = asyncio.run(httpx_async_request(
os.environ["CHECK_URL"], os.environ["PROXY_URL"]))
print({"status": response.status_code})
The snippet uses the imports and TLS helper above. For a service making many requests, move the client lifetime outside the per-request helper so connections can be reused. Start with one request; add bounded concurrency after authentication and routing are stable. HTTPX async guidance explains the client lifecycle.
What the local tests actually established
| Check | Observed result |
|---|---|
| Sync and async HTTP forwarding | Owned loopback destination identified the authenticated proxy route |
| Wrong proxy credentials | Proxy returned 407; request did not reach the destination |
| HTTPS CONNECT | Untrusted disposable certificate rejected; request passed with that specific CA |
| Authenticated SOCKS5 to HTTPS | Owned destination identified the SOCKS route with certificate validation enabled |
These requests ran on Windows with Python 3.12.14 and HTTPX 0.28.1. The destination did not receive a Proxy-Authorization header. The SOCKS fixture used an IP-literal destination, so this does not prove proxy-side hostname DNS, DNS privacy, or public exit behavior.
Troubleshoot one failure layer at a time
For an unexpected direct route, check that the request uses this configured client rather than a separate convenience call. For a connection error, confirm protocol, host, port and reachability. For a certificate error, investigate the approved CA chain and hostname; keep verification enabled. For a timeout, distinguish connect, read, write and pool waits before adding retries. Compare aiohttp session setup if you are migrating async code, or Requests configuration for an existing Requests service.
Frequently asked questions
Does HTTPX automatically use environment proxies?
It can when environment trust is enabled. These examples disable that inheritance to make the selected route explicit; the environment-variable guide shows measured differences between clients.
Can I use the same proxy with sync and async code?
Both client types accept a proxy endpoint. Test authentication and the destination observation in each client; sharing configuration does not prove that separate code paths used it.
Verify one client before increasing workload
Return to Proxy Setup and Developer Guides for adjacent clients. Use the proxy verification checklist to record a direct baseline and the destination’s observation. When that is repeatable, compare Mexela proxy locations against the actual destination and location requirements.
Download the developer examples
Download the developer examples ZIP for the locally tested Python clients, environment checks, CI route-check files and native PHP cURL helper. The n8n workflow is a separate illustrative reference: it was not imported or executed in n8n. Read the included README for runtime versions, setup and the limits of each check.
ZIP SHA-256: 3a43f7d1e6cbb9b2917dbcf88aaa8fe1a2733c33326187cb89c9a9119aad3e20. Keep real proxy credentials and destination secrets in your approved secret store; they are absent from this archive. Start with one approved destination and compare its observation with a direct baseline.
Versioned Python HTTPX source files
Use the Python HTTPX folder in the public source repository for the example, setup instructions, exact dependency versions and a sanitized test summary. The repository is a dated snapshot of owned examples; its loopback checks do not certify a commercial endpoint or every application configuration.
