> For the complete documentation index, see [llms.txt](https://docs.clickai.vn/clickai-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clickai.vn/clickai-docs/nha-phat-trien/tich-hop-clickai-qua-restful-api.md).

# Tích hợp ClickAI qua RESTful API

## Mục lục

·       \[Cách API Integration hoạt động]\(#cách-api-integration-hoạt-động)

·       \[Bắt đầu]\(#bắt-đầu)

·       \[Xác thực (Authentication)]\(#xác-thực-authentication)

·       \[API lấy danh sách Assistants]\(#api-lấy-danh-sách-assistants)

·       \[API cho Text Generation App]\(#api-cho-text-generation-app)

·       \[API cho Conversational App (Chatbot / Agent)]\(#api-cho-conversational-app)

·       \[API cho Workflow App]\(#api-cho-workflow-app)

·       \[Quản lý conversation\_id]\(#quản-lý-conversation\_id)

·       \[Streaming vs Blocking Response]\(#streaming-vs-blocking-response)

·       \[File Upload API]\(#file-upload-api)

·       \[Feedback API]\(#feedback-api)

·       \[Conversations API]\(#conversations-api)

·       \[Error Handling]\(#error-handling)

·       \[Rate Limits]\(#rate-limits)

·       \[SDK & Libraries]\(#sdk--libraries)

&#x20;

## Cách API Integration hoạt động

┌─────────────────┐                    ┌─────────────────┐\
│   Ứng dụng      │   HTTP Request     │   ClickAI       │\
│   của bạn       │──────────────────▶│   Platform      │\
│   (Frontend/    │                    │                 │\
│    Backend)     │◀──────────────────│   AI Processing │\
│                 │   JSON / SSE       │                 │\
└─────────────────┘   Response         └─────────────────┘

1\.     Build ứng dụng AI trong ClickAI Studio

2\.     Generate API credentials (Secret Key)

3\.     Call API từ ứng dụng của bạn

4\.     Users tương tác qua giao diện của bạn — ClickAI xử lý AI phía sau

&#x20;

## Bắt đầu

### Bước 1: Truy cập API Settings

5\.     Mở ứng dụng trong ClickAI Studio

6\.     Nhấn "Publish" → Chọn tab "API"

7\.     Xem tài liệu API tự động cho ứng dụng

### Bước 2: Tạo API Key

8\.     Trong tab API, nhấn "+ Create API Key"

9\.     Đặt tên mô tả (ví dụ: production-backend)

10\.  Copy Secret Key — lưu trữ an toàn, chỉ hiển thị 1 lần

### Bước 3: Xác định endpoint phù hợp

ClickAI có hai base URL tùy theo loại API:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Base URL</td><td valign="top">Dùng cho</td></tr><tr><td valign="top">https://clickai.io</td><td valign="top">Studio / Console APIs (quản lý apps, workspace)</td></tr><tr><td valign="top">https://api.clickai.io</td><td valign="top">App Runtime APIs (chat, workflow, files)</td></tr></tbody></table>

&#x20;

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Loại ứng dụng</td><td valign="top">Base</td><td valign="top">Endpoint</td></tr><tr><td valign="top">Danh sách Assistants</td><td valign="top">clickai.io</td><td valign="top">GET /assistant/api/assistant/my-assistants</td></tr><tr><td valign="top">Text Generation</td><td valign="top">api.clickai.io</td><td valign="top">POST /v1/completion-messages</td></tr><tr><td valign="top">Chatbot / Agent</td><td valign="top">api.clickai.io</td><td valign="top">POST /v1/chat-messages</td></tr><tr><td valign="top">Workflow</td><td valign="top">api.clickai.io</td><td valign="top">POST /v1/workflows/run</td></tr></tbody></table>

&#x20;

## Xác thực (Authentication)

Mọi API request đều yêu cầu Bearer Token trong header:

Authorization: Bearer {YOUR\_SECRET\_KEY}\
Content-Type: application/json

🛑 CAUTION: \*\*Bảo mật API Key:\*\* - KHÔNG BAO GIỜ đặt API Key trong code client-side (JavaScript frontend) - KHÔNG commit API Key vào Git repository - Luôn gọi API từ backend server - Lưu API Key trong environment variables - Rotate key định kỳ nếu nghi ngờ bị lộ

&#x20;

## API lấy danh sách Assistants

Lấy danh sách các assistants (apps) thuộc workspace của bạn.

### Endpoint

GET <https://clickai.io/assistant/api/assistant/my-assistants>

### Query Parameters

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Parameter</td><td valign="top">Type</td><td valign="top">Required</td><td valign="top">Mô tả</td></tr><tr><td valign="top">page</td><td valign="top">integer</td><td valign="top">❌</td><td valign="top">Trang hiện tại (mặc định: 1)</td></tr><tr><td valign="top">page_size</td><td valign="top">integer</td><td valign="top">❌</td><td valign="top">Số items mỗi trang (mặc định: 10)</td></tr><tr><td valign="top">mode</td><td valign="top">string</td><td valign="top">❌</td><td valign="top">Loại app: workflow, agent-chat, chat, completion</td></tr><tr><td valign="top">workspace_id</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">ID workspace của bạn</td></tr></tbody></table>

&#x20;

### Ví dụ — Lấy danh sách Workflow Apps

curl -G '<https://clickai.io/assistant/api/assistant/my-assistants>' \\\
&#x20; -d 'page=1' \\\
&#x20; -d 'page\_size=10' \\\
&#x20; -d 'mode=workflow' \\\
&#x20; -d 'workspace\_id=\<YOUR\_WORKSPACE\_ID>' \\\
&#x20; -H 'accept: application/json' \\\
&#x20; -H 'authorization: Bearer \<YOUR\_ACCESS\_TOKEN>'

### Ví dụ — Lấy danh sách Agent Chat Apps

curl -G '<https://clickai.io/assistant/api/assistant/my-assistants>' \\\
&#x20; -d 'page=1' \\\
&#x20; -d 'page\_size=10' \\\
&#x20; -d 'mode=agent-chat' \\\
&#x20; -d 'workspace\_id=\<YOUR\_WORKSPACE\_ID>' \\\
&#x20; -H 'accept: application/json' \\\
&#x20; -H 'authorization: Bearer \<YOUR\_ACCESS\_TOKEN>'

### Ví dụ — Python

import requests\
\
url = "<https://clickai.io/assistant/api/assistant/my-assistants"\\>
\
headers = {\
&#x20;   'accept': 'application/json',\
&#x20;   'authorization': 'Bearer YOUR\_ACCESS\_TOKEN',\
}\
\
params = {\
&#x20;   'page': 1,\
&#x20;   'page\_size': 10,\
&#x20;   'mode': 'workflow',\
&#x20;   'workspace\_id': 'YOUR\_WORKSPACE\_ID'\
}\
\
response = requests.get(url, headers=headers, params=params)\
data = response.json()\
\
for app in data.get('data', \[]):\
&#x20;   print(f"App: {app\['name']} (ID: {app\['id']})")

### Response

{\
&#x20; "page": 1,\
&#x20; "page\_size": 10,\
&#x20; "total": 5,\
&#x20; "data": \[\
&#x20;   {\
&#x20;     "id": "app-abc123",\
&#x20;     "name": "Customer Support Bot",\
&#x20;     "mode": "agent-chat",\
&#x20;     "description": "AI hỗ trợ khách hàng",\
&#x20;     "created\_at": 1705395332\
&#x20;   }\
&#x20; ]\
}

💡 TIP: Sử dụng API này để tự động lấy danh sách apps trong workspace, phục vụ cho việc tích hợp MCP hoặc quản lý apps qua script.

&#x20;

## API cho Text Generation App

Dành cho ứng dụng tạo nội dung một lần (single-turn), không lưu lịch sử hội thoại.

### Endpoint

POST <https://api.clickai.io/v1/completion-messages>

### Request Body

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Required</td><td valign="top">Mô tả</td></tr><tr><td valign="top">inputs</td><td valign="top">object</td><td valign="top">✅</td><td valign="top">Biến đầu vào theo cấu hình app</td></tr><tr><td valign="top">response_mode</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">"streaming" hoặc "blocking"</td></tr><tr><td valign="top">user</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">User ID duy nhất để tracking</td></tr><tr><td valign="top">files</td><td valign="top">array</td><td valign="top">❌</td><td valign="top">Files đính kèm (nếu app hỗ trợ)</td></tr></tbody></table>

&#x20;

### Ví dụ — cURL

curl --location --request POST '<https://api.clickai.io/v1/completion-messages>' \\\
&#x20; \--header 'Authorization: Bearer YOUR-SECRET-KEY' \\\
&#x20; \--header 'Content-Type: application/json' \\\
&#x20; \--data-raw '{\
&#x20;   "inputs": {\
&#x20;     "text": "Viết email cảm ơn khách hàng đã mua sản phẩm"\
&#x20;   },\
&#x20;   "response\_mode": "streaming",\
&#x20;   "user": "user-abc-123"\
&#x20; }'

### Ví dụ — Python

import requests\
import json\
\
url = "<https://api.clickai.io/v1/completion-messages"\\>
\
headers = {\
&#x20;   'Authorization': 'Bearer YOUR-SECRET-KEY',\
&#x20;   'Content-Type': 'application/json',\
}\
\
data = {\
&#x20;   "inputs": {\
&#x20;       "text": "Viết email cảm ơn khách hàng đã mua sản phẩm"\
&#x20;   },\
&#x20;   "response\_mode": "streaming",\
&#x20;   "user": "user-abc-123"\
}\
\
response = requests.post(url, headers=headers, data=json.dumps(data))\
print(response.text)

### Ví dụ — JavaScript (Node.js)

const axios = require('axios');\
\
const response = await axios.post(\
&#x20; '<https://api.clickai.io/v1/completion-messages',\\>
&#x20; {\
&#x20;   inputs: { text: 'Viết email cảm ơn khách hàng' },\
&#x20;   response\_mode: 'streaming',\
&#x20;   user: 'user-abc-123'\
&#x20; },\
&#x20; {\
&#x20;   headers: {\
&#x20;     'Authorization': 'Bearer YOUR-SECRET-KEY',\
&#x20;     'Content-Type': 'application/json'\
&#x20;   }\
&#x20; }\
);\
\
console.log(response.data);

### Response (Blocking Mode)

{\
&#x20; "id": "msg-abc123",\
&#x20; "answer": "Kính gửi Quý khách hàng...",\
&#x20; "created\_at": 1705395332,\
&#x20; "metadata": {\
&#x20;   "usage": {\
&#x20;     "prompt\_tokens": 45,\
&#x20;     "completion\_tokens": 128,\
&#x20;     "total\_tokens": 173,\
&#x20;     "total\_price": "0.000346",\
&#x20;     "currency": "USD"\
&#x20;   }\
&#x20; }\
}

&#x20;

## API cho Conversational App

Dành cho Chatbot và Agent — hỗ trợ multi-turn conversation.

### Endpoint

POST <https://api.clickai.io/v1/chat-messages>

### Request Body

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Required</td><td valign="top">Mô tả</td></tr><tr><td valign="top">inputs</td><td valign="top">object</td><td valign="top">❌</td><td valign="top">Biến đầu vào (chỉ cho cuộc hội thoại mới)</td></tr><tr><td valign="top">query</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">Tin nhắn của người dùng</td></tr><tr><td valign="top">response_mode</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">"streaming" hoặc "blocking"</td></tr><tr><td valign="top">conversation_id</td><td valign="top">string</td><td valign="top">❌</td><td valign="top">ID cuộc hội thoại (để trống = tạo mới)</td></tr><tr><td valign="top">user</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">User ID duy nhất</td></tr><tr><td valign="top">files</td><td valign="top">array</td><td valign="top">❌</td><td valign="top">Files đính kèm</td></tr></tbody></table>

&#x20;

### Ví dụ — cURL

curl --location --request POST '<https://api.clickai.io/v1/chat-messages>' \\\
&#x20; \--header 'Authorization: Bearer YOUR-SECRET-KEY' \\\
&#x20; \--header 'Content-Type: application/json' \\\
&#x20; \--data-raw '{\
&#x20;   "inputs": {},\
&#x20;   "query": "Xin chào, tôi cần hỗ trợ về sản phẩm",\
&#x20;   "response\_mode": "streaming",\
&#x20;   "conversation\_id": "",\
&#x20;   "user": "user-abc-123"\
&#x20; }'

### Ví dụ — Python

import requests\
import json\
\
url = "<https://api.clickai.io/v1/chat-messages"\\>
\
headers = {\
&#x20;   'Authorization': 'Bearer YOUR-SECRET-KEY',\
&#x20;   'Content-Type': 'application/json',\
}\
\
data = {\
&#x20;   "inputs": {},\
&#x20;   "query": "Xin chào, tôi cần hỗ trợ về sản phẩm",\
&#x20;   "response\_mode": "streaming",\
&#x20;   "conversation\_id": "",\
&#x20;   "user": "user-abc-123"\
}\
\
response = requests.post(url, headers=headers, data=json.dumps(data))\
print(response.text)

### Response (Blocking Mode)

{\
&#x20; "id": "msg-abc456",\
&#x20; "conversation\_id": "conv-xyz789",\
&#x20; "answer": "Xin chào! Tôi có thể giúp gì cho bạn...",\
&#x20; "created\_at": 1705395332,\
&#x20; "metadata": {\
&#x20;   "usage": {\
&#x20;     "prompt\_tokens": 56,\
&#x20;     "completion\_tokens": 89,\
&#x20;     "total\_tokens": 145\
&#x20;   }\
&#x20; }\
}

&#x20;

## API cho Workflow App

Dành cho Workflow — batch processing, pipeline execution.

### Endpoint

POST <https://api.clickai.io/v1/workflows/run>

### Request Body

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Required</td><td valign="top">Mô tả</td></tr><tr><td valign="top">inputs</td><td valign="top">object</td><td valign="top">✅</td><td valign="top">Biến đầu vào cho workflow</td></tr><tr><td valign="top">response_mode</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">"streaming" hoặc "blocking"</td></tr><tr><td valign="top">user</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">User ID duy nhất</td></tr></tbody></table>

&#x20;

### Ví dụ — cURL

curl --location --request POST '<https://api.clickai.io/v1/workflows/run>' \\\
&#x20; \--header 'Authorization: Bearer YOUR-SECRET-KEY' \\\
&#x20; \--header 'Content-Type: application/json' \\\
&#x20; \--data-raw '{\
&#x20;   "inputs": {\
&#x20;     "input\_text": "Phân tích dữ liệu bán hàng Q4 2024"\
&#x20;   },\
&#x20;   "response\_mode": "streaming",\
&#x20;   "user": "user-abc-123"\
&#x20; }'

### Response (Blocking Mode)

{\
&#x20; "workflow\_run\_id": "wf-run-abc123",\
&#x20; "task\_id": "task-xyz789",\
&#x20; "data": {\
&#x20;   "id": "wf-abc123",\
&#x20;   "workflow\_id": "wf-def456",\
&#x20;   "status": "succeeded",\
&#x20;   "outputs": {\
&#x20;     "result": "Kết quả phân tích..."\
&#x20;   },\
&#x20;   "total\_tokens": 456,\
&#x20;   "created\_at": 1705395332,\
&#x20;   "finished\_at": 1705395345\
&#x20; }\
}

&#x20;

## Quản lý conversation\_id

conversation\_id là khóa quan trọng nhất khi làm việc với Conversational Apps.

### Quy tắc chi tiết

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Tình huống</td><td valign="top">Xử lý</td><td valign="top">Ghi chú</td></tr><tr><td valign="top">Bắt đầu hội thoại mới</td><td valign="top">conversation_id: "" (để trống)</td><td valign="top">Hệ thống tạo ID mới và trả về trong response</td></tr><tr><td valign="top">Tiếp tục hội thoại</td><td valign="top">conversation_id: "conv-xyz789"</td><td valign="top">Sử dụng ID từ response trước</td></tr><tr><td valign="top">Truyền inputs giữa session</td><td valign="top">Dùng Conversation Variables</td><td valign="top">inputs bị bỏ qua khi có conversation_id</td></tr></tbody></table>

&#x20;

### Ví dụ flow hoàn chỉnh

import requests\
import json\
\
BASE\_URL = "<https://api.clickai.io/v1"\\>
HEADERS = {\
&#x20;   'Authorization': 'Bearer YOUR-SECRET-KEY',\
&#x20;   'Content-Type': 'application/json',\
}\
\
\# === Lượt 1: Bắt đầu hội thoại mới ===\
response1 = requests.post(f"{BASE\_URL}/chat-messages", headers=HEADERS,\
&#x20;   data=json.dumps({\
&#x20;       "inputs": {"language": "vi"},\
&#x20;       "query": "Xin chào, tôi muốn hỏi về bảo hành",\
&#x20;       "response\_mode": "blocking",\
&#x20;       "conversation\_id": "",  # Để trống → tạo mới\
&#x20;       "user": "user-001"\
&#x20;   })\
)\
data1 = response1.json()\
conv\_id = data1\["conversation\_id"]  # Lưu lại conversation\_id\
print(f"Bot: {data1\['answer']}")\
print(f"Conversation ID: {conv\_id}")\
\
\# === Lượt 2: Tiếp tục hội thoại ===\
response2 = requests.post(f"{BASE\_URL}/chat-messages", headers=HEADERS,\
&#x20;   data=json.dumps({\
&#x20;       "inputs": {},\
&#x20;       "query": "Sản phẩm XYZ có bảo hành bao lâu?",\
&#x20;       "response\_mode": "blocking",\
&#x20;       "conversation\_id": conv\_id,  # Truyền ID để tiếp tục\
&#x20;       "user": "user-001"\
&#x20;   })\
)\
data2 = response2.json()\
print(f"Bot: {data2\['answer']}")\
\
\# === Lượt 3: Tiếp tục ===\
response3 = requests.post(f"{BASE\_URL}/chat-messages", headers=HEADERS,\
&#x20;   data=json.dumps({\
&#x20;       "inputs": {},\
&#x20;       "query": "Cảm ơn bạn!",\
&#x20;       "response\_mode": "blocking",\
&#x20;       "conversation\_id": conv\_id,\
&#x20;       "user": "user-001"\
&#x20;   })\
)\
data3 = response3.json()\
print(f"Bot: {data3\['answer']}")

🚨 WARNING: Khi truyền \`conversation\_id\` đã tồn tại, mọi giá trị trong \`inputs\` sẽ bị bỏ qua. Chỉ \`query\` được xử lý. Nếu cần thay đổi biến giữa session, hãy sử dụng \*\*Conversation Variables\*\*.

&#x20;

## Streaming vs Blocking Response

### Blocking Mode

·       Response trả về toàn bộ kết quả khi xử lý xong

·       Phù hợp cho: Backend processing, batch jobs

·       Timeout mặc định: 60 giây

{\
&#x20; "response\_mode": "blocking"\
}

### Streaming Mode (Khuyến nghị)

·       Response trả về từng phần qua Server-Sent Events (SSE)

·       Phù hợp cho: Real-time chat, UX tốt hơn

·       Không giới hạn thời gian xử lý

{\
&#x20; "response\_mode": "streaming"\
}

Xử lý SSE trong JavaScript:

const eventSource = new EventSource(\
&#x20; '<https://api.clickai.io/v1/chat-messages',\\>
&#x20; {\
&#x20;   method: 'POST',\
&#x20;   headers: {\
&#x20;     'Authorization': 'Bearer YOUR-SECRET-KEY',\
&#x20;     'Content-Type': 'application/json'\
&#x20;   },\
&#x20;   body: JSON.stringify({\
&#x20;     inputs: {},\
&#x20;     query: 'Xin chào',\
&#x20;     response\_mode: 'streaming',\
&#x20;     user: 'user-001'\
&#x20;   })\
&#x20; }\
);\
\
// Hoặc dùng fetch API\
const response = await fetch('<https://api.clickai.io/v1/chat-messages>', {\
&#x20; method: 'POST',\
&#x20; headers: {\
&#x20;   'Authorization': 'Bearer YOUR-SECRET-KEY',\
&#x20;   'Content-Type': 'application/json'\
&#x20; },\
&#x20; body: JSON.stringify({\
&#x20;   inputs: {},\
&#x20;   query: 'Xin chào',\
&#x20;   response\_mode: 'streaming',\
&#x20;   conversation\_id: '',\
&#x20;   user: 'user-001'\
&#x20; })\
});\
\
const reader = response.body.getReader();\
const decoder = new TextDecoder();\
\
while (true) {\
&#x20; const { done, value } = await reader.read();\
&#x20; if (done) break;\
\
&#x20; const chunk = decoder.decode(value);\
&#x20; const lines = chunk.split('\n');\
\
&#x20; for (const line of lines) {\
&#x20;   if (line.startsWith('data: ')) {\
&#x20;     const data = JSON.parse(line.slice(6));\
&#x20;     if (data.event === 'message') {\
&#x20;       process.stdout.write(data.answer);\
&#x20;     }\
&#x20;   }\
&#x20; }\
}

### SSE Event Types

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Event</td><td valign="top">Mô tả</td></tr><tr><td valign="top">message</td><td valign="top">Phần nội dung phản hồi (text chunk)</td></tr><tr><td valign="top">agent_message</td><td valign="top">Phản hồi từ Agent mode</td></tr><tr><td valign="top">agent_thought</td><td valign="top">Quá trình suy luận của Agent</td></tr><tr><td valign="top">message_file</td><td valign="top">File được tạo bởi AI</td></tr><tr><td valign="top">message_end</td><td valign="top">Kết thúc tin nhắn, bao gồm metadata</td></tr><tr><td valign="top">tts_message</td><td valign="top">Audio chunk (nếu bật TTS)</td></tr><tr><td valign="top">message_replace</td><td valign="top">Nội dung thay thế (content moderation)</td></tr><tr><td valign="top">error</td><td valign="top">Lỗi xảy ra trong stream</td></tr><tr><td valign="top">ping</td><td valign="top">Keep-alive signal</td></tr></tbody></table>

&#x20;

&#x20;

## File Upload API

Upload file để sử dụng trong conversation:

### Endpoint

POST <https://api.clickai.io/v1/files/upload>

### Request (multipart/form-data)

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Mô tả</td></tr><tr><td valign="top">file</td><td valign="top">file</td><td valign="top">File cần upload</td></tr><tr><td valign="top">user</td><td valign="top">string</td><td valign="top">User ID</td></tr></tbody></table>

&#x20;

### Ví dụ — cURL

curl --location --request POST '<https://api.clickai.io/v1/files/upload>' \\\
&#x20; \--header 'Authorization: Bearer YOUR-SECRET-KEY' \\\
&#x20; \--form 'file=@"/path/to/document.pdf"' \\\
&#x20; \--form 'user="user-abc-123"'

### Response

{\
&#x20; "id": "file-abc123",\
&#x20; "name": "document.pdf",\
&#x20; "size": 1024000,\
&#x20; "extension": "pdf",\
&#x20; "mime\_type": "application/pdf",\
&#x20; "created\_by": "user-abc-123",\
&#x20; "created\_at": 1705395332\
}

### Sử dụng file trong chat

{\
&#x20; "inputs": {},\
&#x20; "query": "Tóm tắt nội dung file này",\
&#x20; "response\_mode": "streaming",\
&#x20; "conversation\_id": "",\
&#x20; "user": "user-abc-123",\
&#x20; "files": \[\
&#x20;   {\
&#x20;     "type": "document",\
&#x20;     "transfer\_method": "local\_file",\
&#x20;     "upload\_file\_id": "file-abc123"\
&#x20;   }\
&#x20; ]\
}

&#x20;

## Feedback API

Thu thập feedback người dùng cho từng tin nhắn:

### Endpoint

POST <https://api.clickai.io/v1/messages/{message\\_id}/feedbacks>

### Request Body

{\
&#x20; "rating": "like",\
&#x20; "user": "user-abc-123",\
&#x20; "content": "Câu trả lời rất hữu ích!"\
}

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Values</td><td valign="top">Mô tả</td></tr><tr><td valign="top">rating</td><td valign="top">"like", "dislike", null</td><td valign="top">Đánh giá (null = hủy đánh giá)</td></tr><tr><td valign="top">content</td><td valign="top">string</td><td valign="top">Comment bổ sung (optional)</td></tr></tbody></table>

&#x20;

&#x20;

## Conversations API

### Lấy danh sách conversations

GET <https://api.clickai.io/v1/conversations?user=user-abc-123\\&limit=20>

### Lấy lịch sử tin nhắn

GET <https://api.clickai.io/v1/messages?conversation\\_id=conv-xyz789\\&user=user-abc-123\\&limit=20>

### Đổi tên conversation

POST <https://api.clickai.io/v1/conversations/{conversation\\_id}/name>

{\
&#x20; "name": "Hỗ trợ bảo hành sản phẩm XYZ",\
&#x20; "auto\_generate": false,\
&#x20; "user": "user-abc-123"\
}

### Xóa conversation

DELETE <https://api.clickai.io/v1/conversations/{conversation\\_id}>

&#x20;

## Error Handling

### HTTP Status Codes

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Code</td><td valign="top">Mô tả</td></tr><tr><td valign="top">200</td><td valign="top">Thành công</td></tr><tr><td valign="top">400</td><td valign="top">Bad Request — Request không hợp lệ</td></tr><tr><td valign="top">401</td><td valign="top">Unauthorized — API Key sai hoặc hết hạn</td></tr><tr><td valign="top">403</td><td valign="top">Forbidden — Không có quyền truy cập</td></tr><tr><td valign="top">404</td><td valign="top">Not Found — Resource không tồn tại</td></tr><tr><td valign="top">429</td><td valign="top">Too Many Requests — Vượt quá rate limit</td></tr><tr><td valign="top">500</td><td valign="top">Internal Server Error — Lỗi server</td></tr></tbody></table>

&#x20;

### Error Response Format

{\
&#x20; "code": "invalid\_param",\
&#x20; "message": "The 'query' field is required",\
&#x20; "status": 400\
}

### Xử lý lỗi — Python

import requests\
import json\
\
try:\
&#x20;   response = requests.post(url, headers=headers, data=json.dumps(data))\
&#x20;   response.raise\_for\_status()\
&#x20;   result = response.json()\
&#x20;   print(result\["answer"])\
\
except requests.exceptions.HTTPError as e:\
&#x20;   if e.response.status\_code == 401:\
&#x20;       print("API Key không hợp lệ")\
&#x20;   elif e.response.status\_code == 429:\
&#x20;       print("Quá nhiều requests, thử lại sau")\
&#x20;   else:\
&#x20;       error = e.response.json()\
&#x20;       print(f"Lỗi: {error.get('message', 'Unknown error')}")\
\
except requests.exceptions.ConnectionError:\
&#x20;   print("Không thể kết nối đến ClickAI API")

&#x20;

## Rate Limits

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Gói dịch vụ</td><td valign="top">Requests/phút</td><td valign="top">Requests/ngày</td></tr><tr><td valign="top">Sandbox</td><td valign="top">100</td><td valign="top">10,000</td></tr><tr><td valign="top">Professional</td><td valign="top">500</td><td valign="top">100,000</td></tr><tr><td valign="top">Team</td><td valign="top">1,000</td><td valign="top">500,000</td></tr><tr><td valign="top">Enterprise</td><td valign="top">Custom</td><td valign="top">Custom</td></tr></tbody></table>

&#x20;

📝 NOTE: Khi vượt rate limit, API trả về \`429 Too Many Requests\`. Implement retry logic với exponential backoff.

&#x20;

## SDK & Libraries

### Official SDKs

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Ngôn ngữ</td><td valign="top">Package</td><td valign="top">Install</td></tr><tr><td valign="top">Python</td><td valign="top">clickai-sdk</td><td valign="top">pip install clickai-sdk</td></tr><tr><td valign="top">JavaScript</td><td valign="top">@clickai/sdk</td><td valign="top">npm install @clickai/sdk</td></tr></tbody></table>

&#x20;

### Python SDK

from clickai import ClickAI\
\
client = ClickAI(api\_key="YOUR-SECRET-KEY")\
\
\# Chat\
response = client.chat.create(\
&#x20;   query="Xin chào",\
&#x20;   user="user-001"\
)\
print(response.answer)\
\
\# Streaming\
for chunk in client.chat.create(\
&#x20;   query="Viết một bài thơ",\
&#x20;   user="user-001",\
&#x20;   stream=True\
):\
&#x20;   print(chunk.answer, end="")

### JavaScript SDK

import { ClickAI } from '@clickai/sdk';\
\
const client = new ClickAI({ apiKey: 'YOUR-SECRET-KEY' });\
\
// Chat\
const response = await client.chat.create({\
&#x20; query: 'Xin chào',\
&#x20; user: 'user-001'\
});\
console.log(response.answer);\
\
// Streaming\
const stream = await client.chat.create({\
&#x20; query: 'Viết một bài thơ',\
&#x20; user: 'user-001',\
&#x20; stream: true\
});\
\
for await (const chunk of stream) {\
&#x20; process.stdout.write(chunk.answer);\
}

&#x20;

*📖 Xem thêm: \[MCP Server]\(./02-mcp-server.md) · \[Build]\(../01-build.md) · \[Publish]\(../02-publish.md)*


---

# 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.clickai.vn/clickai-docs/nha-phat-trien/tich-hop-clickai-qua-restful-api.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.
