Skip to content

Node.js Proxy Setup: Environment Variables, fetch, axios and SOCKS5

Node.js can send requests through a proxy in several ways, and which one works depends on the client you use and on the proxy type. This guide shows each way with a short example and how to check the result.

Built-in support through environment variables

The Node.js HTTP documentation describes built-in proxy support that reads the HTTP_PROXY, HTTPS_PROXY and NO_PROXY variables, in lower or upper case. It is off unless you turn it on with the NODE_USE_ENV_PROXY=1 variable or the --use-env-proxy flag, and the docs mark it Stability 1.1, active development. The NODE_USE_ENV_PROXY variable was added in v24.0.0 and v22.21.0, and the --use-env-proxy flag and the proxyEnv option of http.Agent in v24.5.0 and v22.21.0; older versions ignore the variable. When on, it applies to fetch() from v24.0.0 and v22.21.0, and to http.request() and https.request() through the global agent from v24.5.0 and v22.21.0, so on v24.0.0 to v24.4.x it covers fetch() only.

The documented proxy URL formats are http:// and https://, with credentials written as http://username:password@host:port. So this built-in support covers HTTP proxies; it does not cover SOCKS5.

Built-in support through environment variables: example

NODE_USE_ENV_PROXY=1 \
HTTPS_PROXY="http://USERNAME:[email protected]:2222" \
node app.js

Replace USERNAME and PASSWORD with the values from My assets. Use HTTP_PROXY too if you request plain http:// addresses. This needs Node.js v24.5.0 or v22.21.0 or newer for the http and https modules; fetch() honours it from v24.0.0 (v22.21.0 on Node 22).

Built-in support through environment variables, continued

The docs also describe http.setGlobalProxyFromEnv() (added in v24.14.0 and v25.4.0) to switch this on from code. Node.js says built-in proxy support is not an anonymity feature and that a proxy can see connection metadata, so use only proxies you trust.

fetch with undici ProxyAgent

Install the undici package and pass a ProxyAgent as the dispatcher. The current undici documentation says a ProxyAgent routes requests through an HTTP, HTTPS or SOCKS5 proxy (check that your installed undici version supports socks5: URLs; the example below uses the HTTP port), and that credentials in the proxy URL are URL-decoded before use.

fetch with undici ProxyAgent: example

import { ProxyAgent, fetch } from 'undici'

const user = encodeURIComponent('USERNAME')
const pass = encodeURIComponent('PASSWORD')
const dispatcher = new ProxyAgent(`http://${user}:${pass}@proxy.proxy302.com:2222`)

const res = await fetch('https://ipinfo.io/json', { dispatcher })
console.log(await res.json())

Encoding the password protects characters such as @, : and / that would otherwise break the URL.

axios

The axios documentation lets you set a proxy object with protocol, host, port and auth (username and password) on a request. For an https:// target, axios 1.16.1 and newer open a CONNECT tunnel through the proxy. In 1.16.0 and older the Node http adapter sends the request to an HTTP proxy as a plain-text forward-proxy request, with no tunnel and no end-to-end TLS, so update axios first (1.17.0 also keeps the TLS options of your own httpsAgent inside the tunnel). If the proxy itself uses HTTPS, set protocol to https; Proxy302's HTTP port 2222 takes protocol http.

axios: example

import axios from 'axios'

const res = await axios.get('https://ipinfo.io/json', {
  proxy: {
    protocol: 'http',
    host: 'proxy.proxy302.com',
    port: 2222,
    auth: { username: 'USERNAME', password: 'PASSWORD' },
  },
})
console.log(res.data)

In the proxy object the username and password are separate fields, so they are not URL-encoded.

Proxy agents, including SOCKS5

The proxy-agents project provides http.Agent classes for the built-in http and https modules. https-proxy-agent connects to an HTTP or HTTPS proxy and uses the CONNECT method. socks-proxy-agent connects to a SOCKS proxy. In the package source, both read the username and password from the proxy URL and URL-decode them, and socks-proxy-agent accepts the schemes socks4, socks4a, socks5 and socks5h.

Proxy agents, including SOCKS5: example

import https from 'node:https'
import { SocksProxyAgent } from 'socks-proxy-agent'

const agent = new SocksProxyAgent(
  `socks5h://${encodeURIComponent('USERNAME')}:${encodeURIComponent('PASSWORD')}@proxy.proxy302.com:3333`
)
https.get('https://ipinfo.io/json', { agent }, (res) => res.pipe(process.stdout))

With socks5h the proxy resolves the host name. Proxy302 SOCKS5 is on port 3333; for an HTTP proxy use HttpsProxyAgent with port 2222.

Check the exit

Request https://ipinfo.io/json through the proxy, as in each example above. The country and org fields should describe the proxy, not your own connection. If the result shows your own address, the request did not use the proxy: check that the variable was set before Node started, or that the agent or dispatcher is passed to this request. The proxy location guide explains the fields.

Typical errors

ECONNREFUSED means the target machine actively refused the connection, which the Node.js error list says usually results from no service being active there: check the host and the port. ETIMEDOUT means the other side did not respond in time: check the port, your network and the timeout you set. ENOTFOUND is a DNS lookup failure: check the host name. Status 407 means the proxy did not accept the credentials; see the authentication guide. Also check that the protocol matches the port, HTTP on 2222 and SOCKS5 on 3333; the connection failed guide walks through the checks.

How to do it with Proxy302

Any of these clients only needs the host, port, username and password of a Proxy302 proxy. Rotating residential traffic is billed per GB and the exit country is set when you generate the proxy; HTTP is on port 2222 and SOCKS5 on port 3333 of proxy.proxy302.com. A static ISP IP stays fixed for the whole lease term and does not rotate. The table shows exits in 7 countries from our latest hourly check; the probe sends its request to ChatGPT, which is only an example target of a network check. A proxy does not change ChatGPT's terms, and OpenAI's list of supported countries still applies. Details are on the rotating residential page and in the proxy location guide.

Measured exits: ChatGPT from 7 countries

Measured exits: ChatGPT from 7 countries
ItemCityNetwork (ISP)ASN
United StatesNeenah, WisconsinCharter Communications IncAS10796
United KingdomCowplain, EnglandTalkTalk Communications LimitedAS13285
GermanyKöln, North Rhine-WestphaliaNetCologne Gesellschaft fur Telekommunikation mbHAS8422
JapanTokyo, TokyoKDDI CORPORATIONAS2516
SingaporeSingapore, SingaporeSingtel Fibre BroadbandAS9506
BrazilMauá, São PauloTELEFÔNICA BRASIL S.AAS27699
IndonesiaBogor, West JavaPT Mora Telematika IndonesiaAS131111

Hourly check of the Proxy302 probe, 25 October 2026, 08:29 UTC: one request to ChatGPT per country through a dynamic residential exit, using the public Proxy302 entry. Only the first three octets of an IP are public. One sample per country, not an average; later checks will differ.

How to do it with Proxy302: steps

  1. Step 1

    Open the rotating residential page, choose the line, and keep Protocol on HTTP or choose SOCKS5, then click Review cost and generate.

  2. Step 2

    Open My assets (the console page that holds everything you bought) and copy HOST, PORT, USERNAME and PASSWORD. The Connect page shows a ready example with your own endpoint.

  3. Step 3

    Enter the details in the browser or tool you are setting up.

  4. Step 4

    Confirm the exit with https://ipinfo.io/json through the proxy.

  5. Step 5

    From mainland China, start the helper tool from the Connect page first and keep it open while you use the proxy.

Prices and limits

The Budget line costs US$1.50 per GB, US$1.35 per GB in a 100 GB package and US$1.20 per GB at 300 GB. There is no free trial, and the smallest purchase is 1 GB. Which exit locations are available changes daily.

FAQ

Does the built-in support work with SOCKS5?

The Node.js docs describe only HTTP and HTTPS proxy URLs for it. For SOCKS5 use a package such as socks-proxy-agent.

How do I put special characters in the proxy password?

Encode the password with encodeURIComponent before placing it in the proxy URL. The agents and undici decode it back when they build the login.

Why does the request still show my own IP?

The proxy was not applied to that request: the variable was set after Node started, or the agent or dispatcher was not passed to the call.

What does ECONNREFUSED mean with a proxy?

Nothing accepted the connection at the proxy host and port you gave. Check both values and the protocol.

Reference links

9
  1. Node.js documentation: HTTP, Built-in Proxy Support

    Official documentation · Confirmed on:

    Read on 25 October 2026: NODE_USE_ENV_PROXY=1, HTTP_PROXY, HTTPS_PROXY and NO_PROXY, proxy URL formats (http and https, with username and password), http.setGlobalProxyFromEnv(), the stability note (1.1, active development) and the security considerations.

  2. Node.js documentation: command-line options, --use-env-proxy

    Official documentation · Confirmed on:

    Read on 25 October 2026: --use-env-proxy is equivalent to NODE_USE_ENV_PROXY=1 and takes precedence when both are set.

  3. Node.js documentation: common system errors

    Official documentation · Confirmed on:

    Read on 25 October 2026: the meaning of ECONNREFUSED, ETIMEDOUT and ENOTFOUND.

  4. undici documentation: ProxyAgent

    Official documentation · Confirmed on:

    Read in full on 25 October 2026: ProxyAgent routes requests through an HTTP, HTTPS or SOCKS5 proxy, can be passed as dispatcher to fetch, and credentials in the proxy URI are URL-decoded.

  5. axios documentation: request config, proxy

    Official documentation · Confirmed on:

    Read on 25 October 2026: the proxy object (protocol, host, port, auth), the CONNECT tunnel for https targets (axios 1.16.1 and newer; checked in lib/adapters/http.js and the 1.16.1 and 1.17.0 release notes), and protocol https for a proxy that uses HTTPS.

  6. proxy-agents project: https-proxy-agent and socks-proxy-agent

    Official documentation · Confirmed on:

    Read on 25 October 2026: the README of both packages and their source; CONNECT for HTTP(S) proxies, the SOCKS schemes accepted, and URL-decoding of the username and password.

  7. MDN: encodeURIComponent()

    Official documentation · Confirmed on:

    Read on 25 October 2026: encodeURIComponent() escapes characters such as ?, =, / & and : for use in a URI component.

  8. Proxy302 product facts and price list

    Product observation · Confirmed on:

    Product facts file of the site (values checked against the console, the backend and the price table on 18-19 October 2026): products, prices in US$ (1 PTC = US$1), lease terms, protocols and ports, countries, quantity limits, no free trial.

  9. Proxy302 hourly connectivity check

    Product observation · Confirmed on:

    Public JSON of the probe at proxy302.com/live/connectivity/chatgpt.json, read 25 October 2026, 08:29 UTC: one request per exit country through a dynamic residential exit; exit city, ASN, network and response time.

Ready to try it?

Create an account, top up once, then pick rotating residential, static ISP or datacenter IPs and have one running in your browser within minutes.