> For the complete documentation index, see [llms.txt](https://docs.ipwo.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ipwo.net/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-claude-api-zhong-shi-yong-ipwo-dai-li.md).

# 如何在 Claude API 中使用 IPWO 代理

在部分网络环境下，开发者可能需要通过代理服务器访问 Claude API。通过配置 IPWO 住宅代理，您可以为 Claude API 请求提供稳定的网络连接，并根据业务需求选择不同国家或地区的出口 IP。

本文将介绍如何在 Python 和 Node.js 环境中为 Claude API 配置 IPWO 代理。

***

### 环境准备

开始之前，请确保您已经：

* 拥有 [IPWO 住宅代理账号](/readme/zhu-ce-deng-lu.md)
* [获取代理用户名和密码](/kai-fa-zhe-wen-dang/kuai-su-kai-shi/huo-qu-dai-li-xin-xi.md)
* 获取代理服务器地址和端口
* 拥有 Claude API Key

示例代理信息：

```
Proxy Host: us.ipwo.net
Proxy Port: 7878

Username: username
Password: password
```

***

### 安装 Claude SDK

#### Python

```bash
pip install anthropic httpx
```

#### Node.js

```bash
npm install @anthropic-ai/sdk https-proxy-agent
```

***

### Python 示例

Claude 官方 Python SDK 基于 HTTP 请求运行，可通过 httpx 配置代理。

```python
from anthropic import Anthropic
import httpx

proxy_url = "http://username:password@us.ipwo.net:7878"

http_client = httpx.Client(
    proxy=proxy_url
)

client = Anthropic(
    api_key="YOUR_CLAUDE_API_KEY",
    http_client=http_client
)

response = client.messages.create(
    model="claude-sonnet-4-0",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Hello Claude"
        }
    ]
)

print(response.content[0].text)
```

***

### Python 异步示例

```python
import asyncio
import httpx
from anthropic import AsyncAnthropic

proxy_url = "http://username:password@us.ipwo.net:7878"

async def main():
    http_client = httpx.AsyncClient(
        proxy=proxy_url
    )

    client = AsyncAnthropic(
        api_key="YOUR_CLAUDE_API_KEY",
        http_client=http_client
    )

    response = await client.messages.create(
        model="claude-sonnet-4-0",
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": "Hello Claude"
            }
        ]
    )

    print(response.content[0].text)

asyncio.run(main())
```

***

### Node.js 示例

通过 https-proxy-agent 将 Claude API 请求转发至代理服务器。

```javascript
import Anthropic from "@anthropic-ai/sdk";
import { HttpsProxyAgent } from "https-proxy-agent";

const proxyAgent = new HttpsProxyAgent(
  "http://username:password@us.ipwo.net:7878"
);

const anthropic = new Anthropic({
  apiKey: process.env.CLAUDE_API_KEY,
  httpAgent: proxyAgent
});

const response = await anthropic.messages.create({
  model: "claude-sonnet-4-0",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: "Hello Claude"
    }
  ]
});

console.log(response.content[0].text);
```

***

### 使用环境变量配置代理

部分程序和框架会自动读取系统代理配置。

#### Linux/macOS

```bash
export HTTP_PROXY=http://username:password@us.ipwo.net:7878
export HTTPS_PROXY=http://username:password@us.ipwo.net:7878
```

#### Windows

```cmd
set HTTP_PROXY=http://username:password@us.ipwo.net:7878
set HTTPS_PROXY=http://username:password@us.ipwo.net:7878
```

***

### 验证代理是否生效

在调用 Claude API 前，建议先验证代理连接是否正常。

```bash
curl -x "us.ipwo.net:7878" \
-U "username:password" \
ipinfo.io
```

如果返回代理出口 IP 信息，说明代理连接正常。

***

### 常见问题

#### Connection Timeout

可能原因：

* 网络无法访问代理节点
* 代理账号已过期
* 防火墙限制连接

解决方案：

* 检查代理账户状态
* 测试其他节点
* 使用 curl 验证代理连接

***

#### Proxy Authentication Failed

可能原因：

* 用户名错误
* 密码错误
* 代理认证格式错误

请检查：

```
username
password
host
port
```

是否与控制台显示的信息一致。

***

#### Claude API 返回 401 Unauthorized

此错误通常与 Claude API Key 有关，而非代理配置问题。

建议检查：

* API Key 是否有效
* API Key 是否正确加载
* 账户额度是否正常

***

### 最佳实践

* 长时间运行的 AI 应用建议使用 Sticky Session，减少连接波动。
* 在生产环境中使用环境变量管理 API Key 和代理凭证。
* 高并发场景建议控制请求速率，避免频繁建立连接。
* Docker、云服务器和自动化工作流环境建议统一配置系统代理。

***

### 相关阅读

完成 Claude API 代理配置后，您可能还需要：

* [OpenAI SDK 使用代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-openai-sdk-zhong-shi-yong-ipwo-dai-li.md)
* [Gemini API 使用代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-gemini-api-zhong-shi-yong-ipwo-dai-li.md)
* [LangChain 使用住宅代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-langchain-xiang-mu-zhong-pei-zhi-ipwo-dai-li.md)
* [Browser Use 使用住宅代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-browser-use-zhong-pei-zhi-ipwo-dai-li.md)
* [MCP 工具代理配置指南](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-wei-mcp-gong-ju-he-mcp-server-pei-zhi-ipwo-zhu-zhai-dai-li.md)
* [获取和配置住宅代理](/kai-fa-zhe-wen-dang/kuai-su-kai-shi/huo-qu-dai-li-xin-xi.md)
* [如何检测代理是否生效](/dai-li-jian-ce-yu-yin-si-an-quan/ru-he-jian-ce-dai-li-shi-fou-sheng-xiao.md)

> 提示：如果您正在开发 AI Agent、自动化工作流或多模型应用，建议统一使用代理配置管理 OpenAI、Claude、Gemini 等模型服务，以简化部署和维护流程。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ipwo.net/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-claude-api-zhong-shi-yong-ipwo-dai-li.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
