How to Use a Proxy With Axios & Node.js

Axios is a popular open-source JavaScript library that facilitates HTTP requests from the browser and a Node.js environment. The tool features the automatic transformation of JSON data, efficient management of HTTP headers, and the ability to cancel requests conveniently. 

The following shows how to implement Oxylabs Datacenter and Residential Proxies with Axios.

axios integration with Oxylabs proxies

Installing Axios

Axios can be installed using any Javascript package manager such as npm, Yarn, Bower, etc. For example, to install it using npm, you can simply run the following command:

npm install axios

You can also install it using CDN as shown below:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

If you’re using a different package or an alternative CDN, please refer to Axios’ official documentation

Setting up proxies

Axios can be integrated with proxy servers. Let’s take a look at the code snippet below to set up an Axios proxy server:

const browser = require('axios')

browser.get('https://example.com',

   {

       proxy: {

           protocol: 'http',

           host: '<host_address>',

           port: <port_number>

       }

   }

).then(result => {

       console.log(result.data)

   }).catch(error => console.error(error))

The code is pretty straightforward – first, we instantiate a browser object using the Axios library. Then, we’re making a GET request to the website: example.com. Notice, when we’re making the GET request, we’re also passing a dictionary with the proxy information. Inside the dictionary, we are passing the protocol, host, and port to properly identify which proxy to use.

Residential Proxies

To use Oxylabs' Residential Proxies, we’ll have to slightly modify the above code, updating the information as shown below:

proxy: {

           protocol: 'http, https, or socks5',

           host: 'pr.oxylabs.io',

           port: 7777,

           username: 'USERNAME',

           password: 'PASSWORD',

       }

The host address is now set to pr.oxylabs.io, the port is set to 7777, and the protocol is either HTTP or HTTPS. Also, you’ll have to update the credentials with your own – you can set up your proxy user's credentials in our dashboard.  

Datacenter Proxies

If you’re using Datacenter Proxies, fill in the information as shown below: 

proxy: {

           protocol: 'http, https, or socks5',

           host: 'dc.oxylabs.io',

           port: 8001,

           username: 'USERNAME',

           password: 'PASSWORD',

       }

Enterprise Dedicated Datacenter Proxies

If you’re using Enterprise Dedicated Datacenter Proxies purchased through the sales team, you have to choose the protocol of your acquired proxies. Insert your unique IP address in the host field, and enter 60000 in the port field:

proxy: {

           protocol: 'http or socks5',

           host: '1.2.3.4',

           port: 60000,

           username: 'USERNAME',

           password: 'PASSWORD',

       }

Self-Service Dedicated Datacenter Proxies

When it comes to Self-Service Dedicated Datacenter Proxies purchased via the Oxylabs’ dashboard, you have to use the port number that corresponds with the sequential number of the IP address from the acquired list. For example, port 8001 will use the first proxy IP from your list:

proxy: {

           protocol: 'http, https, or socks5',

           host: 'ddc.oxylabs.io',

           port: 8001,

           username: 'USERNAME',

           password: 'PASSWORD',

       }

ISP Proxies

proxy: {

           protocol: 'http, https, or socks5',

           host: 'isp.oxylabs.io',

           port: 8001,

           username: 'USERNAME',

           password: 'PASSWORD',

       }

Note: For additional Oxylabs’ proxy customization options – such as country-specific entry points – refer to Shared Datacenter, Enterprise Dedicated Datacenter, Self-Service Dedicated Datacenter, and Residential Proxies documentations, respectively.

Testing proxy connection

Finally, let’s put the proxy integration to the test and see if the connection was successful. You can navigate to the ip.oxylabs.io website and see if your original IP address was changed with that of a proxy server. Check out the code given below:

const browser = require('axios')

browser.get('https://ip.oxylabs.io',

   {

       proxy: {

           protocol: 'http',

           host: 'pr.oxylabs.io',

           Port: 7777,

           username: 'USERNAME',

           password: 'PASSWORD',

       }

   }

).then(result => {

       console.log(result.data)

   }).catch(error => console.error(error))

The code above showcases the connection to a Residential Proxy. 

Note: make sure to update the code using your own credentials and proxy server information. If you have a cURL command that uses Oxylabs proxies, use this cURL to Node Axios converter to quickly create a working Axios code for integrating your purchased proxies.

Conclusion

Robust Oxylabs’ proxies allow developers to use Axios with enhanced anonymity and security, while protecting them from IP blocks. 

If you have any questions regarding an Axios proxy server from Oxylabs, don’t hesitate to get in touch with our team for more information.

Please be aware that this is a third-party tool not owned or controlled by Oxylabs. Each third-party provider is responsible for its own software and services. Consequently, Oxylabs will have no liability or responsibility to you regarding those services. Please carefully review the third party's policies and practices and/or conduct due diligence before accessing or using third-party services.

Get the latest news from data gathering world

I'm interested