Axios Proxy in Node.js: HTTP and SOCKS5

Learn how to configure an axios proxy in Node.js for HTTP, HTTPS, and SOCKS5, debug failed tunnels, rotate IPs, and verify the exit address.
A blue line-art USB dongle connects to a Raspberry Pi, cloud, and globe on a navy background.

An axios proxy setup in Node.js routes server-side HTTP requests through an HTTP, HTTPS, or SOCKS5 endpoint instead of the machine’s direct connection. This guide shows the reliable agent-based pattern, native fetch with Undici, IP rotation, and a simple way to verify the exit address.

What you need before you start

Use these examples in a Node.js server, worker, or script. Axios’s proxy configuration is a Node.js feature; adding proxy to Axios code that runs in a browser does not change the browser’s network route.

You need:

  • A proxy endpoint with its host, port, and authentication details.
  • A current Node.js installation and an ES module file such as proxy.mjs.
  • Axios for the first three examples, plus the matching agent package for HTTPS or SOCKS5.
  • A destination you own or are authorised to test, such as an API endpoint in your own application.

Install the packages in your project:

npm install axios https-proxy-agent socks-proxy-agent undici

Keep the endpoint in an environment variable rather than committing credentials. If you need a refresher on host, port, username, and password, see Proxy Format Explained and ProxyKite’s proxy format guide.

For the examples, these placeholders mean:

http://USERNAME:PASSWORD@PROXY_HOST:PROXY_PORT

The http:// at the start describes how your application connects to the proxy gateway. It does not mean that an HTTPS destination is sent without TLS. For an HTTPS destination, a standard HTTP proxy can open a CONNECT tunnel and Axios then negotiates TLS with the destination.

Step 1: Try Axios’s built-in proxy option

Why: Axios already supports an HTTP proxy object, so this is the shortest configuration for a conventional HTTP proxy.

Create axios-basic.mjs:

import axios from "axios";

const client = axios.create({
  proxy: {
    protocol: "http",
    host: process.env.PROXY_HOST,
    port: Number(process.env.PROXY_PORT),
    auth: {
      username: process.env.PROXY_USER,
      password: process.env.PROXY_PASSWORD,
    },
  },
  timeout: 30_000,
});

const response = await client.get("https://api.ipify.org?format=json");
console.log(response.data);

Run it with your values:

PROXY_HOST=your-proxy-host \
PROXY_PORT=12345 \
PROXY_USER=your-user \
PROXY_PASSWORD='your-password' \
node axios-basic.mjs

Success looks like a JSON response from the test endpoint. The address should be the proxy’s exit address, not the public address of the server running Node.js.

Axios also reads the conventional HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables. Set proxy: false when you want to ignore those variables for a request, especially when an explicit agent is responsible for the connection. The official Axios request configuration reference documents the interaction between proxy, httpAgent, and httpsAgent.

Step 2: Use https-proxy-agent when HTTPS needs an explicit tunnel

Why: An agent makes the CONNECT path explicit and avoids ambiguity when Axios’s built-in proxy option does not behave correctly with a particular HTTPS gateway.

Create axios-https-agent.mjs:

import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";

const proxyUrl = process.env.PROXY_URL;
if (!proxyUrl) {
  throw new Error("Set PROXY_URL before starting the script");
}

const httpsAgent = new HttpsProxyAgent(proxyUrl);

const response = await axios.get("https://api.ipify.org?format=json", {
  httpsAgent,
  proxy: false,
  timeout: 30_000,
});

console.log(response.data);

Run it with a URL-encoded username and password if either contains @, :, /, or another reserved character:

PROXY_URL='http://USERNAME:PASSWORD@PROXY_HOST:PROXY_PORT' node axios-https-agent.mjs

The important pair is httpsAgent plus proxy: false. The first sends HTTPS requests through the agent; the second prevents Axios from applying an environment proxy or its own proxy path on top of that agent. If the proxy gateway itself uses TLS, use an https:// proxy URL, as documented by the agent package and Axios.

If your application sends both HTTP and HTTPS requests, configure the appropriate agent for each protocol rather than assuming one agent belongs in both slots. For HTTPS destinations, the httpsAgent example above is the useful starting point.

Step 3: Route Axios through a SOCKS5 proxy

Why: Axios’s proxy object describes an HTTP-style proxy. A SOCKS5 endpoint needs an agent that performs the SOCKS handshake before Axios sends the request.

Create axios-socks.mjs:

import axios from "axios";
import { SocksProxyAgent } from "socks-proxy-agent";

const proxyUrl = process.env.SOCKS5_URL;
if (!proxyUrl) {
  throw new Error("Set SOCKS5_URL before starting the script");
}

const socksAgent = new SocksProxyAgent(proxyUrl);

const client = axios.create({
  httpAgent: socksAgent,
  httpsAgent: socksAgent,
  proxy: false,
  timeout: 30_000,
});

const response = await client.get("https://api.ipify.org?format=json");
console.log(response.data);

Run it like this:

SOCKS5_URL='socks5://USERNAME:PASSWORD@PROXY_HOST:PROXY_PORT' \
node axios-socks.mjs

Success means the request completes and the returned address belongs to the SOCKS5 exit. The official socks-proxy-agent README describes it as an http.Agent implementation for HTTP and HTTPS requests, which is why the same agent is placed in both Axios slots.

Do not remove proxy: false from this example if HTTP_PROXY or HTTPS_PROXY is set in the process environment. Otherwise, Axios can select its own proxy path instead of letting the SOCKS agent handle the connection.

Step 4: Use native-style fetch with Undici’s ProxyAgent

Why: Node’s built-in fetch is powered by Undici, but the separately installed Undici package gives you the ProxyAgent dispatcher and explicit per-request control.

Create fetch-proxy.mjs:

import { fetch, ProxyAgent } from "undici";

const proxyUrl = process.env.PROXY_URL;
if (!proxyUrl) {
  throw new Error("Set PROXY_URL before starting the script");
}

const dispatcher = new ProxyAgent(proxyUrl);

try {
  const response = await fetch("https://api.ipify.org?format=json", {
    dispatcher,
  });

  if (!response.ok) {
    throw new Error(`Proxy request returned HTTP ${response.status}`);
  }

  console.log(await response.json());
} finally {
  await dispatcher.close();
}

The dispatcher is the Undici equivalent of the Axios agent in the earlier examples. For a long-running worker, you can create one ProxyAgent, reuse it for requests to the same proxy, and close it during shutdown. The official Undici ProxyAgent documentation covers HTTP, HTTPS, SOCKS5 proxy URIs, authentication, local dispatchers, and global dispatchers.

If you want to keep using the global fetch, import setGlobalDispatcher from undici, set a ProxyAgent, and then call fetch as usual. A local dispatcher is safer when only some requests should use the proxy.

Step 5: Rotate the IP from Node.js

Why: Rotation belongs to the proxy service or the mobile connection, not to Axios. Your Node code should call the rotation control, wait for its successful response, and then verify the new exit address.

ProxyKite supports on-demand rotation, rotation links, and timers. First copy the rotation link or API details for the specific dongle. The rotation-links guide and rotation API reference show the exact control available to your account.

For a GET-based rotation link, the sequence is:

import axios from "axios";

const rotationUrl = process.env.ROTATION_URL;
if (!rotationUrl) {
  throw new Error("Set ROTATION_URL before starting the script");
}

const rotationResponse = await axios.get(rotationUrl, {
  timeout: 30_000,
});

if (rotationResponse.status < 200 || rotationResponse.status >= 300) {
  throw new Error(`Rotation returned HTTP ${rotationResponse.status}`);
}

console.log("Rotation control accepted");

If your API entry specifies POST, use axios.post with the documented body instead. Do not guess the method or payload. After the control request succeeds, send a fresh request through the same proxy endpoint and check the exit IP. Treat the address returned by that check as the source of truth for your application.

For the networking side of a rotating 4G setup, see How a 4G Rotating Proxy Changes IPs. ProxyKite’s per-dongle licence includes HTTP and SOCKS5 endpoints, on-demand or scheduled rotation, and cloud routing without requiring a static IP or inbound port forwarding. The licence is $9 per dongle per month, with traffic using your own SIM data plan and no per-GB charge.

Step 6: Check the exit IP and response path

Why: A successful TCP connection only proves that something answered. An exit-IP check confirms that the request reached the destination through the intended proxy.

Reuse the client from Step 2 or Step 3 and run:

const { data } = await client.get("https://api.ipify.org?format=json");
console.log("Exit IP:", data.ip);

For a more useful application check, log the destination status, elapsed time, and a request ID supplied by your own API. Do not log the proxy URL, Proxy-Authorization header, cookies, or response bodies that may contain customer data.

Use this small verification checklist:

  1. The request is running in Node.js, not browser JavaScript.
  2. The proxy host and port are reachable from the machine running the script.
  3. The proxy scheme matches the endpoint: http:// or https:// for an HTTP-style gateway, socks5:// for SOCKS5.
  4. Axios is using the intended agent, and proxy: false is present when that agent owns routing.
  5. The exit-IP response is different from the direct connection when the proxy is expected to change it.
  6. A rotation response is followed by a new exit-IP check rather than an assumption that the address changed.

Troubleshooting Axios proxy errors

ECONNRESET or a TLS error on an HTTPS URL

Check whether the gateway is an HTTP proxy carrying an HTTPS destination. Try the explicit HttpsProxyAgent example, keep proxy: false, and make sure the proxy URL uses the gateway’s actual scheme. Do not change the destination from https:// to http:// just to hide a TLS error.

Axios connects directly instead of using the agent

Look for HTTP_PROXY, HTTPS_PROXY, or NO_PROXY in the process environment. With an explicit agent, set proxy: false in the Axios instance or request, then print only safe configuration such as the destination hostname and selected protocol.

The SOCKS5 example says the proxy protocol is invalid

Check the endpoint copied from your provider and use the correct socks5:// scheme. Make sure the same SocksProxyAgent is assigned to both httpAgent and httpsAgent, and that the Axios config includes proxy: false.

Authentication fails even though the password looks right

A reserved character can change how a URL is parsed. Encode credentials before constructing the URL, or use Axios’s structured auth object with the built-in HTTP proxy option. Also check that the proxy password is separate from any dashboard password.

Rotation returns success but the IP check is unchanged

Verify that the control link belongs to the same dongle as the proxy endpoint. Make the exit-IP request after the rotation control has completed, and use the documented API response or retry policy rather than a hard-coded speed promise. If the address still does not change, inspect the rotation response and the dongle’s current mobile connection state.

FAQ

Does Axios support SOCKS5 proxies?

Not through the ordinary proxy object. Use socks-proxy-agent, pass the agent as both httpAgent and httpsAgent, and set proxy: false so Axios does not choose another proxy path.

Why do I need proxy: false with an Axios agent?

It tells Axios not to apply its own proxy resolution, including relevant environment variables. The custom agent then owns the connection to the proxy and performs the HTTP CONNECT or SOCKS handshake.

Can I use this Axios proxy configuration in a browser?

No. httpAgent, httpsAgent, and the Node Axios proxy path are server-side controls. Keep proxy credentials in a backend, worker, or private script rather than shipping them in frontend JavaScript.

How do I rotate a proxy IP in a Node.js script?

Call the rotation link or API for the specific proxy, using the documented method and payload. After it succeeds, make a new request through the proxy and verify the returned exit IP before continuing.

What is the simplest way to test an Axios proxy?

Make a GET request to an IP-echo endpoint through the configured client and compare its returned address with a direct request. Then test the real API you are authorised to call, because an IP check alone does not verify destination-specific authentication, TLS, redirects, or response handling.

Previous Article

ERR_PROXY_CONNECTION_FAILED: Causes and Fixes

Next Article

IPv6 Mobile Proxy: NAT64 and 464XLAT Explained