Once you have a working proxy, you still have to tell your tools to use it. Each tool does this differently, and small details such as the URL scheme or where DNS is resolved change the result. This guide shows the exact syntax for curl, Python requests, Node.js and Puppeteer. All addresses are documentation examples (203.0.113.x, 198.51.100.x); replace them with your own proxy.
Before you start: confirm the proxy works
Debugging your script is hard if the proxy itself is dead. Paste it into the proxy checker first. It tests HTTP, HTTPS and SOCKS5 and tells you which protocol the proxy speaks, which is what decides the URL scheme below. If you get a 407 later, see fixing proxy error 407.
curl
Use -x (or --proxy). The scheme selects the protocol:
# HTTP proxy curl -x http://203.0.113.10:8080 https://api.ipify.org # with credentials curl -x http://203.0.113.10:8080 --proxy-user 'user:pass' https://api.ipify.org # SOCKS5, DNS resolved by the proxy (socks5h) or locally (socks5) curl -x socks5h://203.0.113.10:1080 https://api.ipify.org
curl also reads environment variables, which is convenient for scripts and containers:
export http_proxy=http://203.0.113.10:8080 export https_proxy=http://203.0.113.10:8080 export no_proxy=localhost,127.0.0.1,.internal.example curl https://api.ipify.org
Use lowercase names with curl: it deliberately ignores the uppercase HTTP_PROXY (a protection against the "httpoxy" CGI issue), though uppercase HTTPS_PROXY and NO_PROXY are accepted. Many other tools prefer the uppercase forms, so setting both is common. no_proxy is a comma-separated list of hosts that bypass the proxy. Use -v to see the CONNECT request and the proxy's response.
Python requests
Pass a proxies dictionary. The keys are the target URL scheme, the values are the proxy URL:
import requests
proxies = {
"http": "http://203.0.113.10:8080",
"https": "http://203.0.113.10:8080",
}
r = requests.get("https://api.ipify.org", proxies=proxies, timeout=15)
print(r.text)
For SOCKS, install the extra: pip install "requests[socks]", then use a socks5h:// URL so DNS goes through the proxy:
proxies = {
"http": "socks5h://user:[email protected]:1080",
"https": "socks5h://user:[email protected]:1080",
}
requests also honors the HTTP_PROXY, HTTPS_PROXY and NO_PROXY environment variables. To apply a proxy to many calls, set it once on a session: s = requests.Session(); s.proxies.update(proxies). Always set a timeout; without one, a dead proxy can hang a script indefinitely.
Node.js
Node's built-in http and https modules do not read proxy variables by default. The common solution is a proxy agent from npm.
npm install axios https-proxy-agent socks-proxy-agent
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const { SocksProxyAgent } = require('socks-proxy-agent');
// HTTP proxy
const httpAgent = new HttpsProxyAgent('http://user:[email protected]:8080');
// SOCKS5 proxy
const socksAgent = new SocksProxyAgent('socks5h://user:[email protected]:1080');
axios.get('https://api.ipify.org', {
httpAgent: httpAgent,
httpsAgent: httpAgent,
proxy: false, // stop axios from applying its own proxy logic
}).then(r => console.log(r.data));
Swap in socksAgent for the SOCKS5 case. Percent-encode special characters in the username and password with encodeURIComponent.
If you use the built-in fetch (backed by undici), use undici's ProxyAgent as the dispatcher:
const { ProxyAgent, fetch } = require('undici');
const dispatcher = new ProxyAgent('http://203.0.113.10:8080');
fetch('https://api.ipify.org', { dispatcher })
.then(r => r.text())
.then(console.log);
Newer Node.js releases also have an option to honor proxy environment variables natively; check the documentation for your version before relying on it, since it is not available in older long-term-support releases.
Puppeteer
Chrome takes the proxy address as a launch flag, but it does not accept credentials in that flag. Authenticate on the page instead:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
args: ['--proxy-server=http://203.0.113.10:8080'],
});
const page = await browser.newPage();
await page.authenticate({ username: 'user', password: 'pass' });
await page.goto('https://api.ipify.org');
console.log(await page.evaluate(() => document.body.innerText));
await browser.close();
})();
Chrome does not support username and password authentication for SOCKS5 proxies. For those, either whitelist your IP with the provider or run a small local forwarding proxy that adds the credentials.
Verify the exit IP
Every example above prints the address the target sees. If it shows your own IP, the proxy was bypassed (check NO_PROXY, the wrong dictionary key, or a missing proxy: false). If it shows the proxy's IP, look it up in the IP lookup to confirm country, ASN and whether it is flagged as a proxy or VPN.
Handling failures and rotating proxies
Proxies fail more often than ordinary servers, so build for it. Set a connect and read timeout on every request, retry a small number of times, and switch to another proxy when one keeps failing. A simple pattern in Python is to keep a list of working proxies and pick one per request:
import random, requests
pool = [
"http://203.0.113.10:8080",
"http://203.0.113.11:8080",
"http://198.51.100.7:3128",
]
def fetch(url, tries=3):
for _ in range(tries):
p = random.choice(pool)
try:
return requests.get(url, proxies={"http": p, "https": p}, timeout=(5, 15))
except requests.RequestException:
continue
raise RuntimeError("all attempts failed")
Re-test the pool regularly and drop dead entries; a list that worked last week may be mostly dead today. Also respect the target site's terms and rate limits: a proxy changes your address, not your obligations.
Debugging tips
- curl:
-vprints the CONNECT request and the proxy's reply, which usually pinpoints the problem (407, 502, a refused connection). - Python: an
SSLErrororProxyErrorwrapping a connection reset often means the proxy does not support HTTPS tunnelling, or the protocol is wrong. - Node.js: set
NODE_DEBUG=http,netto trace sockets, and logerr.code(for exampleECONNREFUSEDorETIMEDOUT). - Compare with a known-good test: if curl works with the same proxy but your code does not, the difference is in your client configuration, not the proxy.
Common mistakes
- Using
socks5://when you needsocks5h://, which leaks DNS queries from your machine. - Putting
https://in the proxy URL for a proxy that only speaks plain HTTP on its port. The scheme describes the proxy, not the target. - Forgetting timeouts and retries; free proxies fail often.
- Committing proxy credentials to source control. Read them from environment variables.