The essential points from this guide -- each one is explained in detail below.
Use https-proxy-agent or http-proxy-agent packages to create proxy agents for any Node.js HTTP client.
axios supports proxy config through an agent option or a proxy object with host and port fields.
undici (Node.js built-in fetch engine) uses its own ProxyAgent with a uri constructor parameter.
node-fetch requires a proxy agent passed explicitly since it does not read environment variables by default.
The https-proxy-agent package is the foundation for proxy support in most Node.js HTTP clients. It creates an Agent instance that tunnels HTTPS requests through an HTTP CONNECT proxy. Install both http-proxy-agent and https-proxy-agent for complete protocol coverage.
import { HttpsProxyAgent } from 'https-proxy-agent';
import fetch from 'node-fetch';
const agent = new HttpsProxyAgent('http://USER:PASS@gw.knoxproxy.com:7000');
const response = await fetch('https://httpbin.org/ip', { agent });
const data = await response.json();
console.log(data.origin); // Proxy exit IPThe agent handles the CONNECT tunnel setup, TLS negotiation, and credential authentication transparently. You create it once and reuse it across multiple requests. For SOCKS5 proxies, use the socks-proxy-agent package with the same pattern, pointing at port 7001 for KnoxProxy.
axios supports proxy configuration through either a proxy config object or by passing a custom httpsAgent. The agent approach is more flexible because it works with authenticated proxies and SOCKS5 connections. The proxy config object only supports basic HTTP proxies without authentication in the URL.
import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';
const agent = new HttpsProxyAgent('http://USER:PASS@gw.knoxproxy.com:7000');
// Agent approach (recommended)
const response = await axios.get('https://httpbin.org/ip', {
httpsAgent: agent,
httpAgent: agent,
});
console.log(response.data);
// Create a reusable axios instance with proxy
const client = axios.create({
httpsAgent: agent,
httpAgent: agent,
timeout: 30000,
});
const r = await client.get('https://example.com');Creating a dedicated axios instance with the agent pre-configured keeps your code clean and ensures every request from that instance routes through the proxy without repeating the agent option.
Node.js 18+ includes a built-in fetch powered by undici. To route native fetch requests through a proxy, use undici's ProxyAgent and the setGlobalDispatcher function. This approach requires no third-party packages beyond undici itself.
import { ProxyAgent, setGlobalDispatcher } from 'undici';
const proxyAgent = new ProxyAgent({
uri: 'http://USER:PASS@gw.knoxproxy.com:7000',
requestTls: { rejectUnauthorized: true },
});
// Global dispatcher -- all fetch calls use the proxy
setGlobalDispatcher(proxyAgent);
const response = await fetch('https://httpbin.org/ip');
const data = await response.json();
console.log(data);Setting a global dispatcher affects all fetch calls in the process, which is convenient for scrapers but may not be suitable for applications that need to mix proxied and direct traffic. For per-request control, pass the dispatcher option to individual fetch calls instead of setting it globally.
got is a popular Node.js HTTP client known for its retry logic and hook system. It supports proxies through the agent option, following the same pattern as other Node.js clients. got also provides built-in retry, timeout, and error handling that work well with proxy-based scraping.
import got from 'got';
import { HttpsProxyAgent } from 'https-proxy-agent';
const agent = new HttpsProxyAgent('http://USER:PASS@gw.knoxproxy.com:7000');
const response = await got('https://httpbin.org/ip', {
agent: { https: agent },
retry: { limit: 3 },
timeout: { request: 30000 },
}).json();
console.log(response);got's built-in retry logic automatically retries failed requests, which pairs well with proxy rotation. When a request fails due to a blocked IP, the retry hits the backconnect gateway again and gets a fresh exit IP, often succeeding on the next attempt without any extra code.
Ready to put this into practice? Browse Residential Proxies
KnoxProxy Research Team · Technical Content
Network engineers and proxy infrastructure specialists with 10+ years in anti-bot systems, web scraping, and IP routing.
90.4M+ ethically sourced residential IPs across 195 countries. Start free -- no credit card required.