> ## 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 無效？

## 常見錯誤現象

當您看到類似以下錯誤訊息时，通常是請求地址（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`

## 什麼是 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. 進入「API 密鑰管理」頁面
> 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.