🎯 A customizable, anti-detection cloud browser powered by self-developed Chromium designed for web crawlers and AI Agents.👉Try Now
Back to Blog

Superagent Proxy Guide for Node.js: Complete Tutorial

Michael Lee
Michael Lee

Expert Network Defense Engineer

24-Dec-2025
Take a Quick Look

Master Superagent proxy integration with Scrapeless Proxies — powerful HTTP client for Node.js with enterprise-grade proxy support.

Introduction to Superagent and Proxy Integration

Superagent is a lightweight, flexible HTTP request library for Node.js that simplifies making HTTP requests with a clean, chainable API. One of its most powerful features is seamless proxy support, making it ideal for web scraping, API testing, and applications requiring anonymity or geographic flexibility.

This comprehensive guide covers everything you need to know about using proxies with Superagent, from basic configuration to advanced implementation strategies.

What is Superagent?

Superagent is a popular Node.js HTTP client library known for its:

Intuitive API: Simple, chainable syntax for constructing requests
Flexible: Supports various request types (GET, POST, PUT, DELETE, PATCH)
Middleware Support: Extensible through middleware functions
Automatic Serialization: Handles JSON and form data automatically
Error Handling: Built-in retry logic and error management
Lightweight: Minimal dependencies and fast performance

While Superagent is excellent for many use cases, its native proxy support requires additional configuration. This guide shows you exactly how to set it up.

Why Use Proxies with Superagent?

Integrating proxies with Superagent enables:

Web Scraping: Collect data from websites without IP blocking or detection
API Testing: Test APIs from different geographic locations and IP addresses
Load Distribution: Spread requests across multiple IPs for better performance
Anonymity: Hide your real IP address and server location
Bypass Restrictions: Access geographically restricted content and services
Rate Limit Management: Work around IP-based rate limiting by rotating addresses

Setting Up Superagent with Proxies

Step 1: Install Dependencies

Begin by installing Superagent and the necessary proxy agent packages:

bash Copy
npm install superagent
npm install https-proxy-agent
npm install http-proxy-agent
npm install socks-proxy-agent

These packages allow you to configure proxy support for HTTP, HTTPS, and SOCKS5 connections.

Step 2: Basic Proxy Configuration

Create a configuration file to manage your proxy settings:

javascript Copy
// proxy-config.js
module.exports = {
  // Proxy server details
  http: 'http://proxy.example.com:8080',
  https: 'https://proxy.example.com:8080',
  socks5: 'socks5://proxy.example.com:1080',
  
  // Authentication
  username: 'your-username',
  password: 'your-password',
  
  // Retry settings
  maxRetries: 3,
  retryDelay: 1000,
  
  // Timeout settings
  timeout: 30000
};

Step 3: Basic Superagent Proxy Request

Here's the simplest way to make a request through a proxy:

javascript Copy
const request = require('superagent');
const HttpProxyAgent = require('http-proxy-agent');
const HttpsProxyAgent = require('https-proxy-agent');
const config = require('./proxy-config');

async function requestWithProxy(url) {
  const httpAgent = new HttpProxyAgent(config.http);
  const httpsAgent = new HttpsProxyAgent(config.https);

  try {
    const response = await request
      .get(url)
      .agent(url.startsWith('https') ? httpsAgent : httpAgent)
      .timeout(config.timeout);

    return response.body;
  } catch (error) {
    console.error('Request failed:', error.message);
    throw error;
  }
}

// Usage
requestWithProxy('https://api.example.com/data')
  .then(data => console.log(data))
  .catch(error => console.error(error));

Step 4: Proxy with Authentication

For proxies requiring credentials, include authentication in the proxy URL:

javascript Copy
const request = require('superagent');
const HttpsProxyAgent = require('https-proxy-agent');

// Include credentials in proxy URL
const proxyUrl = `http://username:password@proxy.example.com:8080`;
const agent = new HttpsProxyAgent(proxyUrl);

async function requestWithAuth(url) {
  try {
    const response = await request
      .get(url)
      .agent(agent)
      .set('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36')
      .timeout(30000);

    return response.body;
  } catch (error) {
    console.error('Request failed:', error.message);
    throw error;
  }
}

Advanced Superagent Proxy Implementations

Implementing Proxy Rotation

Rotate through multiple proxies to avoid detection and distribute load:

javascript Copy
const request = require('superagent');
const HttpsProxyAgent = require('https-proxy-agent');

const proxyList = [
  'http://proxy1.com:8080',
  'http://proxy2.com:8080',
  'http://proxy3.com:8080'
];

let currentIndex = 0;

function getNextProxy() {
  const proxy = proxyList[currentIndex];
  currentIndex = (currentIndex + 1) % proxyList.length;
  return proxy;
}

async function requestWithRotation(url) {
  const proxy = getNextProxy();
  const agent = new HttpsProxyAgent(proxy);

  try {
    const response = await request
      .get(url)
      .agent(agent)
      .timeout(30000);

    console.log(`Success with proxy: ${proxy}`);
    return response.body;
  } catch (error) {
    console.error(`Failed with proxy ${proxy}:`, error.message);
    throw error;
  }
}

Implementing Retry Logic with Proxy Fallback

Create resilient requests that automatically retry with different proxies:

javascript Copy
async function requestWithRetry(url, maxRetries = 3) {
  let lastError;

  for (let attempt = 1; attempt <= maxRetries; attempt++) {
    try {
      const proxy = getNextProxy();
      const agent = new HttpsProxyAgent(proxy);

      const response = await request
        .get(url)
        .agent(agent)
        .timeout(15000);

      console.log(`Request succeeded on attempt ${attempt}`);
      return response.body;
    } catch (error) {
      lastError = error;
      console.log(`Attempt ${attempt} failed:`, error.message);
      
      // Exponential backoff
      if (attempt < maxRetries) {
        await new Promise(resolve => 
          setTimeout(resolve, Math.pow(2, attempt) * 1000)
        );
      }
    }
  }

  throw lastError;
}

Custom Superagent Middleware for Proxy Management

Create reusable middleware for proxy handling:

javascript Copy
const request = require('superagent');
const HttpsProxyAgent = require('https-proxy-agent');

function proxyMiddleware(proxy) {
  return (req) => {
    const agent = new HttpsProxyAgent(proxy);
    req.agent(agent);
  };
}

function retryMiddleware(maxRetries = 3) {
  return (req) => {
    req.retry(maxRetries, (err, res) => {
      // Retry on network errors or 5xx responses
      if (err) {
        return !!err.code && err.code !== 'ECONNABORTED';
      }
      return !!res && res.status >= 500;
    });
  };
}

// Usage with middleware
async function requestWithMiddleware(url, proxy) {
  try {
    const response = await request
      .get(url)
      .use(proxyMiddleware(proxy))
      .use(retryMiddleware(3))
      .set('User-Agent', 'Mozilla/5.0...')
      .timeout(30000);

    return response.body;
  } catch (error) {
    console.error('Request failed:', error.message);
    throw error;
  }
}

SOCKS5 Proxy Support

For SOCKS5 proxies, use the socks-proxy-agent:

javascript Copy
const request = require('superagent');
const SocksProxyAgent = require('socks-proxy-agent');

async function requestWithSocks5(url, socksProxy) {
  const agent = new SocksProxyAgent(socksProxy);

  try {
    const response = await request
      .get(url)
      .agent(agent)
      .timeout(30000);

    return response.body;
  } catch (error) {
    console.error('SOCKS5 request failed:', error.message);
    throw error;
  }
}

// Usage
// requestWithSocks5('https://example.com', 'socks5://proxy:1080');

POST Requests with Proxy

Superagent proxy support works for all HTTP methods:

javascript Copy
async function postWithProxy(url, data, proxy) {
  const agent = new HttpsProxyAgent(proxy);

  try {
    const response = await request
      .post(url)
      .agent(agent)
      .send(data)
      .set('Content-Type', 'application/json')
      .timeout(30000);

    return response.body;
  } catch (error) {
    console.error('POST request failed:', error.message);
    throw error;
  }
}

// Usage
postWithProxy('https://api.example.com/submit', 
  { key: 'value' }, 
  'http://proxy:8080')
  .then(response => console.log(response))
  .catch(error => console.error(error));

Scrapeless Proxies: Enterprise-Grade for Superagent

Scrapeless Proxies provide access to real residential, datacenter, IPv6, and static ISP IPs, designed for Node.js developers. With over 90 million residential IPs in 195+ countries, Scrapeless delivers unmatched coverage and reliability.

🌍 Residential Proxies

Perfect for Superagent web scraping:

  • 90M+ Real IPs: Access to over 90 million residential IP addresses across 195+ countries
  • Automatic Rotation: Seamless IP rotation prevents blocking
  • 99.98% Success Rate: Industry-leading reliability
  • Geo-targeting Support: Target specific geographic locations
  • Multi-Protocol Support: HTTP, HTTPS, and SOCKS5 protocols
  • Ultra-Fast Performance: Sub-0.5s response times
  • Easy Superagent Integration: Direct proxy URL support

⚡ Datacenter Proxies

High-speed datacenter solutions for Superagent:

  • 99.99% Uptime: Enterprise-grade reliability
  • Ultra-Fast Response: Optimized for speed-critical operations
  • Unlimited Sessions: No session duration restrictions
  • Easy API Integration: Simple setup with Superagent
  • High Bandwidth: Support for large-scale operations
  • Low Latency: Minimal performance impact
  • Cost-Effective: Affordable for high-volume usage

🔐 IPv6 Proxies

Next-generation IPv6 support for Node.js:

  • 50M+ IPv6 IPs: Extensive pool of verified IPv6 addresses
  • Automatic Rotation: Intelligent rotation mechanisms
  • High Anonymity: Maximum privacy protection
  • Dedicated IPs: Static IPv6 options available
  • GDPR & CCPA Compliant: Full regulatory compliance
  • Pay-Per-GB Billing: Transparent pricing model

🏠 Static ISP Proxies

Dedicated static IPs for long-term operations:

  • Dedicated Static IPs: Consistent IP addresses
  • 99.99% Uptime: Enterprise reliability
  • Low Latency: Minimal response delays
  • Perfect for APIs: Maintain persistent sessions
  • Geo-targeting Support: Location-based allocation
  • Multi-Protocol Support: HTTP, HTTPS, SOCKS5

Best Practices for Superagent Proxies

Always Use HTTPS Agents: For secure connections, use HttpsProxyAgent to maintain encryption.

Implement Proper Error Handling: Proxies may fail. Always use try-catch and implement retry logic.

Set Reasonable Timeouts: Proxies add latency. Use 30-60 second timeouts for typical operations.

Rotate Proxies: Change proxies frequently to avoid detection and distribute load.

Monitor Success Rates: Track which proxies work best and identify failing ones.

Use User-Agent Rotation: Combine proxy rotation with varied User-Agent headers.

Respect Rate Limits: Even with proxies, respect website rate limits and robots.txt.

Log and Debug: Maintain detailed logs for troubleshooting and performance analysis.

Troubleshooting Common Issues

Connection Refused Errors

Problem: Getting connection refused errors when using proxy.

Solution: Verify proxy server address and port are correct, ensure the proxy is running and accessible, check firewall rules aren't blocking the connection, and test proxy connectivity separately.

Proxy Authentication Failures

Problem: Getting 407 Proxy Authentication Required errors.

Solution: Ensure credentials are correctly encoded in the proxy URL, check for special characters requiring URL encoding, verify credentials are correct with your proxy provider, and test credentials independently.

Timeout Issues

Problem: Requests are timing out through the proxy.

Solution: Increase timeout values, check proxy server performance, reduce concurrent requests, and verify network connectivity to the proxy.

SSL/TLS Certificate Errors

Problem: Certificate validation errors when using HTTPS proxies.

Solution: Verify the proxy server's SSL certificate is valid, ensure your Node.js environment trusts the certificate, and consider whether you need to disable certificate verification (not recommended for production).

Frequently Asked Questions

How do I use rotating proxies with Superagent?

Create a proxy list and implement a rotation function that cycles through proxies. Use the rotation function when making requests to get the next proxy from the list.

Can I use SOCKS5 proxies with Superagent?

Yes, use the socks-proxy-agent package. Install it with npm install socks-proxy-agent and use it similarly to HTTP/HTTPS agents.

How do I handle proxy failures gracefully?

Implement retry logic that catches errors and attempts the request with a different proxy. Use exponential backoff between retries to avoid overwhelming proxies.

What's the best way to manage proxy credentials?

Store proxy credentials in environment variables or configuration files, never hardcode them in your source code. Use URL encoding for special characters in credentials.

Should I rotate User-Agents with proxies?

Yes, combining proxy rotation with User-Agent rotation makes your requests appear more natural and reduces detection risk.

How do I test if my proxy is working?

Make test requests to IP-checking services like https://httpbin.org/ip or https://api.ipify.org and verify the response shows the proxy's IP, not your real IP.

Can I use proxies for Superagent middleware?

Yes, you can create middleware that applies proxy settings to requests. This allows reusable, centralized proxy management across your application.

Conclusion

Superagent combined with proxies provides a powerful solution for web scraping, API testing, and anonymous browsing in Node.js applications. By following the patterns and best practices outlined in this guide, you can implement robust, production-grade proxy support.

With Scrapeless Proxies' extensive IP pool, reliable infrastructure, and direct support for HTTP, HTTPS, and SOCKS5 protocols, you have everything needed to build scalable Node.js applications. Start your free trial today and experience the performance and reliability of enterprise-grade proxies.

At Scrapeless, we only access publicly available data while strictly complying with applicable laws, regulations, and website privacy policies. The content in this blog is for demonstration purposes only and does not involve any illegal or infringing activities. We make no guarantees and disclaim all liability for the use of information from this blog or third-party links. Before engaging in any scraping activities, consult your legal advisor and review the target website's terms of service or obtain the necessary permissions.

Most Popular Articles

Catalogue