v1.10.82-f67ee7d
Skip to main content
← Back to Code Snippets

JavaScript Got Proxy

Complete Got proxy integration example for JavaScript/Node.js with Hex Proxies. Includes authentication, timeouts, and retry handling.

JavaScriptGot
Install:npm install got https-proxy-agent
JavaScript / Got
const got = require("got");
const { HttpsProxyAgent } = require("https-proxy-agent");

const agent = new HttpsProxyAgent(
  "http://user:pass@gate.hexproxies.com:8080"
);

async function fetchWithProxy() {
  try {
    const response = await got("https://httpbin.org/ip", {
      agent: { https: agent },
      timeout: { request: 30000 },
      retry: { limit: 2, methods: ["GET"] },
      responseType: "json",
    });
    console.log("Origin IP:", response.body.origin);
  } catch (error) {
    if (error.code === "ERR_GOT_REQUEST_ERROR") {
      console.error("Proxy/network error:", error.message);
    } else {
      console.error("Request failed:", error.message);
    }
  }
}

fetchWithProxy();

Why Got for Proxy Work

Got distinguishes itself in the Node.js HTTP client landscape through its built-in retry engine, advanced timeout system, and hook-based lifecycle management. While Axios focuses on simplicity and node-fetch on standards compliance, Got is engineered for resilience, which is exactly what production proxy workloads demand. Its retry system automatically handles transient proxy failures with configurable backoff strategies, and its multi-phase timeout configuration catches problems at every stage of the proxied request: DNS resolution, socket connection, TLS handshake, first byte, and complete transfer.

Got's hook system provides seven lifecycle points (init, beforeRequest, beforeRedirect, beforeRetry, afterResponse, beforeError, and init) where you can inject proxy-specific logic without subclassing or wrapping the library. Register a `beforeRetry` hook that switches to a different proxy endpoint when the current one returns errors, or an `afterResponse` hook that checks for soft blocks (200 responses with CAPTCHA pages) and triggers a retry through gate.hexproxies.com:8080 with a new session.

Configuration Patterns

Got separates the HTTP and HTTPS agents in its configuration, accepting `agent: { http: httpAgent, https: httpsAgent }`. For most proxy workloads targeting HTTPS sites, you only need the `https` agent. Got's timeout object supports granular phase-based timing: `timeout: { lookup: 1000, connect: 5000, secureConnect: 5000, socket: 10000, response: 10000, send: 10000, request: 30000 }`. Each phase maps to a specific step in the proxied connection, making it straightforward to identify whether slowness comes from DNS, proxy connection, TLS, or target response.

Got's pagination helper is valuable for proxy-driven API crawling. Configure `got.paginate.all()` with a proxy agent and Got will follow paginated API responses automatically, applying the same proxy and retry configuration to each page. This eliminates boilerplate pagination loops and handles edge cases like mid-pagination proxy failures with automatic retry.

Common Pitfalls

Got v12+ is ESM-only, which is the same migration challenge as node-fetch v3. If your project uses CommonJS, either pin to Got v11 or migrate to ESM. The ESM migration requires `"type": "module"` in package.json and converting all `require()` calls to `import` statements. Got v11 and v12+ have slightly different APIs for retry and timeout configuration, so check the version-specific documentation.

Got's automatic retry behavior can mask proxy issues if not configured carefully. By default, Got retries on network errors and 5xx responses, which means a misconfigured proxy might trigger retries that succeed on the second attempt, hiding the underlying instability from your monitoring. Set `retry: { limit: 2, statusCodes: [408, 429, 500, 502, 503, 504] }` and log every retry through a `beforeRetry` hook so you can track proxy reliability over time rather than only seeing final outcomes.

Performance Optimization

Got's caching support via the `cache` option works well with proxy workloads where you fetch the same resources periodically. Pass a Keyv instance as the cache adapter and Got will automatically serve cached responses for repeated URLs, bypassing the proxy entirely for unchanged content. This is valuable for monitoring workloads where you check pages hourly but the content changes daily, saving both proxy bandwidth and target server load.

For throughput optimization, Got's stream interface outperforms the promise-based interface for large downloads because it does not buffer the entire response in memory. Use `got.stream(url, { agent: { https: agent } })` and pipe the response to a file write stream or a transform stream. This keeps memory usage flat regardless of response size and is essential when downloading files or crawling data-heavy pages through the proxy.

Tips

  • 1
    Got has built-in retry logic that works well with proxy rotation.
  • 2
    Set retry.methods to control which HTTP methods are retried on failure.
  • 3
    Use responseType json to skip manual parsing.

Ready to Integrate?

Get proxy credentials and start coding in minutes.