To use a proxy in Rust, configure a reqwest::Client with Proxy::all(), add the proxy’s login details, and send your request through that client. A good first test is an IP lookup. It gives you something concrete to compare before you put the same settings into your application.
This tutorial starts with a small command-line program. You’ll get one request working, check the result, and then adapt it for IP authentication, SOCKS5, or an existing async project.
Set up a small Rust project
Open a terminal and create a project.
cargo new rust-proxy-demo
cd rust-proxy-demo
Add this dependency to Cargo.toml, underneath the existing [dependencies] heading.
reqwest = { version = "=0.13.5", default-features = false, features = ["blocking", "rustls", "socks"] }
The examples use reqwest 0.13.5. The blocking feature lets this little program run with a regular main() function, rustls enables HTTPS, and socks adds SOCKS support. You can find the available features in the reqwest documentation.
If your application already runs on Tokio, jump to the async section below after reading the proxy setup. For a standalone script, the blocking client keeps the first example easier to follow.
Make your first request through the proxy
You’ll need the proxy host, port, username, and password from your provider. Set them as environment variables so you can change the connection without editing the Rust file. The values below are placeholders.
On Linux or macOS, use the following commands.
export PROXY_ENDPOINT='http://proxy.example:8080'
export PROXY_USER='your-proxy-user'
export PROXY_PASSWORD='your-proxy-password'
export CHECK_URL='https://api.ipify.org'
In Windows PowerShell, set the same four variables like this.
$env:PROXY_ENDPOINT = 'http://proxy.example:8080'
$env:PROXY_USER = 'your-proxy-user'
$env:PROXY_PASSWORD = 'your-proxy-password'
$env:CHECK_URL = 'https://api.ipify.org'
Use the scheme and port your provider specifies. For example, http:// at the start of the proxy address describes the connection to the proxy. Your destination can still be an HTTPS website.
Replace src/main.rs with this code.
use reqwest::{blocking::Client, Proxy};
use std::{env, error::Error, time::Duration};
fn main() -> Result<(), Box<dyn Error>> {
let proxy = Proxy::all(env::var("PROXY_ENDPOINT")?)?.basic_auth(
&env::var("PROXY_USER")?,
&env::var("PROXY_PASSWORD")?,
);
let client = Client::builder()
.no_proxy()
.proxy(proxy)
.connect_timeout(Duration::from_secs(5))
.timeout(Duration::from_secs(20))
.build()?;
let response = client
.get(env::var("CHECK_URL")?)
.send()
.map_err(reqwest::Error::without_url)?;
println!("HTTP {}", response.status());
let body = response
.error_for_status()
.and_then(|response| response.text())
.map_err(reqwest::Error::without_url)?;
println!("{}", body.trim());
Ok(())
}
Run it from the project directory.
cargo run
When the request succeeds, you should see an HTTP status followed by the public IP reported for the connection. This is an example of the output, using a documentation-only address.
HTTP 200 OK
203.0.113.42
The first build takes longer because Cargo downloads and compiles the dependencies. Later runs reuse that work. Keep the generated Cargo.lock with this application if you want subsequent builds to resolve the same dependency versions.
The code prints the status before reading the body. It also turns HTTP 4xx and 5xx responses into errors, so a failed request doesn’t quietly look like a successful IP lookup. The IP service used here returns the caller’s public address. You can also set CHECK_URL to a test endpoint you control. See the ipify API and reqwest response handling.
Check which IP your request is using
Compare the result with a direct request from the same machine. With curl installed, this command bypasses its proxy settings for the lookup.
curl --noproxy '*' https://api.ipify.org
In PowerShell, use curl.exe --noproxy '*' https://api.ipify.org so the command runs curl itself.
Compare both addresses with the exit IP your provider expects. An IP-check response shows the address that particular service saw. If you need to confirm access to your own API, make a second request to that API and check its logs too.
In the Rust example, .no_proxy() comes before .proxy(proxy). It clears inherited proxy settings, then the next call adds the explicit proxy you supplied. The order matters. Calling .no_proxy() after adding your proxy would clear it again. See ClientBuilder::no_proxy().
Proxy::all() covers HTTP and HTTPS destinations. If you use Proxy::http() instead, its rule covers HTTP destination URLs. That’s an easy mistake to miss when your test URL starts with https://. The rule names and examples are documented in the Proxy API.
Using a proxy with IP authentication
Some proxies authorize your machine’s public IP instead of asking for a username and password. If that’s how your endpoint works, replace the first proxy declaration in main() with this line.
let proxy = Proxy::all(env::var("PROXY_ENDPOINT")?)?;
The rest of the program stays the same. Add the public IP of the machine running Rust to your provider’s allowlist. When you move the program from your laptop to a VPS, check the VPS address and authorize it as well.
With username-and-password authentication, Proxy::basic_auth() supplies the proxy credentials. If the destination API also has a login, configure that on the request separately. For example, a destination bearer token belongs on client.get(url).bearer_auth(token). Our proxy authentication guide covers both access methods.
Why an HTTP proxy works with an HTTPS URL
You may have an address such as http://proxy.example:8080 and want to request https://api.example.com. That’s a normal setup. reqwest asks the HTTP proxy to open a CONNECT tunnel, then establishes TLS with the destination through that tunnel.
Keep the proxy scheme your provider gave you. Changing it to https:// asks for TLS on the connection to the proxy itself, which requires an endpoint that supports it. Basic proxy credentials sent over a plain HTTP connection are not encrypted on that hop.
Certificate checks stay enabled in the example. If a certificate error appears, check the system clock, destination certificate, and any company TLS inspection first. A network that uses a private certificate authority may need that CA added to the client.
let pem = std::fs::read("company-ca.pem")?;
let certificate = reqwest::Certificate::from_pem(&pem)?;
// Add this while building the client, before .build().
let builder = Client::builder()
.no_proxy()
.proxy(proxy)
.tls_certs_merge([certificate]);
Use a CA provided by your network administrator. tls_certs_merge() adds it alongside the existing trust store. The client certificate documentation explains this setting. For the protocol differences, see our HTTP, HTTPS, and SOCKS5 guide.
Switch to SOCKS5
The dependency configuration above already includes the socks feature. To use a SOCKS5 endpoint that resolves destination hostnames at the proxy, change the endpoint variable.
export PROXY_ENDPOINT='socks5h://proxy.example:1080'
cargo run
In PowerShell, set $env:PROXY_ENDPOINT = 'socks5h://proxy.example:1080' and run cargo run again. Use your provider’s actual SOCKS port and credentials.
The h in socks5h requests proxy-side resolution of the destination hostname. With socks5, the client resolves that destination locally. The proxy host itself still needs to be reachable from your machine. The main example continues to use the same client builder and login settings. This distinction is implemented in reqwest’s SOCKS connector.
If your project already uses Tokio
An async application uses reqwest::Client and awaits the response. The proxy configuration is the same. Add Tokio to the existing dependencies.
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
Replace src/main.rs with the following async version and run cargo run again. It reads the same environment variables as the first example. In the download, this version is a separate binary you can run with cargo run --locked --bin async-example.
use reqwest::{Client, Proxy};
use std::{env, error::Error, time::Duration};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let proxy = Proxy::all(env::var("PROXY_ENDPOINT")?)?.basic_auth(
&env::var("PROXY_USER")?,
&env::var("PROXY_PASSWORD")?,
);
let client = Client::builder()
.no_proxy()
.proxy(proxy)
.connect_timeout(Duration::from_secs(5))
.timeout(Duration::from_secs(20))
.build()?;
let response = client
.get(env::var("CHECK_URL")?)
.send()
.await
.map_err(reqwest::Error::without_url)?;
println!("HTTP {}", response.status());
let response = response
.error_for_status()
.map_err(reqwest::Error::without_url)?;
let body = response
.text()
.await
.map_err(reqwest::Error::without_url)?;
println!("{}", body.trim());
Ok(())
}
Choose the client that fits the application around it. reqwest’s blocking client runs on a regular thread, while the async client works with Tokio. See the blocking API guidance.
When the request fails
Start with the message you actually received. A rejected login and an unreachable port need different fixes.
| What you see | What to check first |
|---|---|
| HTTP 407 | Proxy credentials, account status, and the authentication method required by that endpoint. |
| A connection or tunnel error | Proxy host, port, scheme, and whether the machine can reach that address. A rejected CONNECT login can also surface here before a destination response exists. |
| A timeout | Whether the proxy connects, whether the destination responds, and whether your configured limits fit the request. |
| A certificate error | Destination hostname, certificate chain, system clock, and any private CA used by your network. |
| HTTP 403 or 429 | Destination access rules and request limits. Try the same endpoint with a single request before adding concurrency. |
| An environment-variable error before the request | That all four variables were set in the terminal where you ran cargo run. |
The example sets a five-second connection timeout and a twenty-second request timeout. Adjust those values for the API you’re using. A slow download and a quick IP lookup may need different limits.
The two without_url() calls keep the request URL out of reqwest’s printed errors. That’s handy when an API uses query parameters for sensitive values. The error itself still propagates to the terminal. Applications that need structured handling can also inspect is_timeout() and is_connect(). See reqwest’s Error reference.
A few things to keep in mind as the script grows
Should I build a client for every request?
Create the configured client once and reuse it for requests using that proxy. reqwest keeps a connection pool, so building a fresh client inside every loop adds avoidable setup work. The Client documentation describes connection reuse.
Can I use environment proxy settings instead?
Yes. A default native reqwest client can read HTTP_PROXY, HTTPS_PROXY, and ALL_PROXY. The tutorial configures the proxy explicitly to make each run easier to inspect. If your deployment manages proxy settings centrally, use the environment-based setup described in our environment variable guide.
Will a new client give me a new IP?
That depends on the proxy service. A fixed endpoint may keep the same exit address. A rotating service may use a session identifier, a rotation interval, or connection-based rules. Check those details with your provider before adding rotation logic to Rust.
Where should I go after the IP check works?
Use the same client for one request to your actual API. Confirm the expected response, then add the rest of your application’s logic. If you need help checking the route, our proxy verification guide walks through the next checks.
Examples and testing
Updated November 22, 2026. The blocking and async programs were compiled with Rust 1.99.0 and reqwest 0.13.5 on Ubuntu 24.04 in WSL2. Both passed local checks for HTTP forwarding, proxy authentication failures, HTTPS CONNECT, certificate validation, and SOCKS5 with the destination hostname passed to the proxy.
You can download the source files for this tutorial, including the blocking and async examples and their dependency lockfile.
More Rust examples
Our earlier diagnostic example bundle includes additional route, authentication, and TLS checks. You can also browse the Rust examples in the source repository or return to the developer guides.
