How to Use a Proxy With Axios and Node.js
Tutorials

Vilius Dumcius
Key Takeaways
-
Integrating an Axios proxy into your scraping scripts is essential because it effectively masks your real IP address and prevents you from being blacklisted by the websites you're trying to scrape.
-
You'll find that using reliable proxy servers with strong proxy authentication provides the necessary security and uptime for professional web scraping operations.
-
Managing a diverse proxy pool with rotating proxies ensures that your requests remain undetected by distributing traffic across a vast range of different IP addresses.
Axios is one of the most commonly used Node.js libraries for web scraping. It enables developers to download the contents of websites, which can later be parsed with a library like Cheerio .
By default, your real IP address is visible to the destination server when making requests with Axios. It can lead to your IP being flagged or permanently banned by the target website’s security systems.
To avoid that, web scrapers use proxies that act as middlemen between the client and the server. They help you hide your scraping activities and protect your IP address.
We’ll show you how to configure Axios with a proxy agent to ensure reliable connections when scraping with Node.js.
How to Use Proxies in Node.js
Getting your environment ready is the first step toward building a resilient scraper that can handle HTTP proxies.
Setup
First, ensure that Node.js is installed and your proxy configuration is ready. If you don’t, you can use the official instructions to download and install it.
After that, install Axios and Cheerio with the following terminal command. Axios will be used for making web requests, while Cheerio will be used for the web scraping script example.
npm i axios cheerio
Then, create a new folder called axios_proxy and move inside that folder. Run the npm init command to create a new Node.js project. Finally, create a file called index.js and open it in your code editor.
Basic HTTP Request With Axios
Here's how a basic proxy or HTTP request made with Axios looks.
const axios = require('axios')
const cheerio = require('cheerio');
axios.get('https://quotes.toscrape.com/')
.then((r) => {
console.log(r.data)
})
All the functions above do is request the content of a web page (in this case, it's Quotes to Scrape ) and print it out.
If you use a library like Cheerio, you can parse the request to extract the necessary information.
const axios = require('axios');
const cheerio = require('cheerio');
axios.get('https://quotes.toscrape.com/')
.then((response) => {
const $ = cheerio.load(response.data);
const quote_blocks = $('.quote');
const quotes = quote_blocks.map((_, quote_block) => {
const text = $(quote_block).find('.text').text();
const author = $(quote_block).find('.author').text();
return { text, author };
}).toArray();
console.log(quotes);
})
.catch(err => {
console.error("Scraping failed:", err.message);
});
In the case of Quotes to Scrape, the website is made for scraping. Websites like Amazon use advanced detection to trigger CAPTCHAs if they see high-volume traffic from a single IP or suspicious browser headers. For this reason, it's useful to add a proxy to your Axios requests.
Using a Proxy With Axios
To use a proxy server with Axios, you need to create a new variable that holds the value for the proxy protocol, IP address, and port of the proxy that you want to connect to.
Here's an example:
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const proxyUrl = 'http://176.193.113.206:8989';
const agent = new HttpsProxyAgent(proxyUrl);
axios.get('https://quotes.toscrape.com/', {
httpsAgent: agent,
proxy: false
})
.then(res => console.log('Response through proxy:', res.status));
Requests will now be funneled through the proxy that you provided.
Set a Proxy Globally in Axios
If you're planning to make dozens of different calls, you probably don't want to pass a proxy object into every single request manually. Axios allows you to create a custom instance where you can define a proxy agent once, ensuring it applies to every request made through that instance.
It keeps your code cleaner and ensures that, as long as you use the custom instance, you won't accidentally leak your real IP on secondary requests. It’s particularly useful when you’re building larger applications that require consistent HTTP proxies across different modules.
Using SOCKS5 Proxy With Axios
Axios doesn’t support SOCKS proxies natively, so you must use a specialized library like socks-proxy-agent to bridge the connection. It comes in handy if your proxy configuration involves a SOCKS proxy server or SOCKS proxy credentials.
First, install the required package:
npm install socks-proxy-agent
Then use it like this:
const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');
const agent = new SocksProxyAgent('socks5://127.0.0.1:9050');
axios.get('https://quotes.toscrape.com/', {
httpAgent: agent,
httpsAgent: agent
}).then((res) => {
console.log(res.data);
}).catch((error) => {
console.error('Error occurred:', error.message);
console.error('Error code:', error.code);
if (error.response) {
console.error('Response status:', error.response.status);
}
});
This setup routes the specific HTTP request through the SOCKS5 proxy agent. It’s a great way to use a SOCKS proxy or a SOCKS proxy server for privacy or bypassing blocks.
Note that the code uses 127.0.0.1, which is your local device’s address. You'll need to replace it with the actual IP of your SOCKS5 server.
How to Find a Proxy?
If you're new to scraping, you probably don't have access to a proxy.
There are two ways to find a proxy to connect to. You can either scour the internet for lists of free proxy services or pay for access to a professional proxy server.
In the first case, these free proxies you will find are likely to be slow, unsafe, unreliable, and they will also provide you with only one IP address. These issues can be addressed, but it takes quite a bit of time and expertise.
In the second case, the proxy service provider will provide you with a secure endpoint to connect to. It will also provide proxy rotation by default. This means that it can change your IP address on every request, masking both your IP address and the fact that any scraping is being done on the page at all!
In addition, since there is a great amount of competition between proxy services, good, reliable services can be gotten for a rather small price - around $7 per GB.
If you’re seeking a reliable proxy provider that offers hard-to-detect proxies that are ethically sourced from real devices worldwide, take a look at IPRoyal residential proxies . They are easy to set up and use, and the next section will provide an example of how you can use them with Axios.
Axios Proxy Authentication Example
Proxies provided by professional services usually require authentication. In addition to all the usual values used to define a proxy, you need to provide a username and password to prove that you have paid for their services.
If you purchase proxy services, the provider should supply you with all the necessary information: the proxy host, port, username, and password you need to connect to the proxy. For example, if you use IPRoyal residential proxies, you can find the necessary information in your dashboard.
Now, you can put all the information provided into a proxy variable. Copy the code below and fill out the residential proxy host, port, username, and password fields with the information that your provider has supplied.
proxy = {
protocol: 'http',
host: 'geo.iproyal.com',
port: 12321,
auth: {
username: 'cool username',
password: 'cool password'
}
}
After that, you can use the proxy as an argument in your axios.get() calls.
axios.get('https://quotes.toscrape.com/')
.then((r) => {
…
})
Here's an example of how a small web scraping script using an authenticated proxy and Axios might look:
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const proxyUrl = 'http://username:[email protected]:12321';
const agent = new HttpsProxyAgent(proxyUrl);
axios.get('https://quotes.toscrape.com/', {
httpsAgent: agent,
proxy: false
})
.then((res) => {
console.log("Successfully connected through proxy! Status:", res.status);
})
.catch((err) => {
console.error("Proxy Authentication failed or connection timed out:", err.message);
});
Every request made through the authenticated proxy will go through a different proxy IP address, which will help to avoid detection in situations when you need to scrape hundreds or thousands of pages.
Using httpAgent in Axios
If you’re not using a SOCKS proxy, but still want more control over your connection configuration, you can use Node’s built-in http.Agent. It provides more control over how connections are handled and reused, particularly for advanced use cases involving HTTP requests.
Since most modern sites use HTTPS, you should typically use the https module and https.Agent to ensure your connection is secure and your proxy is respected.
Here’s how to use the http.Agent:
const axios = require('axios');
const http = require('http');
const agent = new http.Agent({
keepAlive: true,
maxSockets: 10
});
axios.get('http://quotes.toscrape.com/', {
httpAgent: agent
}).then((res) => {
console.log(res.data);
});
It doesn’t configure a proxy on its own, but it works well alongside proxy server setups, especially when handling many HTTP requests or requiring performance optimization. It’s one of those tools you don’t always need, but it helps when you do.
Proxy Option vs Agent Configuration in Axios
It’s important to understand that the proxy option is a high-level Axios proxy setting designed for ease of use. In contrast, using an Agent provides lower-level control over the underlying Node.js networking layer.
You’ll typically reach for agents when dealing with rotating proxies that require custom logic, SOCKS5 support, or complex connection reuse strategies. While the built-in proxy configuration works for simple HTTP requests, using an Agent is the standard for reliable HTTPS tunneling and professional scraping.
Setting a Proxy via Environment Variables
Storing sensitive information, such as the username and password you use for a proxy, in code is not very secure. If you accidentally share the file with another person or put it on a public GitHub repository, the credentials will be exposed.
To fix that, this information is usually stored in user-defined variables that are accessible to programs running on a computer.
Using the terminal, you can define HTTP_PROXY and HTTPS_PROXY environment variables, which include the link to your proxy, including the proxy host, port, and (optionally) authentication details.
If you're using IPRoyal residential proxies, this link is accessible in your dashboard.
Copy the link and set it as an environment variable for both HTTP and HTTPS using the following commands if you're using Windows:
set HTTP_PROXY=http://username:password@host:port
set HTTPS_PROXY=http://username:password@host:port
If you're using Linux or MacOS, you need to use the export command instead of set:
export HTTP_PROXY=http://username:password@host:port
export HTTPS_PROXY=http://username:password@host:port
If you run your Axios scraping script using this terminal, it will use the defined proxy by default. Axios automatically checks for HTTP_PROXY and HTTPS_PROXY environment variables and uses them as proxies if found.
Rotating Proxies
If you have plenty of proxy servers that you can use and you don't use a professional proxy service that provides proxy rotation by default, it's possible to create a working solution that picks and tries a random proxy from a list of possible options.
First, instead of creating one Axios proxy variable, create an array with multiple proxies.
proxies = [
{
protocol: 'http',
host: '128.172.183.18',
port: 8000
},
{
protocol: 'http',
host: '18.4.13.6',
port: 8080
},
{
protocol: 'http',
host: '65.108.34.224',
port: 8888
}
]
Then, create a function that randomly selects one of the HTTP proxies.
function get_random_proxy(proxies) {
return proxies[Math.floor((Math.random() * proxies.length))];
}
Now you can call it on the proxies array to pick a random proxy to go through every time you want to make a request.
axios.get('https://quotes.toscrape.com/', { proxy: get_random_proxy(proxies) })
.then((r) => { …
Be careful, though: this solution doesn't account for the fact that proxy servers might break or shut down. If you're using multiple proxies that are unreliable (can fail to return the requested information), you will also need to supply a retry mechanism that detects proxy failure.
How to Check if Your Axios Proxy Works
Before you start a massive scraping job, you’ve got to verify that your basic proxy configuration is working. You can do it by making a request to an IP-checking endpoint like https://api.ipify.org?format=json .
If the response shows a proxy's IP address that differs from your local device’s IP, then you’ve successfully masked your identity. Testing your proxy settings early prevents you from accidentally burning through your home IP address.
Why Should You Use Proxies?
When connecting to a server, you share the IP address from which your request comes. If this is a one-off request, it doesn't matter.
However, if you plan to request multiple pages to scrape information from the website, your IP address may be flagged by a detection system or administrator and blacklisted. This means that you won't be able to access the website until you change your IP address.
Proxies act as middlemen and forward your Axios requests to the server. The server sees the proxy’s IP as the origin; high-quality anonymous proxies ensure your real IP is never forwarded in the request headers.
They enable you to scrape large amounts of data and make many more requests than you could with a single IP address. The best of them provide a pool of IP addresses through a single, authorized endpoint, enabling you to rotate IPs on request.
Conclusion
Using proxy services is a great way to enable large-scale web scraping, since it lets you hide scraping activity from website administrators. If you're using Axios, setting up both unauthenticated and authenticated proxies for your scraping projects is quite easy.
But sometimes, a proxy pool is not enough to look natural. In these cases, you can use a browser automation tool like Puppeteer to mimic a real user visiting the site.
FAQ
AxiosError: connect ETIMEDOUT
This error means that Axios timed out while trying to connect to a remote server. Depending on the server it failed to connect to (which is listed in the error), it means that either the page you're trying to connect to or the proxy is down.
To ensure that proxies don't shut down during the scraping process, it is essential to either use a reliable proxy provider or write code for rotating proxies and retry with a different one.
AxiosError: Request failed with status code 404
This error means that Axios was able to connect to a server of your choosing, but there was no resource to serve because the URL you provided to Axios was incorrect (didn't correspond to any web page on the server).
AxiosError: Request failed with status code 407
A 407 error means Proxy Authentication is required. Ensure your credentials are correct in your proxy object or embedded in your Proxy Agent URL. It should look like this:
proxy = {
protocol: 'http',
host: 'geo.iproyal.com',
port: 12321,
auth: {
username: 'cool username',
password: 'cool password'
}
}
What is a proxy in the context of Axios?
A proxy server acts as a middleman. It takes your request, forwards it to the target site, and then sends the response back. When you use a proxy configuration with Axios, the target server sees the proxy’s IP instead of yours, which helps mask your identity if the setup is correct.
Working with an HTTPS proxy server is useful when scraping or needing to mask your location or identity.
Does Axios support HTTP and HTTPS proxies?
While Axios has a built-in proxy option, using a specialized agent is the most reliable way to handle HTTPS proxies in a Node.js environment. However, it doesn’t apply in the browser environment. You can provide the proxy host, port, and even auth in the proxy configuration. If you’re working with a SOCKS proxy setup, you’ll need an external library, such as socks-proxy-agent, to assist you.
Can I disable the proxy in Axios?
Yes. If your environment has a default proxy configuration, but you want Axios to ignore it, you can set the proxy: false option like this:
axios.get('https://iproyal.com', { proxy: false });
That’ll make the request go straight out without using any proxy server.
Does Axios proxy work in the browser?
No. Axios doesn’t support the proxy option in the browser. Browser security rules don’t allow direct low-level proxy configuration. If you need to use a SOCKS proxy or HTTPS proxy pool, you’ll need to set it up in the browser itself or at the system/network level.