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

# OpenAI Chat Completions API गाइड

> पूर्ण request URL, Bearer API Key, curl, Python और JavaScript उदाहरण के साथ BetterToken का OpenAI-compatible Chat Completions API उपयोग करें।

OpenAI-compatible Chat Completions को `POST https://www.bettertoken.ai/v1/chat/completions` से कॉल करें। अधिकतर external tools Base URL `https://www.bettertoken.ai/v1` लेते हैं और `/chat/completions` स्वयं जोड़ते हैं।

## मुख्य सेटिंग्स

| फ़ील्ड           | मान                                              |
| ---------------- | ------------------------------------------------ |
| API Key          | BetterToken API Key                              |
| Full request URL | `https://www.bettertoken.ai/v1/chat/completions` |
| Model            | `YOUR_MODEL_ID`                                  |

## आवश्यकताएं

* <a href={"https://bettertoken.ai/register"}>BetterToken API Key बनाएं</a>
* <a href={"https://bettertoken.ai/pricing"}>model plaza</a> या key **Setup** dialog से Model ID कॉपी करें
* `curl` या Python 3 / Node.js वाला terminal तैयार रखें

## इंस्टॉल करें

<Tabs>
  <Tab title="curl">
    `curl` के लिए SDK नहीं चाहिए। पुष्टि करें कि `curl --version` आपके terminal में चलता है।
  </Tab>

  <Tab title="Python">
    ```bash theme={null}
    python -m pip install openai
    ```
  </Tab>

  <Tab title="JavaScript">
    ```bash theme={null}
    npm install openai
    ```
  </Tab>
</Tabs>

## मैन्युअल सेटअप

### curl

```bash theme={null}
curl "https://www.bettertoken.ai/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  --data '{
    "model": "YOUR_MODEL_ID",
    "messages": [
      {
        "role": "user",
        "content": "Hello"
      }
    ]
  }'
```

### Python SDK

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

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://www.bettertoken.ai/v1",
)

response = client.chat.completions.create(
    model="YOUR_MODEL_ID",
    messages=[
        {"role": "user", "content": "Hello"},
    ],
)

print(response.choices[0].message.content)
```

### JavaScript SDK

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

const client = new OpenAI({
  apiKey: "YOUR_API_KEY",
  baseURL: "https://www.bettertoken.ai/v1",
});

const response = await client.chat.completions.create({
  model: "YOUR_MODEL_ID",
  messages: [{ role: "user", content: "Hello" }],
});

console.log(response.choices[0].message.content);
```

## कनेक्शन सत्यापित करें

दाईं ओर Playground में:

1. **Authorization** के अंतर्गत `YOUR_API_KEY` भरें।
2. `model` को `YOUR_MODEL_ID` से बदलें।
3. `messages` संपादित करें और अनुरोध भेजें।

Playground पहले से पूरा URL `https://www.bettertoken.ai/v1/chat/completions` उपयोग करता है।

मॉडल output के साथ `200` response का `choices[0].message.content` में होना कनेक्शन की पुष्टि करता है।

## मॉडल बदलें

`YOUR_MODEL_ID` को model plaza या **Setup** dialog की पूरी Model ID से बदलें। display name के आधार पर Model ID को छोटा न करें।

## सामान्य त्रुटियां

| त्रुटि         | समाधान                                                                                                                                         |
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `401`          | `Authorization: Bearer YOUR_API_KEY` की पुष्टि करें, फिर BetterToken API Key दोबारा कॉपी करें।                                                 |
| `404`          | direct HTTP call `https://www.bettertoken.ai/v1/chat/completions` उपयोग करता है; SDK `base_url` `https://www.bettertoken.ai/v1` उपयोग करता है। |
| मॉडल नहीं मिला | model plaza या **Setup** dialog की पूरी Model ID उपयोग करें।                                                                                   |
| अमान्य अनुरोध  | पुष्टि करें कि `messages` एक array है और हर item में `role` व `content` हैं।                                                                   |

## उन्नत सेटअप

### समर्थित provider

| Provider | स्थिति         |
| -------- | -------------- |
| Claude   | समर्थित नहीं   |
| GPT      | मैन्युअल सेटअप |
| Kimi     | मैन्युअल सेटअप |
| GLM      | मैन्युअल सेटअप |

<Note>ये स्थितियां केवल इस पेज पर बताए गए BetterToken सेटअप पर लागू होती हैं।</Note>

<Accordion title="सेटअप के तरीकों का अर्थ">
  * **मैन्युअल सेटअप**: API Key, Base URL और Model दर्ज करें।
  * **समर्थित नहीं**: सीधे सेटअप का कोई सत्यापित तरीका अभी उपलब्ध नहीं है।
</Accordion>

## तकनीकी विवरण

<Accordion title="Base URL और पूर्ण request URL">
  पूरा request URL मांगने वाले tool, जैसे GitHub Copilot और TRAE, `https://www.bettertoken.ai/v1/chat/completions` उपयोग करते हैं। path स्वयं जोड़ने वाले tool, जैसे Cursor, Cline, OpenCode, n8n और Dify, केवल `https://www.bettertoken.ai/v1` उपयोग करते हैं।
</Accordion>


## OpenAPI

````yaml api-reference/openapi.json POST /v1/chat/completions
openapi: 3.1.0
info:
  title: BetterToken GPT Image 2 API
  description: >-
    OpenAI-compatible image generation and image editing endpoints for
    BetterToken.
  version: 1.0.0
servers:
  - url: https://www.bettertoken.ai
security:
  - bearerAuth: []
paths:
  /v1/chat/completions:
    post:
      tags:
        - OpenAI Chat Completions
      summary: 创建 Chat Completions 响应
      description: >-
        通过 BetterToken 的 OpenAI-compatible Chat Completions API 向 GPT
        提供商模型发送对话消息。适用于需要 /v1/chat/completions 的外部工具和应用。
      operationId: createChatCompletion
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionsRequest'
            example:
              model: YOUR_MODEL_ID
              messages:
                - role: user
                  content: Hello
      responses:
        '200':
          $ref: '#/components/responses/ChatCompletionResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '402':
          $ref: '#/components/responses/PaymentRequired'
        '429':
          $ref: '#/components/responses/RateLimited'
        '500':
          $ref: '#/components/responses/ServerError'
      x-codeSamples:
        - lang: cURL
          label: cURL
          source: |-
            curl 'https://www.bettertoken.ai/v1/chat/completions' \
              -H 'Authorization: Bearer YOUR_API_KEY' \
              -H 'Content-Type: application/json' \
              --data '{
                "model": "YOUR_MODEL_ID",
                "messages": [{"role": "user", "content": "Hello"}]
              }'
components:
  schemas:
    ChatCompletionsRequest:
      type: object
      required:
        - model
        - messages
      properties:
        model:
          type: string
          description: GPT 提供商中可用的模型 ID。请以模型广场当前显示的模型为准。
          example: YOUR_MODEL_ID
        messages:
          type: array
          minItems: 1
          description: 对话消息数组。每项应包含 role 和 content。
          items:
            type: object
            required:
              - role
              - content
            properties:
              role:
                type: string
                example: user
              content:
                type: string
                example: Hello
            additionalProperties: true
        stream:
          type: boolean
          description: 是否请求流式响应。
          default: false
      additionalProperties: true
    ChatCompletionResponse:
      type: object
      description: OpenAI-compatible Chat Completions API 响应。实际字段会随所选模型和请求参数变化。
      additionalProperties: true
    ErrorResponse:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: invalid request
            type:
              type: string
              example: invalid_request_error
            code:
              type: string
              example: invalid_request
          additionalProperties: true
        message:
          type: string
          example: insufficient quota
      additionalProperties: true
  responses:
    ChatCompletionResponse:
      description: GPT 提供商模型返回的 Chat Completions API 响应。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ChatCompletionResponse'
    BadRequest:
      description: 请求格式错误、缺少参数、JSON 或 multipart 解析失败、尺寸格式错误。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    Unauthorized:
      description: API Key 缺失或无效。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    PaymentRequired:
      description: 额度或余额不足。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    RateLimited:
      description: 触发限速、并发限制或上游繁忙。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    ServerError:
      description: 网关或上游服务异常。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: BetterToken API Key
      description: >-
        Use your BetterToken API Key as a bearer token. Do not expose API keys
        in frontend browser code, screenshots, logs, tickets, or Git
        repositories.

````