> ## 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

> 通过将 base URL 设置为 https://api.cometapi.com/v1，将兼容 OpenAI 的客户端或直接 HTTP 请求路由到 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 auth。 |

## 相关链接

* [CometAPI 快速开始](/zh-Hans/overview/quick-start)
* [重要指南](/zh-Hans/overview/important-guidelines)
* [错误代码与处理](/zh-Hans/errors/error-codes-handling)
* [模型页面](/zh-Hans/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": "通过将 base URL 设置为 https://api.cometapi.com/v1，将兼容 OpenAI 的客户端或直接 HTTP 请求切换到 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>
