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

# Thay đổi base URL sang CometAPI

> Định tuyến một client tương thích OpenAI hoặc yêu cầu HTTP trực tiếp tới CometAPI bằng cách đặt base URL thành https://api.cometapi.com/v1.

Thay đổi base URL thành `https://api.cometapi.com/v1` khi bạn sử dụng SDK hoặc endpoint tương thích OpenAI. Giữ nguyên đường dẫn endpoint và định dạng phần thân yêu cầu theo tài liệu tham chiếu API, sau đó sử dụng API key CometAPI của bạn.

## Sử dụng base URL chính xác

Base URL tương thích OpenAI là:

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

## Base URL trong Python

Ví dụ Python sau chỉ thay đổi cấu hình client:

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

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

## Base URL trong Node.js

Ví dụ Node.js sau đặt base URL CometAPI:

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

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

## Kiểm tra thay đổi

Yêu cầu sau xác minh rằng base URL và API key hoạt động:

```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."
      }
    ]
  }'
```

Phản hồi thành công sẽ trả về một đối tượng chat completion:

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

## Các lỗi thường gặp

| Error                        | Fix                                                                                                          |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------ |
| `404`                        | Bao gồm `/v1` trong base URL cho các endpoint tương thích OpenAI.                                            |
| HTML instead of JSON         | Kiểm tra xem HTTP client của bạn có theo một chuyển hướng từ đường dẫn sai hay không.                        |
| `401`                        | Sử dụng `Authorization: Bearer $COMETAPI_KEY`.                                                               |
| Management API auth mismatch | Các endpoint quản lý API key sử dụng giá trị truy cập cá nhân, không phải xác thực bearer cho yêu cầu model. |

## Liên kết liên quan

* [Bắt đầu nhanh với CometAPI](/vi/overview/quick-start)
* [Hướng dẫn quan trọng](/vi/overview/important-guidelines)
* [Mã lỗi & cách xử lý](/vi/errors/error-codes-handling)
* [Trang models](/vi/overview/models)
* [Danh mục model](https://www.cometapi.com/models/)
* [Bảng giá](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": "Làm cách nào để thay đổi base URL sang CometAPI?",
        "description": "Thay đổi một client tương thích OpenAI hoặc yêu cầu HTTP trực tiếp sang CometAPI bằng cách đặt base URL thành https://api.cometapi.com/v1.",
        "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": "Tài liệu CometAPI",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "Hướng dẫn",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "Làm cách nào để thay đổi base URL sang CometAPI?",
            "item": "https://apidoc.cometapi.com/guides/change-base-url-to-cometapi"
          }
        ]
      }
    ]
    }
    `}
</script>
