DEV Community

Cover image for Integrating Proxies with Axios
Kev the bur
Kev the bur

Posted on

Integrating Proxies with Axios

How to Use Proxies with Axios: A Practical Guide

When working with HTTP clients in JavaScript, Axios is often the go-to library for making requests. But what if you want to route your traffic through a proxy? Whether for privacy, testing, or bypassing geo-restrictions, setting up proxies with Axios can be straightforward with the right approach.

In this article, we'll walk through how to integrate proxies into your Axios setup, including how to use residential proxies from providers like DataImpulse to enhance your requests.

Integrating Proxies with Axios image 1

Installing Axios

First things first — if you haven’t installed Axios yet, you can easily add it to your project using npm or your preferred package manager:

npm install axios
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can include Axios directly via CDN in a browser environment:

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

For alternative installation methods or more details, check out the official Axios documentation.

Setting Up Proxies with Axios

Axios supports proxy configuration out of the box, which allows you to specify a proxy server for your HTTP requests.

Here’s a simple example that demonstrates how you can define a proxy inside an Axios request:

const axios = require('axios');

axios({
  url: 'https://example.com',
  proxy: {
    protocol: 'http',
    host: '<host_address>',
    port: <port_number>
  }
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});
Enter fullscreen mode Exit fullscreen mode

In this snippet:

  • protocol specifies the proxy protocol (http or https).
  • host refers to the address of the proxy server.
  • port is the port number the proxy server listens on.

This basic configuration directs Axios to send the request through the proxy server.

Using Residential Proxies with Axios and DataImpulse

If you need higher reliability and better IP rotation, residential proxies are a great choice. Providers like DataImpulse offer robust proxy plans that work seamlessly with Axios.

To configure Axios to use DataImpulse’s residential proxies, add authentication details to the proxy configuration. Here's how:

const axios = require('axios');

axios({
  url: 'https://example.com',
  proxy: {
    protocol: 'http',
    host: 'gw.dataimpulse.com',
    port: 823,
    auth: {
      username: 'your_proxy_plan_username',
      password: 'your_proxy_plan_password'
    }
  }
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});
Enter fullscreen mode Exit fullscreen mode

Note that DataImpulse requires credentials to access their proxies. Replace "your_proxy_plan_username" and "your_proxy_plan_password" with your actual proxy plan details.

Testing Your Proxy Connection

Sometimes, proxy integration requires extra setup, especially with HTTPS agents. The https-proxy-agent package can help create a custom HTTPS agent that Axios uses for proxying.

Here’s an example to test your proxy connection with DataImpulse residential proxies:

const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');

const username = 'your_login_here';
const password = 'your_password_here';

const proxyAgent = new HttpsProxyAgent(`http://${username}:${password}@gw.dataimpulse.com:823/`);

axios.get('https://ip-api.com/', { httpsAgent: proxyAgent })
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });
Enter fullscreen mode Exit fullscreen mode

This code snippet:

  • Creates an HTTPS proxy agent with your DataImpulse credentials.
  • Sends a GET request to a public IP API service to verify the outgoing IP.
  • Logs the result or any errors that occur.

Make sure to substitute your actual proxy username and password before running.

Integrating Proxies with Axios image 5

Why Use DataImpulse Proxies?

DataImpulse offers reliable residential proxies that support a wide range of use cases including web scraping, SEO monitoring, ad verification, and more. Their proxy infrastructure is designed to:

  • Provide fresh IP addresses with high anonymity.
  • Support HTTP and HTTPS protocols.
  • Scale easily based on your bandwidth needs.

If you want to try out DataImpulse’s proxies for your axios projects, you can get started with plans as low as $1 per GB.

DataImpulse

Integrating Proxies with Axios image 6

Wrapping Up

Proxies can be a vital tool for developers needing to route HTTP requests through intermediary servers. With Axios, proxy integration is simple once you know how to configure it. Whether you’re using a basic HTTP proxy or a residential proxy from DataImpulse, setting up and testing your proxy with Axios is well within reach.

Give it a try, and enjoy the added flexibility and privacy proxies can offer your HTTP requests.


Images Credits: DataImpulse

Integrating Proxies with Axios image 2

Integrating Proxies with Axios image 3

Integrating Proxies with Axios image 4

Top comments (0)