Configure SOCKS5 and HTTP proxies with popular Node.js HTTP clients. From Axios to native fetch, get your JavaScript apps running through mobile IPs.
// npm install axios socks-proxy-agent
const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');
const agent = new SocksProxyAgent(
'socks5://your_username:your_password@proxy.proxies.sx:10001'
);
const response = await axios.get('https://httpbin.org/ip', {
httpAgent: agent,
httpsAgent: agent
});
console.log(response.data);// npm install axios
const axios = require('axios');
const instance = axios.create({
proxy: {
protocol: 'http',
host: 'proxy.proxies.sx',
port: 10001,
auth: {
username: 'your_username',
password: 'your_password'
}
}
});
const response = await instance.get('https://httpbin.org/ip');
console.log(response.data);// npm install node-fetch socks-proxy-agent
const fetch = require('node-fetch');
const { SocksProxyAgent } = require('socks-proxy-agent');
const agent = new SocksProxyAgent(
'socks5://your_username:your_password@proxy.proxies.sx:10001'
);
const response = await fetch('https://httpbin.org/ip', { agent });
const data = await response.json();
console.log(data);// npm install got socks-proxy-agent
const got = require('got');
const { SocksProxyAgent } = require('socks-proxy-agent');
const agent = new SocksProxyAgent(
'socks5://your_username:your_password@proxy.proxies.sx:10001'
);
const response = await got('https://httpbin.org/ip', {
agent: {
http: agent,
https: agent
}
}).json();
console.log(response);// npm install axios https-proxy-agent
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const agent = new HttpsProxyAgent(
'http://your_username:your_password@proxy.proxies.sx:10001'
);
const response = await axios.get('https://httpbin.org/ip', {
httpsAgent: agent
});
console.log(response.data);import axios, { AxiosInstance } from 'axios';
import { SocksProxyAgent } from 'socks-proxy-agent';
interface ProxyConfig {
host: string;
port: number;
username: string;
password: string;
}
function createProxiedClient(proxy: ProxyConfig): AxiosInstance {
const agent = new SocksProxyAgent(
`socks5://${proxy.username}:${proxy.password}@${proxy.host}:${proxy.port}`
);
return axios.create({
httpAgent: agent,
httpsAgent: agent,
timeout: 30000
});
}
const client = createProxiedClient({
host: 'proxy.proxies.sx',
port: 10001,
username: 'your_username',
password: 'your_password'
});
const { data } = await client.get<{ origin: string }>('https://httpbin.org/ip');
console.log(data.origin);const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');
class ProxyRotator {
constructor(ports, username, password) {
this.ports = ports;
this.username = username;
this.password = password;
this.currentIndex = 0;
}
getNextAgent() {
const port = this.ports[this.currentIndex];
this.currentIndex = (this.currentIndex + 1) % this.ports.length;
return new SocksProxyAgent(
`socks5://${this.username}:${this.password}@proxy.proxies.sx:${port}`
);
}
async get(url) {
const agent = this.getNextAgent();
return axios.get(url, { httpAgent: agent, httpsAgent: agent });
}
}
const rotator = new ProxyRotator(
[10001, 10002, 10003, 10004, 10005],
'your_username',
'your_password'
);
// Each request uses a different proxy
for (let i = 0; i < 5; i++) {
const { data } = await rotator.get('https://httpbin.org/ip');
console.log(`Request ${i + 1}:`, data.origin);
}socks-proxy-agent and https-proxy-agent work with any HTTP client. More flexible than library-specific proxy options.
Create agents once and reuse them. Creating new agents for each request adds unnecessary overhead.
Proxy connections can fail. Use try/catch and implement retry logic with proxy rotation for reliability.
Always configure timeouts. Default is often too long or infinite. 30 seconds is a reasonable starting point.
Get mobile proxies for reliable HTTP requests.