PRIVATEBYTE
ServersProxiesAgentsStatusNetworkAboutFree Proxies
Sign inDeploy

← Docs / Proxies

Datacenter proxies with Python httpx

httpx takes the proxy as one argument. This page uses the datacenter tier: a US datacenter IP that changes for every new connection.

What you need

  • Your proxy API key, from the Proxies page in the portal.
  • pip install httpx (version 0.26 or later, which uses the proxy argument).
export PB_PROXY_KEY="YOUR_KEY"

Send a request through the proxy

import os
import httpx

PROXY = f"http://{os.environ['PB_PROXY_KEY']}:[email protected]:3128"

r = httpx.get("https://api.ipify.org", proxy=PROXY, timeout=30)
print(r.text)  # a US datacenter address

One IP, or a new one each time

A Client keeps its connection open, so all its requests share one address. A bare httpx.get() opens a new connection every time.

# New IP each call: bare calls open a new connection.
for _ in range(3):
    print(httpx.get("https://api.ipify.org", proxy=PROXY, timeout=30).text)

# Same IP for all calls: one Client reuses its connection.
with httpx.Client(proxy=PROXY, timeout=30) as client:
    for _ in range(3):
        print(client.get("https://api.ipify.org").text)

Async

For a new IP on every request with an AsyncClient, stop it keeping connections alive:

import asyncio

async def main():
    limits = httpx.Limits(max_keepalive_connections=0)
    async with httpx.AsyncClient(proxy=PROXY, timeout=30, limits=limits) as client:
        for _ in range(3):
            print((await client.get("https://api.ipify.org")).text)

asyncio.run(main())

Leave keep-alive on when a steady address or a smaller bill matters more. See how usage is counted.

Errors

What you see What it means
407 Proxy Authentication Required The API key is wrong or missing.
400 with Unknown tier "datacentre" The tier word is a close misspelling of a real tier. Fix the spelling. A password that is not close to any tier, such as x, uses your account's default tier.
400 Invalid proxy password settings The password asked for something this tier does not do: a country other than US, a session or a TTL.
A site returns 403 The site is refusing datacenter addresses. Try a residential tier such as standard.

httpx raises httpx.ProxyError when the proxy refuses the tunnel.

Related

  • Datacenter proxies with Python requests
  • Datacenter proxies with Scrapy
  • How rotating proxies work
  • Datacenter proxies

Try your next connection.

Free proxy credit for eligible new proxy users. No card required. Review the amount and conditions before claiming.

Get free proxy credit →

Explore proxy tiers and paid credit

Related

Proxies: your first requestFind your credentials, copy a cURL or Python example, and check the response.Proxy connection format and tiersWhat goes in the username and password, and what each tier is for.Using SOCKS5Port 1080, the same credentials, and the two things SOCKS5 cannot do.

Still stuck? Open a ticket — tell us what was missing and the article gets fixed.

PRIVATEBYTE

Infrastructure for independent minds.

Service status

Products

Virtual serversProxiesGet free proxy creditAgentsMigration

Resources

Looking glassSecurityBlogNews

Free tools

What is my IPProxy checkerPort checkerDomain checkerDNS lookupPassword generatorVPS price calculator

Company

AboutContactX / TwitterTelegram
© 2026 PrivateByte Ltd
TermsPrivacyAcceptable useRefundsAbuse & legalSLA

Registered in England & Wales · Company no. 15834803 · 128 City Road, London EC1V 2NX