Skip to main content

Node.js Integration

This guide covers using WeProxies with popular Node.js HTTP libraries.

Axios

The most popular HTTP client for Node.js.

Installation

npm install axios https-proxy-agent

Basic Usage

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

const PROXY_USER = 'wp_user123';
const PROXY_PASS = 'your-password';
const PROXY_HOST = 'proxy.weproxies.com';
const PROXY_PORT = '1080';

const proxyUrl = `http://${PROXY_USER}:${PROXY_PASS}@${PROXY_HOST}:${PROXY_PORT}`;
const agent = new HttpsProxyAgent(proxyUrl);

axios.get('https://api.ipify.org?format=json', { httpsAgent: agent })
.then(response => console.log(response.data))
.catch(error => console.error(error));

With Country Targeting

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

function createProxy(username, password, options = {}) {
let user = username;

if (options.country) {
user += `-country-${options.country}`;
}
if (options.session) {
user += `-session-${options.session}`;
}

const proxyUrl = `http://${user}:${password}@proxy.weproxies.com:1080`;
return new HttpsProxyAgent(proxyUrl);
}

// US IP
const usAgent = createProxy('wp_user123', 'password', { country: 'US' });

axios.get('https://api.ipify.org?format=json', { httpsAgent: usAgent })
.then(response => console.log('US IP:', response.data));

// UK with sticky session
const ukAgent = createProxy('wp_user123', 'password', {
country: 'GB',
session: 'uk-session-1'
});

axios.get('https://api.ipify.org?format=json', { httpsAgent: ukAgent })
.then(response => console.log('UK IP:', response.data));

Axios Instance with Defaults

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

const proxyUrl = 'http://wp_user123:password@proxy.weproxies.com:1080';
const agent = new HttpsProxyAgent(proxyUrl);

const client = axios.create({
httpsAgent: agent,
timeout: 30000,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
});

// All requests use the proxy
client.get('https://example.com')
.then(response => console.log(response.status));

node-fetch

Fetch API for Node.js.

Installation

npm install node-fetch https-proxy-agent

Basic Usage

const fetch = require('node-fetch');
const HttpsProxyAgent = require('https-proxy-agent');

const proxyUrl = 'http://wp_user123:password@proxy.weproxies.com:1080';
const agent = new HttpsProxyAgent(proxyUrl);

fetch('https://api.ipify.org?format=json', { agent })
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err));

With Async/Await

const fetch = require('node-fetch');
const HttpsProxyAgent = require('https-proxy-agent');

async function fetchWithProxy(url, username, password, country = null) {
let user = username;
if (country) {
user += `-country-${country}`;
}

const proxyUrl = `http://${user}:${password}@proxy.weproxies.com:1080`;
const agent = new HttpsProxyAgent(proxyUrl);

const response = await fetch(url, {
agent,
timeout: 30000
});

return response.json();
}

// Usage
(async () => {
const result = await fetchWithProxy(
'https://api.ipify.org?format=json',
'wp_user123',
'password',
'US'
);
console.log(result);
})();

Got

A powerful HTTP library with built-in retry support.

Installation

npm install got hpagent

Basic Usage

const got = require('got');
const { HttpsProxyAgent } = require('hpagent');

const agent = new HttpsProxyAgent({
proxy: 'http://wp_user123:password@proxy.weproxies.com:1080'
});

got('https://api.ipify.org?format=json', {
agent: { https: agent },
responseType: 'json'
})
.then(response => console.log(response.body))
.catch(error => console.error(error));

With Retry Logic

const got = require('got');
const { HttpsProxyAgent } = require('hpagent');

const agent = new HttpsProxyAgent({
proxy: 'http://wp_user123:password@proxy.weproxies.com:1080'
});

const client = got.extend({
agent: { https: agent },
timeout: { request: 30000 },
retry: {
limit: 3,
methods: ['GET', 'POST'],
statusCodes: [408, 413, 429, 500, 502, 503, 504]
}
});

client('https://example.com')
.then(response => console.log(response.statusCode))
.catch(error => console.error(error));

Concurrent Requests

Using Promise.all

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

const countries = ['US', 'GB', 'DE', 'FR', 'JP'];

async function fetchFromMultipleCountries(url, username, password) {
const requests = countries.map(country => {
const proxyUrl = `http://${username}-country-${country}:${password}@proxy.weproxies.com:1080`;
const agent = new HttpsProxyAgent(proxyUrl);

return axios.get(url, { httpsAgent: agent })
.then(response => ({ country, data: response.data }))
.catch(error => ({ country, error: error.message }));
});

return Promise.all(requests);
}

// Usage
fetchFromMultipleCountries('https://api.ipify.org?format=json', 'wp_user123', 'password')
.then(results => {
results.forEach(result => {
console.log(`${result.country}: ${JSON.stringify(result.data || result.error)}`);
});
});

Rate-Limited Concurrent Requests

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

async function fetchWithRateLimit(urls, username, password, concurrency = 5) {
const results = [];
const proxyUrl = `http://${username}:${password}@proxy.weproxies.com:1080`;
const agent = new HttpsProxyAgent(proxyUrl);

for (let i = 0; i < urls.length; i += concurrency) {
const batch = urls.slice(i, i + concurrency);

const batchResults = await Promise.all(
batch.map(url =>
axios.get(url, { httpsAgent: agent })
.then(res => ({ url, data: res.data }))
.catch(err => ({ url, error: err.message }))
)
);

results.push(...batchResults);

// Small delay between batches
await new Promise(resolve => setTimeout(resolve, 100));
}

return results;
}

TypeScript Support

import axios, { AxiosInstance } from 'axios';
import HttpsProxyAgent from 'https-proxy-agent';

interface ProxyConfig {
username: string;
password: string;
country?: string;
session?: string;
}

function createProxyClient(config: ProxyConfig): AxiosInstance {
let user = config.username;

if (config.country) {
user += `-country-${config.country}`;
}
if (config.session) {
user += `-session-${config.session}`;
}

const proxyUrl = `http://${user}:${config.password}@proxy.weproxies.com:1080`;
const agent = new HttpsProxyAgent(proxyUrl);

return axios.create({
httpsAgent: agent,
timeout: 30000
});
}

// Usage
const client = createProxyClient({
username: 'wp_user123',
password: 'password',
country: 'US'
});

client.get<{ ip: string }>('https://api.ipify.org?format=json')
.then(response => console.log(response.data.ip));

Best Practices

1. Use Environment Variables

const username = process.env.WEPROXIES_USER;
const password = process.env.WEPROXIES_PASS;

2. Handle Errors Properly

try {
const response = await axios.get(url, { httpsAgent: agent });
return response.data;
} catch (error) {
if (error.code === 'ECONNREFUSED') {
console.error('Proxy connection refused');
} else if (error.response?.status === 407) {
console.error('Proxy authentication failed');
} else {
console.error('Request failed:', error.message);
}
}

3. Implement Timeouts

axios.get(url, {
httpsAgent: agent,
timeout: 30000 // 30 seconds
});