> For the complete documentation index, see [llms.txt](https://docs.talordata.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.talordata.com/cn-tw/proxy/integrations-guide/axios-talordata-integration-guide.md).

# Axios & Talordata代理集成指南

Axios 是一個基於 Promise 的 HTTP 客戶端，廣泛應用於資料抓取、API 呼叫等場景。Axios 支援透過內建的 `proxy` 配置項設定代理伺服器，請參考以下步驟設定代理（本教程適用於 Node.js 環境）

{% hint style="success" icon="lightbulb-exclamation-on" %}
在配置代理之前，您需要取得代理憑證。[點擊此處](/cn-tw/proxy/rotating-residential-proxies/quick-start.md)了解如何取得代理憑證
{% endhint %}

***

{% stepper %}
{% step %}

#### **安裝 Axios**

Axios 可透過多種套件管理工具安裝，具體取決於您的專案環境。請前往 [Axios 官方文件](https://axios-http.com/docs/intro) 檢視詳細的安裝指南。例如，使用 npm 安裝：

```http
npm install axios
```

{% endstep %}

{% step %}

#### **配置代理**

在您的程式碼中，需要建立一個包含代理資訊的配置物件。請將以下模板中的佔位符替換為您的實際憑證：

```javascript
const proxyConfig = {
  host: 'your-proxy-host',  
  port: your-proxy-port,       
  protocol: 'http',       
  auth: {
    username: 'your-username', 
    password: 'your-password'
  }
};
```

{% endstep %}

{% step %}

#### **在請求中使用代理**

以下是一個完整的範例，展示如何使用配置好的代理向 ipinfo.io 發送請求，該網站會回傳請求來源的 IP 資訊。請將此程式碼儲存為一個檔案，例如 `test-proxy.js`**。**

```javascript
const axios = require('axios');

// 您的目標網址，此處以 http://ipinfo.io/ 為例
const targetUrl = 'http://ipinfo.io/';

const proxyConfig = {
  host: '您的代理主機',
  port: 您的代理連接埠,
  protocol: 'http',
  auth: {
    username: '您的使用者名稱',
    password: '您的密碼'
  }
};

async function fetchData() {
  try {
    // 將 proxyConfig 作為選項傳遞給 axios.get
    const response = await axios.get(targetUrl, { proxy: proxyConfig });
    console.log('Success:', response.data);
  } catch (error) {
    console.error('Fail:', error.message);
  }
}

fetchData();
```

{% endstep %}

{% step %}

#### **驗證代理**

在終端機中，切換到您存放 JavaScript 檔案（例如 `test-proxy.js`）的目錄，執行以下命令（如果檔案名稱不同，請替換為實際名稱）：

```http
node test-proxy.js
```

如果代理配置正確，您將看到類似如下的輸出：

```javascript
Success: {
  ip: '203.0.113.45',
  city: 'Houston',
  region: 'Texas',
  country: 'US',
  loc: '29.7633,-95.3633',
  org: 'AS174 Cogent Communications, LLC',
  postal: '77002',
  timezone: 'America/Chicago',
  readme: 'https://ipinfo.io/missingauth'
}
```

{% endstep %}
{% endstepper %}

***
