> For the complete documentation index, see [llms.txt](https://docs.tinyapi.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tinyapi.org/1-register/10-openai-chat.md).

# (10) OpenAI-compatible chat

## OpenAI-compatible chat (GPT)

**Quick Start** · 2026/6/13 · อ่านประมาณ 5 นาที

เรียกโมเดลแชตผ่าน OpenAI Chat Completions format ซึ่งเป็นรูปแบบที่เครื่องมือส่วนใหญ่รองรับ

## Endpoint และ Header

**Method**

```
POST
```

**Endpoint**

```
${apiUrl
```

**Authorization**

```
Authorization: Bearer sk-YOUR_API_KEY
```

**Content-Type**

```
application/json
```

## Parameters หลัก

| PARAMETER         | REQUIRED | DESCRIPTION                                         |
| ----------------- | -------- | --------------------------------------------------- |
| model             | ใช่      | Model ID ที่รองรับ openai จาก /v1/models            |
| messages          | ใช่      | ประวัติสนทนา เช่น system, user และ assistant        |
| stream            | ไม่      | true เพื่อรับคำตอบแบบ SSE ทีละส่วน                  |
| max\_tokens       | ไม่      | จำนวน output token สูงสุด                           |
| temperature       | ไม่      | ควบคุมความสุ่มของคำตอบ                              |
| top\_p            | ไม่      | ควบคุมการสุ่มแบบ nucleus sampling                   |
| response\_format  | ไม่      | กำหนดรูปแบบคำตอบ เช่น JSON เมื่อโมเดลรองรับ         |
| tools             | ไม่      | รายการ function หรือ tools ที่โมเดลสามารถเลือกเรียก |
| tool\_choice      | ไม่      | กำหนดว่าจะให้โมเดลเลือก tool แบบใด                  |
| reasoning\_effort | ไม่      | ระดับ reasoning สำหรับโมเดลที่รองรับ                |

## Non-streaming

**cURL**

```bash
curl https://api.tinyapi.org/v1/chat/completions \\
  -H "Authorization: Bearer sk-YOUR_API_KEY" \\
  -H "content-type: application/json" \\
  -d '{
    "model": "your-model-id",
    "messages": [
      {
        "role": "user",
        "content": "Explain what an API is in one sentence."
      }
    ]
  }'
```

**Example response**

```json
{
  "id": "chatcmpl_xxxxxxxxx",
  "object": "chat.completion",
  "model": "your-model-id",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "An API allows software applications to communicate and exchange data."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 20,
    "completion_tokens": 18,
    "total_tokens": 38
  }
}
```

## Streaming

เมื่อใช้ `stream: true` ระบบจะส่ง `chat.completion.chunk` หลายชุดและปิดท้าย stream ตามรูปแบบที่ upstream รองรับ

**cURL streaming**

```bash
curl https://api.tinyapi.org/v1/chat/completions \\
  -H "Authorization: Bearer sk-YOUR_API_KEY" \\
  -H "content-type: application/json" \\
  -d '{
    "model": "your-model-id",
    "stream": true,
    "messages": [
      {
        "role": "user",
        "content": "Write a short greeting."
      }
    ]
  }'
```

## ข้อควรรู้

* Endpoint นี้ใช้ได้กับทุกโมเดลที่ประกาศ `openai` ไม่จำเป็นต้องเป็นโมเดลจาก OpenAI เท่านั้น
* Parameter ขั้นสูงอาจไม่รองรับทุกโมเดล ให้ดู Supported parameters ในหน้า All AI Model ก่อนใช้งาน
* สำหรับ SDK ให้ตั้ง Base URL เป็น `https://api.tinyapi.org/v1`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.tinyapi.org/1-register/10-openai-chat.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
