Using SOCKS5 Proxies with axios
axios's proxy option cannot carry SOCKS5. Install socks-proxy-agent, set it on both agent slots with proxy false, and check the address that comes back.
axios's proxy option configures an HTTP proxy only. SOCKS5 goes in through an agent, which performs the handshake below HTTP and hands axios a finished tunnel.
npm install axios socks-proxy-agentimport axios from "axios";
import { SocksProxyAgent } from "socks-proxy-agent";
const agent = new SocksProxyAgent("socks5h://USERNAME:PASSWORD@HOST:PORT", {
keepAlive: true,
maxSockets: 20,
});
export const client = axios.create({
httpAgent: agent,
httpsAgent: agent,
proxy: false, // required, see below
timeout: 45000,
});
const { data } = await client.get("https://api.ipify.org?format=json");
console.log(data.ip); // the proxy's address, not yoursHOST:PORT must be the SOCKS5 listener: set the Connect card on the Proxies page to SOCKS5 to see it (residential shows a SOCKS5 Port field). Your HTTP credential works on it unchanged. Free accounts are HTTP only; any paid product on the pricing page takes SOCKS5.
The two settings people leave out
proxy: false. axios reads proxy configuration from HTTP_PROXY, HTTPS_PROXY and ALL_PROXY as well as from your config. If one of those is set in the process environment, axios applies it in addition to your agent, and the request either goes through the wrong proxy or tries to speak HTTP to your SOCKS5 endpoint. proxy: false tells axios to use the agent it was given and nothing else. Set it whenever you set an agent, because you cannot see the environment your code will eventually run in. The same variable behaves differently across Node clients; the Node.js proxy guide has the table.
Both agent slots. httpAgent handles http:// targets and httpsAgent handles https://. axios follows redirects by default, and a redirect that crosses schemes switches which agent applies. With only httpsAgent set, the hop to http:// leaves from your own address with no error. Set both slots to the same agent unless you have a reason to differ.
socks5h versus socks5
socks-proxy-agent reads the scheme to decide where DNS happens: socks5h:// hands the hostname to the proxy, socks5:// makes Node resolve it first and send a bare address. Keep the h, so lookups match the exit's region instead of yours. The SOCKS5 setup guide covers the protocol side.
One agent, not one per request
An agent owns a connection pool. Creating one inside a request function discards that pool on every call:
// Wrong: a new agent, a new pool, and a new SOCKS handshake per request.
async function fetchUrl(url) {
const agent = new SocksProxyAgent(PROXY_URL);
return axios.get(url, { httpAgent: agent, httpsAgent: agent, proxy: false });
}Create the agent once at module scope, as in the first example, with keepAlive: true, and share it across every call.
Set maxSockets deliberately. Node's agent default is Infinity, so Promise.all(urls.map(client.get)) opens one socket per URL at the same moment.
Rotating across a pool
One agent per exit, chosen per request:
const agents = PROXY_URLS.map(
(u) => new SocksProxyAgent(u, { keepAlive: true, maxSockets: 10 }),
);
let i = 0;
function nextAgent() {
return agents[i++ % agents.length];
}
export function get(url) {
const agent = nextAgent();
return axios.get(url, { httpAgent: agent, httpsAgent: agent, proxy: false });
}Use the array for static proxies only. Against a rotating or residential gateway, one agent is enough: each new SOCKS5 socket draws an exit, and a kept-alive socket keeps the one it has. Pass keepAlive: false to the agent for a new exit per request, or put a session segment in the username to hold one.
For logged-in work, keep one agent per account for that account's whole session (sticky versus rotating sessions).
Reading the errors
ECONNREFUSEDon the proxy host and port: nothing accepted a TCP connection. Check the endpoint and port; credentials are not involved yet.SocksClientErrorthrown from the agent: the handshake failed. Port first (pointing the agent at the HTTP listener produces this, not a 407), then the credential, then the account tier.- A socket that opens and then goes quiet: usually an exit that has gone away. Try a different exit rather than retrying the same one.
- An axios error whose
response.statusis407: the request reached an HTTP proxy, not the agent. Look for anHTTP_PROXYorHTTPS_PROXYvariable thatproxy: falsewould have ignored; fixing 407 Proxy Authentication Required covers the other causes.
Confirm it at startup
Each of the mistakes above produces a request that succeeds, so the check that matters is the address the destination reports:
import { isIP } from "node:net";
const { status, data } = await client.get("https://api.ipify.org?format=json");
if (status !== 200 || !isIP(data?.ip ?? "")) throw new Error(`egress check failed: HTTP ${status} ${JSON.stringify(data).slice(0, 80)}`);
if (data.ip === MY_OWN_ADDRESS) throw new Error("traffic is not leaving through the proxy");Validate the status and the body before comparing: an error page that merely differs from your own address is not a proxied exit. This proves the route to the echo service, not that any particular target accepts the proxy.
Run it once when the process starts, so a missing agent slot or a stray environment variable is caught before the first real request.