How To Use a SOCKS Proxy in Axios
Tags: javascript, security, softwaredevelopment, softwareengineering
Published: 2024-01-19 21:30:24

[Axios](https://axios-http.com/) is one of the most popular HTTP clients for JavaScript thanks to its versatility, flexibility, and powerful Promise-based API. One of the great things about Axios is that it natively supports HTTP and HTTPS proxies. This allows you to route your HTTP requests through a [proxy server](https://en.wikipedia.org/wiki/Proxy_server), which can be useful for bypassing network restrictions, increasing security, or improving performance.

However, Axios does not natively support SOCKS proxies. You can still use them, but you need an external library such as [`socks-proxy-agent`](https://www.npmjs.com/package/socks-proxy-agent).

Follow this tutorial and learn how to use SOCKS proxies with Axios. But first, let's start with a bit of theory.

## What Is SOCKS Proxy?

A SOCKS proxy is a proxy server that uses the [SOCKS](https://en.wikipedia.org/wiki/SOCKS) protocol to route your data over the Internet. In detail, a SOCKS server proxies TCP connections to an arbitrary IP address. Note that SOCKS also supports authentication, so that only authorized users can access the proxy server. Also, it provides UDP forwarding.

Simply put, the SOCKS proxy acts as an intermediary between the client and the server, forwarding incoming web requests from the client to the server and forwarding the server's responses back to the client. For example, this allows the client to make requests to servers it would not otherwise be able to access due to network restrictions, such as firewalls or censorship.

### What Is the Difference Between an HTTP Proxy and a SOCKS Proxy?

The main difference between an HTTP proxy and a SOCKS proxy is the types of traffic they can handle. An HTTP proxy is specifically designed to handle HTTP traffic. On the contrary, a SOCKS proxy can handle any type of web traffic, including HTTP, HTTPS, FTP, and other protocols. Also, HTTP proxies typically operate at the application layer of the [OSI model](https://en.wikipedia.org/wiki/OSI_model), while SOCKS proxies operate at the [transport layer](https://en.wikipedia.org/wiki/Transport_layer).

So, SOCKS proxies are more versatile than HTTP proxies and can adapt to a wider range of scenarios. At the same time, they may be more complex to configure and use.

## Configuring a SOCKS Proxy in Axios

It is time to see a real-world example of how to configure a SOCKS proxy with Axios. Since the [Tor](https://www.torproject.org/) project comes with a SOCKS proxy you can access locally to route your web traffic through the Tor network, let's see how to use the Tor SOCKS proxy in Axios. Note that you can easily extend this example to any other SOCKS proxy.

First, you need to install `socks-proxy-agent`. This library allows you to connect to a SOCKS proxy in JavaScript. Add `socks-proxy-agent` to your project's dependencies with:

```shell
npm i socks-proxy-agent
```

Once you have installed `socks-proxy-agent`, you can use it to create an agent that acts as a SOCKS proxy for Axios. Here is an example of how to do it with the Tor SOCKS proxy:

```javascript
import axios from "axios"
// on Node.js const axios = require('axios')
import { SocksProxyAgent } from "socks-proxy-agent"
// on Node.js: const SocksProxyAgent = require('socks-proxy-agent')

// define the Tor SOCKS proxy agent
const torProxyAgent = new SocksProxyAgent("socks://127.0.0.1:9050");

// perform a GET request over the Tor SOCKS proxy
const response = await axios.request({
    url: "https://your-target-domain.com/",
    method: "GET",
    httpsAgent: torProxyAgent,
    httpAgent: torProxyAgent,
})
```

The `SocksProxyAgent` constructor takes the SOCKS proxy URL and returns an agent that can be used with Axios. In the case of authenticated SOCKS proxies, follow the URL syntax below:

```
"socks://<USERNAME>:<PASSWORD>@<PROXY_HOST>:<PROXY_PORT>"
```

Then, pass `torProxyAgent` to the Axios `request()` function to perform the HTTP request over the SOCKS proxy you configured. Note that you can use `torProxyAgent` also with Axios' `get()`, `post()`, `put()` and similar functions:

```javascript
const response = await axios.get("https://your-target-domain.com/", {
  httpsAgent: agent,
  httpAgent: agent,
})
```

You can also set the SOCKS proxy globally on your Axios instance with:

```javascript
// globally configure a SOCKS proxy
const axiosInstance = axios.create({
  httpsAgent: torProxyAgent,
  httpAgent: torProxyAgent
})
```

With this configuration, all requests made using the Axios instance will be routed through the Tor SOCKS proxy.

Congrats! You know now how to use a SOCKS proxy server in Axios.

## Conclusion

In this article, you learned what a SOCKS proxy is, how it differs from an HTTP proxy, and how to adopt a SOCKS proxy in Axios. In detail, you learned that Axios does not support SOCKS proxies natively. However, thanks to the `socks-proxy-agent` library you can easily configure Axios to use a SOCKS proxy. Specifically, here you saw how to use the Tor SOCKS proxy in Axios.

Thanks for reading! I hope you found this article helpful.
  
***
_The post "[How To Use a SOCKS Proxy in Axios](https://writech.run/blog/how-to-use-a-socks-proxy-in-axios-6c0355a2e013/)" appeared first on [Writech](https://writech.run)._