How to Use Authenticated SOCKS5 Proxy with Axios

Axios is a popular promise-based HTTP client for Node.js, but it does not support SOCKS5 proxies natively. To route Axios traffic through an authenticated SOCKS5 proxy, you must use a dedicated agent such as socks-proxy-agent. This guide provides a straightforward implementation for installing the necessary dependencies, formatting authenticated proxy credentials, creating a custom agent, and attaching it to Axios requests.


Step 1: Install Dependencies

In your Node.js project, install both axios and socks-proxy-agent:

npm install axios socks-proxy-agent

Step 2: Construct the Proxy URL

An authenticated SOCKS5 proxy URL follows this structure:

socks5://username:password@host:port

Note: If you want DNS resolution to occur on the proxy server rather than locally (recommended for anonymity), use the socks5h:// protocol prefix instead of socks5://.

Step 3: Implement the SOCKS5 Agent in Axios

Import SocksProxyAgent and pass it to the httpAgent and httpsAgent configurations in Axios.

Example: Single Request

const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');

const proxyHost = '127.0.0.1';
const proxyPort = '1080';
const proxyUser = 'your_username';
const proxyPass = 'your_password';

// Build the authenticated SOCKS5 URI
const proxyUrl = `socks5h://${proxyUser}:${proxyPass}@${proxyHost}:${proxyPort}`;
const agent = new SocksProxyAgent(proxyUrl);

async function makeRequest() {
  try {
    const response = await axios.get('https://api.ipify.org?format=json', {
      httpAgent: agent,
      httpsAgent: agent
    });
    console.log('Response data:', response.data);
  } catch (error) {
    console.error('Request failed:', error.message);
  }
}

makeRequest();

Example: Reusable Axios Instance

If your application makes multiple requests, create a configured Axios instance to reuse the agent across all calls:

const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');

const proxyUrl = 'socks5h://user:pass@proxy.example.com:1080';
const agent = new SocksProxyAgent(proxyUrl);

const apiClient = axios.create({
  httpAgent: agent,
  httpsAgent: agent,
  timeout: 10000 // 10-second timeout
});

async function fetchUserData() {
  try {
    const response = await apiClient.get('https://httpbin.org/ip');
    console.log('Origin IP:', response.data.origin);
  } catch (error) {
    console.error('Error:', error.message);
  }
}

fetchUserData();

Important Considerations

  1. Special Characters in Credentials: If your username or password contains special characters (like @, :, or /), encode them using encodeURIComponent() to avoid malformed URL errors.

    const user = encodeURIComponent('user@domain');
    const pass = encodeURIComponent('p@ss:word');
    const proxyUrl = `socks5h://${user}:${pass}@${host}:${port}`;
  2. Error Handling: Network errors occurring before reaching the destination host typically indicate proxy connection issues or invalid authentication credentials. Ensure proper try/catch blocks are in place to handle timeout and connection refused errors.