> ## Documentation Index
> Fetch the complete documentation index at: https://mixroute.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 为什么提示 API Key 无效？

> 解决 API Key 无效错误，了解 Base URL 和 API Key 的正确配置方法

## 常见错误现象

当您看到类似以下错误信息时，通常是**请求地址（Base URL）配置错误**。

```json theme={null}
{
  "error": {
    "message": "Incorrect API key provided: sk-QqHvK***...",
    "type": "invalid_request_error",
    "code": "invalid_api_key"
  }
}
```

> [!WARNING]
>
> 有可能使用了 MixRoute 的 API Key，但请求地址仍然指向 OpenAI 官网 [https://api.openai.com](https://api.openai.com)

## 什么是 Base URL？

Base URL（基础 URL / 请求地址）是 API 请求的目标服务器地址。不同的 API 服务提供商使用不同的 Base URL。

### **MixRoute 的** Base URL 和 API Key 必须对应

```python theme={null}
client = OpenAI(
    api_key="sk-mixroute-key",
    base_url="https://api.mixroute.ai/v1"
    # ✅ 使用 MixRoute API 地址
)
```

## 正确的配置方法

### 方法一：修改 Base URL（推荐）

**Python**

```python theme={null}
from openai import OpenAI

client = OpenAI(
    api_key="sk-your-mixroute-key",  # MixRoute 后台获取的 API Key
    base_url="https://api.mixroute.ai/v1"  # 改为 MixRoute API 地址
)

response = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "你好"}]
)
```

**Node.js**

```javascript theme={null}
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'sk-your-mixroute-key',
  baseURL: 'https://api.mixroute.ai/v1'
});

const response = await client.chat.completions.create({
  model: 'gpt-5.5',
  messages: [{ role: 'user', content: '你好' }]
});
```

**cURL**

```bash theme={null}
curl https://api.mixroute.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-your-mixroute-key" \
  -d '{
    "model": "gpt-5.5",
    "messages": [{"role": "user", "content": "你好"}]
  }'
```

### 方法二：使用环境变量

**Linux/macOS**

```bash theme={null}
export OPENAI_API_KEY="sk-your-mixroute-key"
export OPENAI_BASE_URL="https://api.mixroute.ai/v1"
```

**Windows PowerShell**

```powershell theme={null}
$env:OPENAI_API_KEY="sk-your-mixroute-key"
$env:OPENAI_BASE_URL="https://api.mixroute.ai/v1"
```

**Windows CMD**

```cmd theme={null}
set OPENAI_API_KEY=sk-your-mixroute-key
set OPENAI_BASE_URL=https://api.mixroute.ai/v1
```

## 支持的请求地址格式

| 格式 | 地址 | 适用场景 |
| - | - | - |
| 带 /v1（推荐） | `https://api.mixroute.ai/v1` | 大多数代码库 |
| 带 /v1/（末尾斜杠） | `https://api.mixroute.ai/v1/` | 某些框架要求 |
| 完整路径 | `https://api.mixroute.ai/v1/chat/completions` | cURL 请求 |

## 快速测试方法

使用 cURL 命令快速验证配置是否正确：

```bash theme={null}
curl https://api.mixroute.ai/v1/models \
  -H "Authorization: Bearer sk-your-mixroute-key"
```

预期结果：返回可用模型列表

```json theme={null}
{
  "data": [
    {
      "id": "gpt-5.5",
      "object": "model"
    }
  ]
}
```

如果返回错误，请检查：

1. API Key 是否正确复制（注意首尾空格）
2. 网络连接是否正常
3. 账户余额是否充足

## 常见问题排查

> **确认修改了 Base URL，但仍然报错**
>
> 可能原因：
>
> 1. **拼写错误**：确认地址拼写正确
> 2. **缓存问题**：重启程序或清除缓存后重试
> 3. **使用了代理或中间件**：某些代理工具可能会重定向请求
> 4. **代码中有多处配置**：检查配置文件、环境变量、代码初始化等

> **如何确认 Key 是否有效？**
>
> 在 MixRoute 后台查看：
>
> 1. 登录 [MixRoute 控制台](https://api.mixroute.ai/)
> 2. 进入「令牌」页面
> 3. 检查 Key 状态是否为「启用」
> 4. 确认账户余额充足

> **使用第三方工具如何配置？**
>
> 大多数第三方工具都有「自定义 API」选项：
>
> * **API 地址 / Base URL**：`https://api.mixroute.ai/v1`
> * **API Key**：从 [MixRoute 控制台](https://api.mixroute.ai/) 复制您的 Key
> * **模型名称**：参考[**模型广场**](https://console.mixroute.ai/models)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.