Axios Proxy Builder

0.1.2 · active · verified Sun Apr 19

axios-proxy-builder is a focused utility designed to generate Axios-compatible proxy configuration objects by interpreting standard HTTP/HTTPS proxy environment variables, including `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY`. Currently at version 0.1.2, this package provides a single `configureProxy` function. Its `no-proxy` exclusion logic is notably inspired by the robust implementation found in the now-deprecated `request` library. This tool simplifies proxy integration for Axios in Node.js environments, particularly where proxy settings are managed via system environment variables. While it's in early development, indicated by its 0.x.x versioning, it offers a specific and streamlined solution. The release cadence is likely infrequent, driven by user feedback or minor enhancements rather than a fixed schedule. It differentiates itself from manually configuring Axios proxies by abstracting the parsing of common proxy environment variables, making it a 'set and forget' solution for many common proxy use cases.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure standard HTTP proxy environment variables and then use `configureProxy` to apply the detected proxy settings to an Axios GET request.

import axios from 'axios';
import { configureProxy } from 'axios-proxy-builder';

// IMPORTANT: Set these environment variables before running the script
// For example, in your shell:
// export HTTP_PROXY="http://your.proxy.server:8080"
// export NO_PROXY="localhost,127.0.0.1,api.example.com"
// For HTTPS, also set HTTPS_PROXY.

const targetUrl = 'https://api.ipify.org/?format=json'; // A public API to test IP

async function makeProxiedRequest() {
  // Retrieve proxy configuration based on current environment variables
  const proxyConfig = configureProxy(targetUrl);

  try {
    console.log('Attempting request to:', targetUrl);
    if (proxyConfig) {
      console.log('Using proxy configuration:', proxyConfig);
    } else {
      console.log('No proxy configured via environment variables for this URL.');
    }

    const response = await axios.get(targetUrl, {
      proxy: proxyConfig || false, // Pass the generated proxy object, or false if no proxy
    });

    console.log('Response data:', response.data); // Should show the proxy's IP if configured
    console.log('Request successful!');
  } catch (error: any) {
    if (axios.isAxiosError(error)) {
      console.error('Axios Error:', error.message);
      if (error.response) {
        console.error('Status:', error.response.status);
        console.error('Data:', error.response.data);
      } else if (error.request) {
        console.error('No response received:', error.request);
      } else {
        console.error('Error config:', error.config);
      }
    } else {
      console.error('Unexpected Error:', error);
    }
  }
}

// You would typically call this function in your application flow
// For demonstration, we'll call it directly.
makeProxiedRequest();

view raw JSON →