HTTPS Proxy Agent

9.0.0 · active · verified Tue Apr 21

https-proxy-agent is an `http.Agent` implementation specifically designed for making HTTPS requests through an HTTP or HTTPS proxy server. It leverages the HTTP `CONNECT` method to establish a TCP tunnel through the proxy to the target destination. This enables secure communication over proxied networks and is also compatible with protocols like WebSockets that utilize the `CONNECT` method for proxy tunneling. The current stable version is 9.0.0, released as part of the `proxy-agents` monorepo, which implies a release cadence tied to updates across the proxy agent family. A key differentiator is its direct focus on the `CONNECT` method for HTTPS and WebSocket tunneling, providing a robust solution for environments requiring explicit proxy configuration. It requires Node.js version 20 or higher.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure and use `HttpsProxyAgent` with Node.js's built-in `https` module to route requests through a specified HTTP(s) proxy server.

import * as https from 'https';
import { HttpsProxyAgent } from 'https-proxy-agent';

const PROXY_URL = process.env.HTTPS_PROXY ?? 'http://168.63.76.32:3128'; // Example proxy, replace with a real one
const TARGET_URL = 'https://example.com';

console.log(`Attempting to fetch ${TARGET_URL} via proxy ${PROXY_URL}`);

const agent = new HttpsProxyAgent(PROXY_URL, {
  headers: {
    'X-Custom-Proxy-Header': 'MyValue'
  }
});

https.get(TARGET_URL, { agent }, (res) => {
  console.log(`Status Code: ${res.statusCode}`);
  console.log('Response Headers:', res.headers);

  let data = '';
  res.on('data', (chunk) => {
    data += chunk;
  });
  res.on('end', () => {
    console.log('Response Body Snippet:', data.substring(0, 200) + '...');
  });
}).on('error', (err) => {
  console.error('Error fetching URL:', err.message);
});

view raw JSON →