How to Use Proxy With Axios in 2024: Setup, Proxy Types, Errors

July 31, 2024 · 8 min read

Axios is a powerful JavaScript library for making HTTP requests during web development and scraping. However, many websites block bot traffic and may hinder your Axios web scraper

Fortunately, combining Axios with a proxy to route your requests can help prevent website detection.

This tutorial will show you how to do that using real-world examples. We'll explore free and paid proxies and some handy methods to avoid getting blocked.

How to Use Proxies With Axios

In this section, we'll walk through four ways to set up proxies with Axios, starting with basic setup and proceeding to instance-based setup, proxy authentication, and an environment-level approach involving environment variables. 

In each case, you'll request https://httpbin.io/ip, a test website that returns the IP address of the client making the request.

This tutorial assumes you've set up a Node.js environment on your machine. Otherwise, check our article on getting started with Node.js web scraping.

Let's jump right in!

1. Basic Requests and Adding Proxies

We'll select a free proxy from the Free Proxy List to show you the basic Axios proxy setup.

Here's the selected proxy in this case:

Example
IP: '109.120.156.109',
port: 80

Import Axios and add your proxy details to the request parameter. Specify the host, port, and protocol inside a proxy parameter. Then, log the response data to view your current IP address. We've used response.data to view the IP in JSON format since the test website returns a JSON:

Example
// npm i axios
const axios = require('axios');

axios
    // send request with the proxy
    .get('https://httpbin.io/ip', {
        proxy: {
            host: '109.120.156.109',
            port: 80,
            protocol: 'http',
        },
    })
    .then((response) => {
        // log the response data
        console.log(response.data);
    })
    .catch((error) => {
        // log request error if any
        console.error('Error:', error);
    });

The above code outputs the proxy's IP as shown:

Output
{ origin: '109.120.156.109:39490' }

Cool! You now know how to set up a proxy with a basic Axios request. 

What if you want to use the same proxy for multiple Axios instances? We'll explain in the next section.

2. Set Up a Proxy for Different Axios Instances

Setting up a single proxy for different Axios instances is handy when sharing the same proxy between different request types (GET, POST, UPDATE, or DELETE).

To achieve that, create a new Axios instance with the desired proxy configuration:

Example
// ...

// set an Axios instance that you can use globally
const instance = axios.create({
    proxy: {
        host: '109.120.156.109',
        port: 80,
        protocol: 'http',
    },
});

Send your requests using the Axios instance and log the response data:

Example
// ...

// use the instance for your requests
instance
    .get('https://httpbin.io/ip')
    .then((response) => {
        // log the response data
        console.log(response.data);
    })
    .catch((error) => {
        // log request error if any
        console.error('Error:', error);
    });

Join the two snippets above. Here's the following final code:

Example
// import Axios
const axios = require('axios');

// set an Axios instance that you can use globally
const instance = axios.create({
    proxy: {
        host: '109.120.156.109',
        port: 80,
        protocol: 'http',
    },
});

// use the instance for your requests
instance
    .get('https://httpbin.io/ip')
    .then((response) => {
        // log the response data
        console.log(response.data);
    })
    .catch((error) => {
        // log request error if any
        console.error('Error:', error);
    });

The code outputs the proxy's IP address as expected:

Output
{ origin: '105.113.26.29:5756' }

That's it! Your scraper now uses the same proxy address for multiple Axios request instances. In the next section, you'll see how to handle proxies with authentication.

3. Authenticating Proxies With Axios: Username & Password

Paid proxies often require authentication with a username and password. Axios offers an auth property that lets you pass them to your proxy server.

Example
auth: {
    username: '<YOUR_USERNAME>';
    password: '<YOUR_PASSWORD>';
}

Here's how to add the property to your script:

Example
const axios = require('axios');

axios
    .get('https://httpbin.org/ip', {
        proxy: {
            protocol: '<PROXY_PROTOCOL>',
            host: '<PROXY_DOMAIN>',
            port: '<PROXY_PORT>',
            auth: {
                username: '<YOUR_USERNAME>',
                password: '<YOUR_PASSWORD>',
            },
        },
    })
    .then((res) => {
        console.log(res.data);
    })
    .catch((err) => console.error(err));

The above script will use your proxy detail. Ensure you enter all the proxy parameters correctly.

4. Setting Up Environment Variables for an Axios Proxy

Environmental variables are dynamic values that software programs can access to configure their behavior or obtain system environment data. You can change them without modifying the code.

Axios allows you to specify environment-level variables such as proxy settings, authentication credentials, and other configuration options. This approach lets you automatically access the proxy details without passing them into the Axios request.

To do that, define your environment variables' proxy settings and credentials before sending the Axios request. Let's see an example.

Open the command line to your project root folder and add environment variables using the proxy server details with the set command. For Linux and Mac, use export instead. Ensure you specify both HTTP protocols (HTTP and HTTPS) for the proxies:

Example
set HTTP_PROXY=http://109.120.156.109:80
set HTTPS_PROXY=https://109.120.156.109:80

You don't need to include the proxy in your Axios request anymore since it now automatically uses the proxy server. 

Your code should look like this:

Example
// import Axios
const axios = require('axios');

axios
    // send request with the proxy
    .get('https://httpbin.io/ip')
    .then((response) => {
        // log the response data
        console.log(response.data);
    })
    .catch((error) => {
        // log request error if any
        console.error('Error:', error);
    });

Running the above code outputs the following result:

Output
{ origin: '109.120.156.109:56068' }

Congratulations! You've learned a new way to configure an Axios proxy.

Frustrated that your web scrapers are blocked once and again?
ZenRows API handles rotating proxies and headless browsers for you.
Try for FREE

Use Rotating Proxies With Axios

Websites can detect and block you if you request multiple times with a single IP address. You'll need to distribute the scraping traffic across several IPs to avoid this. Rotating IPs will make your scraper more stealthy and effective.

Different approaches to creating a proxy rotator include:

  • Manually switching proxies after a specified number of requests.
  • Automating the process with an array of proxies.
  • Using a proxy pool solution.

Let's try a free solution!

Rotate IPs With a Free Solution

Rotating proxies with a free solution involves alternating your proxies from a pool of multiple free proxies to use a different IP per request. You can select several free proxies from a website like the Free Proxy List and hardcode them into an array.

Let's see how to do that. Start by defining the proxy list array:

Example
// ...

// replace with your own proxy list
const proxyList = [
    { ip: '109.120.156.109', port: '80' },
    { ip: '20.204.214.23', port: '3129' },
    { ip: '50.169.37.50', port: '80' },
    // ...
];

Create a function that rotates through the proxy list. For each request, it shifts to the next proxy while returning the current proxy to the end of the queue.

Example
// ...

// function to rotate through the list of proxies
const rotateProxy = () => {
    // get the next available proxy
    const proxy = proxyList.shift();
    // add the current proxy back to the end of the list
    proxyList.push(proxy);
    return {
        protocol: 'http',
        host: proxy.ip,
        port: proxy.port,
    };
};

Send your Axios request using the rotateProxy() function as the proxy option:

Example
// ...

axios
    // send request with the proxy
    .get('https://httpbin.io/ip', {
        proxy: rotateProxy(),
    })
    .then((response) => {
        // log the response data
        console.log(response.data);
    })
    .catch((error) => {
        // log request error if any
        console.error('Error:', error);
    });

Put the snippets together, and you'll get this final code:

Example
// import Axios
const axios = require('axios');

// replace with your own proxy list
const proxyList = [
    { ip: '109.120.156.109', port: '80' },
    { ip: '20.204.214.23', port: '3129' },
    { ip: '50.169.37.50', port: '80' },
    // ...
];

// function to rotate through the list of proxies
const rotateProxy = () => {
    // get the next available proxy
    const proxy = proxyList.shift();
    // add the current proxy back to the end of the list
    proxyList.push(proxy);
    return {
        protocol: 'http',
        host: proxy.ip,
        port: proxy.port,
    };
};

axios
    // send request with the proxy
    .get('https://httpbin.io/ip', {
        proxy: rotateProxy(),
    })
    .then((response) => {
        // log the response data
        console.log(response.data);
    })
    .catch((error) => {
        // log request error if any
        console.error('Error:', error);
    });

Run the above code a few times, and you should get a different IP per request, indicating you've implemented proxy rotation into your scraper:

Output
{ origin: '109.120.156.109:56068' }
{ origin: '20.204.214.23:76543' }
{ origin: '50.169.37.50:23652' }

Awesome! You've created your first proxy rotator with Axios. 

However, as mentioned earlier, free proxies are unreliable and often fail. We only used them in this example to show you the basics. For instance, using a free proxy to send multiple requests to a website like Amazon will eventually get you blocked.

So, what's the solution? Let's find out.

Premium Proxy to Avoid Getting Blocked

Free proxies often provide weak IPs that can be easily banned. Although paid, premium proxies are a more reliable way to avoid being blocked.

You should use a premium proxy with IP auto-rotation and geo-targeting features to boost anti-bot bypass capability, such as ZenRows. Besides premium proxy service, ZenRows also offers a full-scale web scraping API featuring all the tools to bypass any anti-bot and CAPTCHA at scale under the same price cap.

Let's quickly see how to use ZenRows' residential proxy feature. 

Sign up and go to the ZenRows Proxy Generator. Grab your proxy credentials (username, password, proxy domain, and proxy port). Then, add them to your code.

generate residential proxies with zenrows
Click to open the image in full screen

Premium proxies usually require authentication with a password and a username. Since you've seen how to set proxies with authentication earlier in this tutorial, implementing ZenRows proxies into your scraper should be straightforward:

Example
// import Axios
const axios = require('axios');
axios
    .get('https://httpbin.org/ip', {
        proxy: {
            protocol: 'http',
            host: '<ZENROWS_PROXY_DOMAIN>',
            port: '<ZENROWS_PROXY_PORT>',
            auth: {
                username: '<ZENROWS_PROXY_USERNAME>',
                password: '<ZENROWS_PROXY_PASSWORD>',
            },
        },
    })
    .then((res) => {
        console.log(res.data);
    })
    .catch((err) => console.error(err));

Congratulations! Your scraper now uses high-quality proxies that are less likely to get blocked.

Axios Proxy Not Working? Make Sure You Check These:

You might face some errors while setting up proxies with Axios. Implementing one or a combination of the following solutions will help you resolve it.

1. Proxy Configuration

Check your proxy configuration for missing parameters or typos. Ensure you separate the parameters correctly. The configuration object should specify the host, port, and protocol separately. If using a premium proxy, handle authentication inside the auth parameter.

Common mistakes include using http://<proxy.example.com> as the host when it should be just <proxy.example.com> or putting the full URL in a single string instead of separating it into host, port, and protocol.

2. Global Environment Variable Setting

Ensure your environment variables are correct and don't conflict with your code-level settings. When using environment variables, specify HTTP and HTTPS proxy URLs, as done earlier in this article. 

Remember that some systems are case-sensitive, so check for typos in variable names. Ideally, you should set your proxy variables before running your Node.js script. Then, ensure you configure the proxy variable inside a command-line instance of your scraping project.

3. Axios Version

You're also likely to encounter errors if your Axios version is outdated. Ensure you're using a recent version of Axios that supports your proxy configuration. Older versions may have bugs or lack features for specific proxy setups. To ensure you install the latest version, run the following installation command:

Terminal
npm install axios@latest

4. Authentication Credentials

If your proxy requires authentication, double-check your credentials. Ensure you're not exposing sensitive information in your code. Use environment variables for credentials when possible. 

5. Proxy Server Status

If the service fails to connect, verify that your proxy server is operational. You can do this by attempting to ping it directly using a tool like cURL. For example:

Terminal
curl -v -x http://<PROXY_IP_ADDRESS>:<PROXY_PORT> https://httpbin.io/ip

6. VPNs

VPNs can interfere with proxy settings by routing all traffic through the VPN server, potentially bypassing your configured Axios proxy. If you're using a VPN, try deactivating it temporarily to see if it resolves the proxy issues.

Conclusion

Using Axios with a proxy to route your requests will prove invaluable in bypassing blocks like Cloudflare in JavaScript.

We've explored several options and seen how free solutions often fail to deliver. That's why you should consider ZenRows, which ensures your scraper effectively bypasses all blocks to scrape any website. Learn more about our Proxy Rotator.

Ready to get started?

Up to 1,000 URLs for free are waiting for you