Choose the configuration your HTTP client uses
A Node.js proxy setting belongs to the client making the request. Copying an Axios proxy object into a fetch call does not configure that fetch request. Decide which client your script uses, then keep its proxy settings beside the request while you test it.
The examples below use Axios’s Node HTTP adapter and the fetch function imported from Undici. The explicit adapter in the Axios example avoids accidentally selecting a different transport. The Undici import keeps fetch and its ProxyAgent on the installed package version. These are server-side scripts, not browser fetch examples.
Check your runtime with node --version and use a fresh working folder. We ran these examples with Node.js 24.18.1, Axios 1.21.1 and Undici 8.13.1. Install those two packages in that folder with the command below. A fixed version makes the example repeatable; it is not a promise about every earlier or later release.
| Client | Where the proxy goes | Proxy login |
|---|---|---|
| Axios with its HTTP adapter | proxy object | proxy.auth.username and password |
| fetch imported from Undici | dispatcher: a ProxyAgent | ProxyAgent token containing Basic authentication |
npm install axios@1.21.1 undici@8.13.1Prepare one small HTTPS test page and private connection details
Use a small HTTPS endpoint you own or are authorized to test. Put its URL in PROXY_TEST_URL. It should return a successful response without a redirect, and its access log should record the X-Proxy-Test-Id request header. Each example generates a new identifier so you can find its exact request without logging the response body.
Supply PROXY_USERNAME and PROXY_PASSWORD privately to the process that runs the script. Do not paste a real proxy URL into a commit, a support screenshot or a shared command transcript. Keep the complete generated username when it includes a location or session suffix. An IPMusk account API key is not the proxy password.
The default HTTP gateway in these scripts is gate.ipmusk.com on port 31212 for IPMusk Rotating Residential. For an assigned Static ISP or Datacenter resource, set PROXY_HOST and PROXY_PORT to that resource’s issued details instead. The gateway is where the client connects; it is not necessarily the address the destination sees.
Send the request with Axios
Save this as axios-proxy.mjs. It requires the target and credentials before it sends anything, explicitly selects the HTTP adapter, and applies an HTTP proxy to the HTTPS target. The proxy protocol describes the connection to the gateway, not the scheme of the destination URL.
Run node axios-proxy.mjs after injecting the environment variables. The ten-second timeout keeps a small diagnostic request bounded. Redirects are disabled so the result stays tied to the test endpoint you selected. The output contains only the HTTP status and the generated request identifier.
The catch handler deliberately avoids printing the complete Axios error object, which can include request configuration. A failed run exits unsuccessfully with a short message. Diagnose it using the private connection details and the redacted request records rather than sharing the raw object.
import axios from 'axios';
import { randomUUID } from 'node:crypto';
async function main() {
for (const key of ['PROXY_TEST_URL', 'PROXY_USERNAME', 'PROXY_PASSWORD']) {
if (!process.env[key]) throw new Error(`Missing ${key}`);
}
const target = new URL(process.env.PROXY_TEST_URL);
if (target.protocol !== 'https:') throw new Error('Use your HTTPS test page');
const requestId = randomUUID();
const response = await axios.get(target.href, {
adapter: 'http',
proxy: {
protocol: 'http',
host: process.env.PROXY_HOST ?? 'gate.ipmusk.com',
port: Number(process.env.PROXY_PORT ?? '31212'),
auth: {
username: process.env.PROXY_USERNAME,
password: process.env.PROXY_PASSWORD,
},
},
headers: { 'X-Proxy-Test-Id': requestId },
timeout: 10000,
maxRedirects: 0,
});
console.log(JSON.stringify({ status: response.status, requestId }));
}
main().catch(() => {
console.error('Proxy test failed; check the private connection details.');
process.exitCode = 1;
});Give Undici fetch a ProxyAgent dispatcher
Save this as fetch-proxy.mjs. The ProxyAgent receives the proxy origin and a Basic authentication token made from the supplied username and password. The token is an encoding of the credentials, not encryption or a safe value to publish. Keep it private just like the password.
The dispatcher option attaches that agent to this request. A successful page response is checked explicitly, the small body is consumed, and the agent is closed in finally. This prevents the example from leaving its connection pool open after the check. The script rejects redirects and has the same ten-second limit as the Axios example.
Run node fetch-proxy.mjs with the same environment and test URL. Built-in Node fetch has runtime-dependent environment proxy facilities too, but this example uses an explicit imported client and dispatcher so the routing choice is visible at the call site. Do not assume another process inherited either this agent or your shell’s environment.
import { fetch, ProxyAgent } from 'undici';
import { randomUUID } from 'node:crypto';
async function main() {
for (const key of ['PROXY_TEST_URL', 'PROXY_USERNAME', 'PROXY_PASSWORD']) {
if (!process.env[key]) throw new Error(`Missing ${key}`);
}
const target = new URL(process.env.PROXY_TEST_URL);
if (target.protocol !== 'https:') throw new Error('Use your HTTPS test page');
const host = process.env.PROXY_HOST ?? 'gate.ipmusk.com';
const port = Number(process.env.PROXY_PORT ?? '31212');
const token = Buffer.from(
`${process.env.PROXY_USERNAME}:${process.env.PROXY_PASSWORD}`,
).toString('base64');
const dispatcher = new ProxyAgent({
uri: `http://${host}:${port}`,
token: `Basic ${token}`,
});
try {
const requestId = randomUUID();
const response = await fetch(target.href, {
dispatcher,
headers: { 'X-Proxy-Test-Id': requestId },
redirect: 'error',
signal: AbortSignal.timeout(10000),
});
if (!response.ok) throw new Error('Test page returned an error');
await response.arrayBuffer();
console.log(JSON.stringify({ status: response.status, requestId }));
} finally {
await dispatcher.close();
}
}
main().catch(() => {
console.error('Proxy test failed; check the private connection details.');
process.exitCode = 1;
});Verify both success and failure from the same process
Find the printed request identifier in your test endpoint’s access log. For a real external proxy, compare the source address observed by your server or trusted ingress with the expected exit information. When an ingress sits in front of the application, establish which address field is trustworthy. A client-supplied forwarding header alone cannot prove the route.
Also try an intentionally wrong test password and an unused local proxy port in an isolated test environment. Those requests should fail before they reach the target. A result that still succeeds may belong to a different request, an excluded destination or a process that never received the intended proxy setting.
Our local verification used a real HTTPS origin with a locally trusted certificate and an authenticated HTTP CONNECT proxy. Each script reached the origin with the correct synthetic credentials. The wrong-password and unreachable-proxy cases failed without an origin request. The origin did not receive Proxy-Authorization. Those six checks validate the local examples, not a production IPMusk exit, its country or access to any third-party site.
Troubleshoot the first failed step
Keep TLS verification enabled. If your own test endpoint uses a private certificate authority, configure the client to trust that CA rather than disabling certificate checks. Once the narrow request works, add the surrounding application back one step at a time. Use an approved source interface if a destination refuses access, and keep the failed request’s identifier and time for support.
| Symptom | Next check |
|---|---|
| The script stops before connecting | Confirm the required environment variables and the installed Node/package versions. |
| The proxy rejects authentication | Check the full issued username, password, host and protocol. A 407 response identifies a proxy authentication challenge. |
| The proxy host is unreachable | Check reachability from the runner that executes Node, including its container or CI network. |
| The target returns an error | Compare its access log and permissions; successful proxy authentication does not grant destination access. |
