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

# 將 base URL 變更為 CometAPI

> 將 OpenAI 相容的用戶端或直接 HTTP 請求透過把 base URL 設為 https://api.cometapi.com/v1 導向到 CometAPI。

當你使用 OpenAI 相容的 SDK 或端點時，請將 base URL 變更為 `https://api.cometapi.com/v1`。保留 API 參考文件中的端點路徑與請求主體格式，然後使用你的 CometAPI API key。

## 使用正確的 base URL

OpenAI 相容的 base URL 為：

```text theme={null}
https://api.cometapi.com/v1
```

## Python base URL

以下 Python 範例只變更用戶端設定：

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

client = OpenAI(
    api_key=os.environ["COMETAPI_KEY"],
    base_url="https://api.cometapi.com/v1",
)
```

## Node.js base URL

以下 Node.js 範例會設定 CometAPI 的 base URL：

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

const client = new OpenAI({
  apiKey: process.env.COMETAPI_KEY,
  baseURL: "https://api.cometapi.com/v1",
});
```

## 測試變更

以下請求會驗證 base URL 和 API key 是否可正常運作：

```bash theme={null}
curl https://api.cometapi.com/v1/chat/completions \
  -H "Authorization: Bearer $COMETAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-model-id",
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ]
  }'
```

成功的回應會傳回一個聊天補全物件：

```json theme={null}
{
  "object": "chat.completion",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello from CometAPI."
      }
    }
  ]
}
```

## 常見錯誤

| 錯誤            | 修正方式                                               |
| ------------- | -------------------------------------------------- |
| `404`         | 在 OpenAI 相容端點的 base URL 中包含 `/v1`。                 |
| HTML 而不是 JSON | 檢查你的 HTTP 用戶端是否因錯誤路徑而跟隨了重新導向。                      |
| `401`         | 使用 `Authorization: Bearer $COMETAPI_KEY`。          |
| 管理 API 驗證不相符  | API key 管理端點使用的是個人存取值，而不是 bearer model-request 驗證。 |

## 相關連結

* [CometAPI 快速開始](/zh-Hant/overview/quick-start)
* [重要指南](/zh-Hant/overview/important-guidelines)
* [錯誤代碼與處理](/zh-Hant/errors/error-codes-handling)
* [模型頁面](/zh-Hant/overview/models)
* [模型目錄](https://www.cometapi.com/models/)
* [定價](https://www.cometapi.com/pricing/)

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "TechArticle",
        "@id": "https://apidoc.cometapi.com/guides/change-base-url-to-cometapi",
        "headline": "如何將 base URL 變更為 CometAPI？",
        "description": "將 OpenAI 相容的用戶端或直接 HTTP 請求透過把 base URL 設為 https://api.cometapi.com/v1 導向到 CometAPI。",
        "url": "https://apidoc.cometapi.com/guides/change-base-url-to-cometapi",
        "author": {
          "@type": "Organization",
          "name": "CometAPI"
        },
        "publisher": {
          "@type": "Organization",
          "name": "CometAPI",
          "url": "https://www.cometapi.com"
        }
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "CometAPI 文件",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "指南",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "如何將 base URL 變更為 CometAPI？",
            "item": "https://apidoc.cometapi.com/guides/change-base-url-to-cometapi"
          }
        ]
      }
    ]
    }
    `}
</script>
