---
title: "How To Use Proxies With HTTPX"
slug: httpx-with-proxies
date: 2025-01-02T08:43:35+00:00
modified: 2025-09-16T16:38:18+00:00
permalink: https://brightdata.com/blog/proxy-101/httpx-with-proxies
type: blog
---

[ Blog ](https://brightdata.com/blog "Blog") / [Proxy 101](https://brightdata.com/blog/proxy-101)







 [Proxy 101](https://brightdata.com/blog/proxy-101)

# How To Use Proxies With HTTPX

Learn HTTPX proxy setups with examples for unauthenticated, authenticated, rotating, and fallback proxies.

 6 min read





 [ ](https://brightdata.com/blog/authors/jake-nulty)

 [Jake Nulty

Technical Writer

 ](https://brightdata.com/blog/authors/jake-nulty)





 ![How To Use Proxies With HTTPX blog image](https://media.brightdata.com/2025/01/How-To-Use-Proxies-With-HTTPX.svg)





Today, we’re going to learn how to use proxies with HTTPX. A proxy sits between your scraper and the site you’re trying to scrape. Your scraper makes a request to the proxy server for the target site. The proxy then fetches the target site and returns it to your scraper.

## [](#How-To-Use-Unauthenticated-Proxies)How To Use Unauthenticated Proxies

In summary, all of our requests go to a `proxy_url`. Below is an example using an unauthenticated proxy. This means that we’re not using a username or password. This example was inspired by their [documentation](https://www.python-httpx.org/advanced/proxies/#http-proxies).

```none
import httpx

proxy_url = "http://localhost:8030"

with httpx.Client(proxy=proxy_url) as client:
    ip_info = client.get("https://geo.brdtest.com/mygeo.json")
    print(ip_info.text)

```

## [](#How-To-Use-Authenticated-Proxies)How To Use Authenticated Proxies

When a proxy requires a username and password, it’s called an “authenticated” proxy. These credentials are used to authenticate your account and give you a connection to the proxy.

With authentication, our `proxy_url` looks like this: `http://<username>:<password>@<proxy_url>:<port_number>`. In the example below, we use both our `zone` and `username` to create the user portion of the authentication string.

We’re using [datacenter proxies](/proxy-types/datacenter-proxies) for our base connection.

```none
import httpx

username = "your-username"
zone = "your-zone-name"
password = "your-password"

proxy_url = f"http://brd-customer-{username}-zone-{zone}:{password}@brd.superproxy.io:33335"

ip_info = httpx.get("https://geo.brdtest.com/mygeo.json", proxy=proxy_url)

print(ip_info.text)

```

The code above is pretty simple. This is the basis for any sort of proxy you want to setup.

- First, we create our config variables: `username`, `zone` and `password`.
- We use those to create our `proxy_url`: `f"http://brd-customer-{username}-zone-{zone}:{password}@brd.superproxy.io:33335"`.
- We make a request to the API to get general information about our proxy connection.

Your response should look similar to this.

```none
{"country":"US","asn":{"asnum":20473,"org_name":"AS-VULTR"},"geo":{"city":"","region":"","region_name":"","postal_code":"","latitude":37.751,"longitude":-97.822,"tz":"America/Chicago"}}

```

## [](#How-To-Use-Rotating-Proxies)How To Use Rotating Proxies

When we use rotating proxies, we create a list of proxies and choose from them randomly. In the code below, we create a list of `countries`. When we make a request, we use `random.choice()` to use a random country from the list. Our `proxy_url` gets formatted to fit the country.

The example below creates a small list of [rotating p](/solutions/rotating-proxies)[roxies](/blog/proxy-101/best-rotating-proxies).

```none
import httpx
import asyncio
import random

countries = ["us", "gb", "au", "ca"]
username = "your-username"
proxy_url = "brd.superproxy.io:33335"

datacenter_zone = "your-zone"
datacenter_pass = "your-password"

for random_proxy in countries:
    print("----------connection info-------------")
    datacenter_proxy = f"http://brd-customer-{username}-zone-{datacenter_zone}-country-{random.choice(countries)}:{datacenter_pass}@{proxy_url}"

    ip_info = httpx.get("https://geo.brdtest.com/mygeo.json", proxy=datacenter_proxy)

    print(ip_info.text)

```

This example really isn’t all that different from your first. Here are the key differences.

- We create an array of countries: `["us", "gb", "au", "ca"]`.
- Instead of making a single request, we make multiple ones. Each time we create a new request, we use `random.choice(countries)` to choose a random country each time we create our `proxy_url`.

## [](#How-To-Create-a-Fallback-Proxy-Connection)How To Create a Fallback Proxy Connection

In the examples above, we’ve used only datacenter and free proxies. Free proxies aren’t very reliable. Datacenter proxies tend to get blocked with more difficult sites.

In this example, we create a function called `safe_get()`. When we call this function, we first try to get the url using a datacenter connection. When this fails, we *fall back* to our residential connection.

```none
import httpx
from bs4 import BeautifulSoup
import asyncio

country = "us"
username = "your-username"
proxy_url = "brd.superproxy.io:33335"

datacenter_zone = "datacenter_proxy1"
datacenter_pass = "datacenter-password"

residential_zone = "residential_proxy1"
residential_pass = "residential-password"

cert_path = "/home/path/to/brightdata_proxy_ca/New SSL certifcate - MUST BE USED WITH PORT 33335/BrightData SSL certificate (port 33335).crt"

datacenter_proxy = f"http://brd-customer-{username}-zone-{datacenter_zone}-country-{country}:{datacenter_pass}@{proxy_url}"

residential_proxy = f"http://brd-customer-{username}-zone-{residential_zone}-country-{country}:{residential_pass}@{proxy_url}"

async def safe_get(url: str):
    async with httpx.AsyncClient(proxy=datacenter_proxy) as client:
        print("trying with datacenter")
        response = await client.get(url)
        if response.status_code == 200:
            soup = BeautifulSoup(response.text, "html.parser")
            if not soup.select_one("form[action='/errors/validateCaptcha']"):
                print("response successful")
                return response
    print("response failed")
    async with httpx.AsyncClient(proxy=residential_proxy, verify=cert_path) as client:
        print("trying with residential")
        response = await client.get(url)
        print("response successful")
        return response

async def main():
    url = "https://www.amazon.com"
    response = await safe_get(url)
    with open("out.html", "w") as file:
        file.write(response.text)

asyncio.run(main())

```

This example is a bit more complicated than the other ones we’ve dealt with in this article.

- We now have two sets of config variables, one for our datacenter connection, and one for our residential connection.
- This time, we use an `AsyncClient()` session to introduce some of the more advanced functionality of HTTPX.
- First, we attempt to make our request with the `datacenter_proxy`.
- If we fail to get a proper response, we retry the request using our `residential_proxy`. Also note the `verify` flag in the code. When using our residential proxies, you need to download and use our [SSL certificate](https://docs.brightdata.com/general/account/ssl-certificate).
- Once we’ve got a solid response, we write the page to an HTML file. We can open this page up in our browser and see what the proxy actually accessed and sent back to us.

If you try the code above, your output and resulting HTML file should look a lot like this.

```none
trying with datacenter
response failed
trying with residential
response successful

```

## [](#How-Bright-Data-Products-Help)How Bright Data Products Help

As you’ve probably noticed throughout this article, our datacenter proxies are very affordable and our [residential proxies](/proxy-types/residential-proxies) provide an excellent fallback for when datacenter proxies don’t work. We also provide various other tools to assist with your data collection needs.

- [Web Unlocker](/products/web-unlocker): Get past even the most difficult anti-bots. Web Unlocker automatically recognizes and solves any CAPTCHAs on the page. Once it’s through the anti-bots, it sends you back the web page.
- [Scraping Browser](/products/scraping-browser): This product has even more features. Scraping Browser actually allows you to control a remote browser with [proxy integration](/integration) and an [automated CAPTCHA solver](/products/web-unlocker/captcha-solver).
- [Web Scraper APIs](/products/web-scraper): With these APIs, we do the scraping for you. All you need to do is call the API and parse the JSON data you receive in the response.
- [Datasets](/products/datasets): Explore our dataset marketplace to find hundreds of pre-collected datasets, or request/build a custom one. You can choose a refresh rate and filter only the data points you need.

## [](#Conclusion)Conclusion

When you combine HTTPX with our proxies you get a private, efficient, and reliable way to scrape the web. If you want to rotate proxies, it’s as simple as using Python’s built-in `random` library. With a combination of datacenter and residential proxies, you can build a redundant connection that gets past most blocking systems.

As you learned, Bright Data offers the full package for your web scraping projects. Start your free trial with Bright Data’s proxies today!



Start free trial[Start free with Google](# "Start free with Google")









 [ ](https://www.linkedin.com/in/jacob-nulty-682803187/)

Jake Nulty

 Technical Writer



  6 years experience



Jacob Nulty is a Detroit-based software developer and technical writer exploring AI and human philosophy, with experience in Python, Rust, and blockchain.



Expertise

  Data Structures   Python   Rust



 [ View all articles ](https://brightdata.com/blog/authors/jake-nulty)











 Table of Contents







400M+ Residential Proxies

**Get 50% off** on residential proxies for 6 months! Use code **RESIGB50**.

[See pricing](/pricing/proxy-network/residential-proxies "See pricing")

1.3M+ Datacenter Proxies

Pay only for the IP, starting from $0.90.

[See pricing](/pricing/proxy-network/datacenter-proxies "See pricing")







 [ ](https://news.ycombinator.com/submitlink?t=How+To+Use+Proxies+With+HTTPX&u=https://brightdata.com/blog/proxy-101/httpx-with-proxies) [ ](https://www.linkedin.com/shareArticle?mini=true&title=How+To+Use+Proxies+With+HTTPX&url=https://brightdata.com/blog/proxy-101/httpx-with-proxies) [ ](http://www.reddit.com/submit?title=How+To+Use+Proxies+With+HTTPX&url=https://brightdata.com/blog/proxy-101/httpx-with-proxies)







##  You might also be interested in

 [ ](https://brightdata.com/blog/ai/openhuman-with-bright-data "Production-Ready Web Access in OpenHuman Through the Bright Data CLI")

 [AI





Antonello Zanini

Technical Writer





### Production-Ready Web Access in OpenHuman Through the Bright Data CLI

Integrate Bright Data CLI with OpenHuman to enable production-ready web access and data collection for AI agents.



 09-Sep-2026

 12 min read

 ](https://brightdata.com/blog/ai/openhuman-with-bright-data)

 [ ](https://brightdata.com/blog/ai/minimax-m3-with-bright-data "Giving self-hosted MiniMax M3 agents live web access with Bright Data")

 [AI





Satyam Tripathi

Technical Writer





### Giving self-hosted MiniMax M3 agents live web access with Bright Data

Self-hosted MiniMax M3 agents get live web access using Bright Data’s search and scraping tools. Bypass blocks and CAPTCHAs.



 09-Sep-2026

 54 min read

 ](https://brightdata.com/blog/ai/minimax-m3-with-bright-data)

 [ ](https://brightdata.com/blog/web-data/multimodal-web-scraping-with-minimax "Multimodal Web Scraping with MiniMax")

 [Web Data





Antonello Zanini

Technical Writer





### Multimodal Web Scraping with MiniMax

Pair Bright Data Web Unlocker with MiniMax M3 vision to extract structured data from images and web page screenshots.



 09-Sep-2026

 4 min read

 ](https://brightdata.com/blog/web-data/multimodal-web-scraping-with-minimax)
