New Residential Premium, our top-tier pool for the toughest sites
HX

Scraping & code · Setup guide

How to use a proxy with HTTPX

HTTPX takes a single proxy= URL on a client or on a one-off call. Credentials go in the URL, and the same setup works for sync and async code.

Broken on our side? Fixed in 48h or refunded.

Quick setup

  1. 1Copy a line in URL format.
  2. 2Create httpx.Client(proxy=url) or httpx.AsyncClient(proxy=url).
  3. 3Use one client per sticky line, or a new client per request to rotate.
  4. 4Install httpx[socks] for SOCKS5.

Your proxy details

After you buy a plan, open My proxies. Choose the Proxy Type (Rotating or Sticky), the Protocol (HTTP or SOCKS5) and a Format, then copy a line. The generator offers three formats:

HOST:PORT:USERNAME:PASSWORD

Paste into apps and anti-detect browsers that split the fields or auto-fill them.

USERNAME:PASSWORD@HOST:PORT

Tools that want credentials before the address.

http://USERNAME:PASSWORD@HOST:PORT

Code and command-line tools. This is the proxy URL used in the examples below.

In the examples on this page, replace USERNAME, PASSWORD, HOST and PORT with the values from your line. SOCKS5_PORT is the port the generator shows when you pick SOCKS5.

Residential lines are either rotating (a new IP on new connections) or sticky (one IP for the time you choose). Datacenter and ISP plans give you a list of dedicated IPs, one per line as IP:PORT:USERNAME:PASSWORD, and each line is one fixed IP.

To test a line, open https://ipinfo.io/json through it: it shows the exit IP and country. For HTTP lines you can also use the free proxy checker.

Basic authenticated proxy

Python
import httpx

proxy = "http://USERNAME:PASSWORD@HOST:PORT"

with httpx.Client(proxy=proxy, timeout=30) as client:
    r = client.get("https://ipinfo.io/json")
    print(r.json())

The proxy= argument arrived in HTTPX 0.26. The older proxies= argument was removed in 0.28, so code that still passes proxies= fails on current versions with an “unexpected keyword argument” error.

Async client

Python
import asyncio
import httpx

PROXY = "http://USERNAME:PASSWORD@HOST:PORT"


async def main():
    async with httpx.AsyncClient(proxy=PROXY, timeout=30) as client:
        r = await client.get("https://ipinfo.io/json")
        print(r.json())


asyncio.run(main())

Different proxies per scheme

Use mounts when you want separate transports, for example to send only some traffic through the proxy. Keep http:// as the proxy URL scheme for the https:// key too.

Python
import httpx

proxy = "http://USERNAME:PASSWORD@HOST:PORT"
mounts = {
    "http://": httpx.HTTPTransport(proxy=proxy),
    "https://": httpx.HTTPTransport(proxy=proxy),
}

with httpx.Client(mounts=mounts, timeout=30) as client:
    print(client.get("https://ipinfo.io/json").json())

Rotating and sticky IPs

Rotation happens per connection. A rotating line gets a new IP when your client opens a new connection; requests sent through the same open HTTPS tunnel leave from the same IP. An HTTPX client keeps a connection pool, so requests through one client can share an IP. The top-level httpx.get() builds a new client for each call, so each call gets a new IP on a rotating line.

To make sticky lines, open the generator, set Proxy Type to Sticky, choose a Sticky Duration if your plan offers one, set how many lines you want in Sticky Amount, then copy or download them. Each line keeps its IP for the chosen time. The session part of a sticky username differs by plan, so always generate sticky lines instead of editing usernames by hand. Use one client per sticky line.

Rotating line: a new IP per call
import httpx

proxy = "http://USERNAME:PASSWORD@HOST:PORT"  # rotating line

for _ in range(3):
    print(httpx.get("https://ipinfo.io/json", proxy=proxy, timeout=30).json()["ip"])

Target a country

On residential plans, add -country- and a two-letter country code to the end of the username, for example USERNAME-country-us or USERNAME-country-gb. To combine a country with a state, a city or a sticky session, build the line in the generator, which shows only the options your plan supports. Datacenter and ISP IPs have a fixed location that you choose when you buy them.

Python
client = httpx.Client(proxy="http://USERNAME-country-de:PASSWORD@HOST:PORT")

SOCKS5

Install
pip install "httpx[socks]"
Python
import httpx

with httpx.Client(proxy="socks5h://USERNAME:PASSWORD@HOST:SOCKS5_PORT", timeout=30) as client:
    print(client.get("https://ipinfo.io/json").json())

HTTPX accepts socks5h:// from version 0.28; on older versions, write socks5://. Use the SOCKS5 port from the generator, not the HTTP port.

Tips

  • HTTPX reads HTTP_PROXY, HTTPS_PROXY and ALL_PROXY when trust_env is on (the default). Pass proxy= explicitly to be sure which proxy a client uses.
  • Reuse one client per sticky line instead of creating a client per request, so you keep both the IP and the connection.

Troubleshooting

  • TypeError: … unexpected keyword argument 'proxies'

    You’re on HTTPX 0.28 or later. Rename proxies= to proxy=.

  • ImportError mentioning socksio

    Install the SOCKS extra with pip install "httpx[socks]".

  • httpx.ProxyError with 407

    The proxy rejected the credentials. Copy a fresh line from the generator.More on this error →

  • The IP doesn’t change on a rotating line

    Your client is reusing an open connection. Requests that share a tunnel share its IP. Open a new connection per request, or use one sticky line per task.More on this error →

  • Timeouts

    Set an explicit timeout of 30–60 seconds, retry on a new connection, and lower concurrency.More on this error →

Seeing another error? Read proxy errors and how to fix them, or contact support.

Questions

How do I set a proxy in HTTPX?

Pass proxy="http://USERNAME:PASSWORD@HOST:PORT" to httpx.Client, httpx.AsyncClient or a top-level call such as httpx.get().

What happened to the proxies argument?

It was deprecated in HTTPX 0.26 and removed in 0.28. Use proxy= for one proxy, or mounts= with httpx.HTTPTransport(proxy=...) for per-scheme setups.

Does HTTPX support SOCKS5 with a password?

Yes, after pip install "httpx[socks]". Use a URL like socks5h://USERNAME:PASSWORD@HOST:SOCKS5_PORT.

How do I rotate IPs with HTTPX?

Use a rotating line and a new connection per request, for example the top-level httpx.get(), or use a different sticky line per client.

Try it with HTTPX for $0.49

Get 1 GB of Residential Lite, then copy your rotating or sticky lines from the generator and paste them into the steps above.

Broken on our side? Fixed in 48h or refunded.

Related guides

All integrations →