How to use proxies in Python and Node.js
Code examples for requests, httpx, aiohttp, Node.js fetch and axios — with HTTP and SOCKS5, authentication and IP rotation.
If you're writing a scraper, a bot or any automation, adding a proxy takes a couple of lines. Below are working examples for popular libraries. Take the proxy details from “My proxies” in the host:port:login:password format.
Python: requests
import requests
proxy = "http://login:password@host:port"
r = requests.get("https://ipinfo.io/json", proxies={"http": proxy, "https": proxy}, timeout=30)
print(r.json()["ip"])
For SOCKS5, install pip install requests[socks] and use the socks5h:// scheme so DNS lookups also go through the proxy.
Python: httpx and aiohttp (async)
import httpx
async with httpx.AsyncClient(proxy="http://login:password@host:port", timeout=30) as client:
r = await client.get("https://ipinfo.io/json")
import aiohttp
async with aiohttp.ClientSession() as s:
async with s.get("https://ipinfo.io/json", proxy="http://login:password@host:port") as r:
print(await r.json())
Node.js: undici / fetch
import { ProxyAgent, fetch } from 'undici';
const agent = new ProxyAgent('http://login:password@host:port');
const res = await fetch('https://ipinfo.io/json', { dispatcher: agent });
console.log(await res.json());
Node.js: axios
import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';
const agent = new HttpsProxyAgent('http://login:password@host:port');
const { data } = await axios.get('https://ipinfo.io/json', { httpsAgent: agent, proxy: false });
For SOCKS5 in Node.js, use the socks-proxy-agent package.
IP rotation
Private proxies: load the list and pick an address at random or in turn for each request.
Residential and mobile: the IP changes on every request automatically. To keep one IP, add a session to the username: login-session-ab12cd-time-10 (details).
Buying through the API
You can buy and renew proxies from code with the ProxyZen API. Ready-made examples in Python, Node.js, PHP and curl are in our GitHub repository — the link is in the docs.
Common errors
- 407 — wrong username or password.
- Timeout — wrong port; residential and mobile use 8080 for HTTP and 1080 for SOCKS5.
- SSL errors — don't disable certificate checks; usually the scheme is wrong (
http://for an HTTP proxy, even if the site is HTTPS).
Try ProxyZen
IPv4 and IPv6 from $0.05, residential and mobile per GB. Instant delivery, HTTP(S) and SOCKS5.