Key takeaways:
- Axios supports HTTP and HTTPS proxies natively in Node.js, tunneling HTTPS traffic to the target through the CONNECT method.
- Rotating residential proxies can reduce the likelihood of IP bans, but advanced anti-bot systems can still detect them.
- Axios lacks native SOCKS5 support, so you’ll need to use an external agent library to handle those connections.
Web scraping or interacting with external APIs often leads to rate limits and geo-blocks. If you send too many requests from a single IP address, security systems will flag your traffic and drop the connection. To avoid this, you need a proxy to route your requests through a different IP and keep your success rates stable.
An Axios proxy routes your HTTP requests through an intermediary server. While Node.js has built-in HTTP modules, most developers stick with Axios for its flexibility and ease of use.
Here's how to configure an Axios proxy for HTTP, HTTPS, and SOCKS5 connections to improve your success rates.
What is Axios?
Axios is a promise-based HTTP client for Node.js and browsers. When using a proxy, Axios routes your request to an intermediary server, then the proxy connects to the target website on your behalf, downloads the data, and returns it to your application.
Even though Node.js now includes the Fetch API natively, developers still prefer Axios since it’s a more convenient option. It automatically parses JSON responses, provides built-in interceptors to modify requests, and offers a simpler configuration for network timeouts compared to native Fetch.
How to set up a proxy in Axios
First, set up a Node.js project and run npm install axios. To route your traffic through a proxy, pass a proxy configuration object directly into your request options, which typically takes 3 properties: host, port, and protocol (which defaults to HTTP). If you use a premium proxy, you’ll also need an auth property.
You can define the proxy configuration directly in your request options based on the required proxy protocol. The native Axios proxy object handles both HTTP and HTTPS targets, so the same proxy configuration works whichever proxy protocol your endpoint uses.
const axios = require('axios');
async function checkIp() {
try {
const response = await axios.get('https://api.ipify.org?format=json', {
proxy: {
protocol: 'http',
host: 'ultra.marsproxies.com',
port: 44443,
}
});
console.log(response.data);
} catch (error) {
console.error(error.message);
}
}
checkIp();
When you run this script, the target server will see the proxy IP instead of your local device. If the logged IP matches your proxy server, your setup is working correctly.
How to use an authenticated proxy with Axios
Public proxies are often slow, unreliable, insecure, and rarely worth the time compared to premium residential proxies. Most developers rely on paid proxy providers, which requires proxy authentication to access their networks. To authenticate in Axios, add an auth object containing your username and password to the proxy configuration.
Just like the previous example, we are using an HTTP target URL in our proxy configuration because native Axios struggles to proxy HTTPS endpoints.
const axios = require('axios');
async function checkAuthenticatedIp() {
try {
const response = await axios.get('https://api.ipify.org?format=json', {
proxy: {
protocol: 'http',
host: 'ultra.marsproxies.com',
port: 44443,
auth: {
username: 'your_marsproxies_username',
password: 'your_marsproxies_password'
}
}
});
console.log(response.data);
} catch (error) {
console.error(error.message);
}
}
checkAuthenticatedIp();
If you misconfigure or omit these credentials in your proxy configuration, the proxy server will return an HTTP 407 Proxy Authentication Required error.
How to rotate proxies with Axios
Sending too many requests from a single IP address will quickly get your scraper blocked. To stay within these rate limits during web scraping, you need to implement proxy rotation using a proxy pool so each request appears to come from a different IP.
If you’re managing a proxy pool of static proxies, you can distribute your traffic by writing a proxy rotation function that cycles through multiple proxies.
Keep in mind that if you use premium residential proxies, your proxy provider usually handles the rotation automatically on their end via a single gateway endpoint, which makes custom proxy configuration unnecessary.
Here’s how you can manually rotate multiple proxies in Axios.
const axios = require('axios');
const proxyList = [
{ host: 'ultra.marsproxies.com', port: 44443, auth: { username: 'user_session-aaaaaaaa', password: 'pass' } },
{ host: 'ultra.marsproxies.com', port: 44443, auth: { username: 'user_session-bbbbbbbb', password: 'pass' } },
{ host: 'ultra.marsproxies.com', port: 44443, auth: { username: 'user_session-cccccccc', password: 'pass' } }
];
let currentIndex = 0;
function getNextProxy() {
const proxy = proxyList[currentIndex];
currentIndex = (currentIndex + 1) % proxyList.length;
return proxy;
}
async function scrapeWithRotation(urls) {
for (const url of urls) {
try {
const response = await axios.get(url, { proxy: getNextProxy() });
console.log(url, response.status);
} catch (error) {
console.error(url, error.message);
}
}
}
The function above cycles sequentially, spreading requests evenly and keeping the order predictable when you need to debug a failed batch. Random selection is the alternative, which can be performed with randomization functions.
function getRandomProxy() {
return proxyList[Math.floor(Math.random() * proxyList.length)];
}
How to use SOCKS5 proxies with Axios
Unlike HTTP proxies that read and rewrite your headers, SOCKS5 operates at a lower network layer and routes traffic without inspecting the contents.
Because Axios lacks native SOCKS5 support, you need an external package like socks-proxy-agent. To use it, instantiate the agent with your proxy URL and pass it into the httpAgent and httpsAgent properties of your request.
const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');
async function testSocks() {
const agent = new SocksProxyAgent(
'socks5h://your_marsproxies_username:[email protected]:44445'
);
try {
const response = await axios.get('https://api.ipify.org?format=json', {
httpAgent: agent,
httpsAgent: agent
});
console.log(response.data);
} catch (error) {
console.error(error.message);
}
}
testSocks();
SOCKS5 is a great choice when you need to work through strict firewalls or ensure the proxy leaves your HTTP packet headers completely untouched.
Using HTTP_PROXY and HTTPS_PROXY with Axios
Many developers keep their configuration separate from their code to switch environments quickly. Axios automatically reads the standard HTTP_PROXY and HTTPS_PROXY environment variables if you set them in your terminal before running your script.
export HTTP_PROXY=http://user:pass@ultra.marsproxies.com:44443
export HTTPS_PROXY=http://user:pass@ultra.marsproxies.com:44443
node scraper.js
By exporting these variables, Axios routes your HTTP requests through the proxy without needing a dedicated proxy configuration object in your JavaScript file.
Just remember that Axios only reads these variables in Node.js, and that setting proxy: false on a request tells Axios to ignore them. You can also define a NO_PROXY variable listing domains that should skip the proxy server entirely.
Common Axios proxy errors
When debugging proxy connections in Axios, you will likely encounter a few common errors:
- HTTP 407 Proxy Authentication Required. The proxy server rejected your request because your credentials are incorrect or your subscription expired.
- ECONNREFUSED. The proxy rejected your connection, which usually happens if you specify the wrong port or a local firewall blocks access.
- ETIMEDOUT. The proxy server failed to respond entirely, which indicates the host is offline or the IP address is wrong.
- SSL Protocol Errors. If your proxy protocol is set to https when the proxy server only accepts http, Axios will fail the TLS handshake and throw a protocol error. Check the proxy protocol your proxy provider documents before changing it.
Best practices for using Axios proxies
Never hardcode your proxy passwords in your source code. Instead, use environment variables (often managed with a package like dotenv) to load sensitive credentials safely.
Because network requests during web scraping often fail, you should set strict timeouts and implement retry logic using an external package like axios-retry to automatically handle transient network errors. If you’re running a large web scraping project, make sure you manage your proxy pool effectively and adjust your proxy configuration so no single IP triggers a rate limit.
Remember that proxies only replace your IP address, which means they don’t encrypt your payload. If you’re working with sensitive information, you’ll need to route your traffic to HTTPS endpoints so TLS can encrypt the data before it passes through the proxy server.
Frequently asked questions
Are people still using Axios in 2026?
After the Axios supply chain attack on March 31, 2026, when compromised versions 1.14.1 and 0.30.4 were briefly published to npm, some teams moved to the native Node.js Fetch API to reduce reliance on third-party packages.
Can Axios use SOCKS5 proxies?
Axios only supports HTTP(S) proxies natively. To route your traffic through a SOCKS5 proxy, you must install an external library like socks-proxy-agent and update your proxy configuration.
How do I check if Axios is using my proxy?
Send a GET request to a plain HTTP IP-checking service, such as http://api.ipify.org. If the returned IP matches your proxy server instead of your local device, your setup is working.
Can I use residential proxies with Axios?
Yes, but you’ll need more than just the host and port. Because residential proxies are paid services, you must also include your username and password in the auth property of your proxy configuration. Alternatively, you can also use IP whitelisting for authorization.