> For the complete documentation index, see [llms.txt](https://docs.maiagent.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.maiagent.ai/tech/api-integration/api-conventions.md).

# API 共通規範

呼叫 MaiAgent 平台 API 的共通規則摘要：網址與版本前綴、API Key 認證、速率限制、錯誤回應、分頁、欄位格式與上傳大小上限；正本在 API 文件

API 跨端點的共通規則以 API 文件的 [API 共通規範](https://docs.maiagent.ai/api/preparation/conventions) 為正本，開發者串接時請以該頁為準。本頁只留各節的一兩句摘要，每節都連到 API 文件的同名段落。

{% hint style="info" %}
各端點的欄位與範例見 [API 文件站](https://docs.maiagent.ai/api)；建立與管理 API Key 的畫面操作見使用者手冊 [API Key 管理](https://docs.maiagent.ai/org/developer/api-keys)。
{% endhint %}

## 網址與版本前綴 <a href="#base-url" id="base-url"></a>

SaaS 的 API 網址是 `https://api.maiagent.ai/api/v1/`，地端與私有雲換成貴公司的平台網域；新的整合請一律使用 `/api/v1/`。詳見 [API 共通規範：網址與版本前綴](https://docs.maiagent.ai/api/preparation/conventions#base-url)。

## 認證 <a href="#authentication" id="authentication"></a>

伺服器對伺服器的呼叫在 `Authorization` 標頭帶 `Api-Key <金鑰>`；金鑰綁定一位成員，權限等於該成員的角色。失效條件、認證失敗的回應與整合用金鑰的建議，見 [API 共通規範：認證](https://docs.maiagent.ai/api/preparation/conventions#authentication)。

每位成員可以同時持有多把有效金鑰，沒有數量上限（見使用者手冊 [API Key 管理](https://docs.maiagent.ai/org/developer/api-keys#key-concepts)）。金鑰也可以用 API 簽發：管理者代發用 `POST /api/v1/organizations/{organizationPk}/api-keys/`（需要「API Key 管理權限」，可設定名稱、到期時間與 RPM），成員自建用 `POST /api/v1/organizations/{organizationPk}/my-api-keys/`；撤銷用對應的 `DELETE`。完整金鑰只在建立的回應中出現一次。欄位見 API 文件[組織和成員](https://docs.maiagent.ai/api/api-reference/zu-zhi-he-cheng-yuan)。

## 速率限制 <a href="#rate-limits" id="rate-limits"></a>

平台 API 沒有全域的預設上限，主要的限制是每把 API Key 的 RPM 與 AI Gateway 的 TPM；超過上限回 `429` 並帶 `Retry-After`。完整的限流範圍見 [API 共通規範：速率限制](https://docs.maiagent.ai/api/preparation/conventions#rate-limits)。

## 錯誤回應 <a href="#errors" id="errors"></a>

請依 HTTP 狀態碼與 `code` 判斷錯誤，不要比對 `detail` 文字。錯誤內容的形狀、各狀態碼的處理建議與 `402` 的情況，見 [API 共通規範：錯誤回應](https://docs.maiagent.ai/api/preparation/conventions#errors)。

## 分頁 <a href="#pagination" id="pagination"></a>

多數列表用 `page`／`page_size` 頁碼分頁（預設 20、上限 100），以 `next` 取下一頁；對話與訊息列表用游標分頁。詳見 [API 共通規範：分頁](https://docs.maiagent.ai/api/preparation/conventions#pagination)。

## 欄位格式 <a href="#field-format" id="field-format"></a>

回應的 key 一律是 camelCase，時間欄位多為毫秒時間戳記字串。詳見 [API 共通規範：欄位格式](https://docs.maiagent.ai/api/preparation/conventions#field-format)。

## 上傳大小上限 <a href="#upload-limits" id="upload-limits"></a>

上傳上限以 `GET /api/v1/allowed-file-specs/` 的回應為準，各用途的現值見 [API 共通規範：上傳大小上限](https://docs.maiagent.ai/api/preparation/conventions#upload-limits)。Presigned 上傳流程見 [Presigned 檔案上傳模式](/tech/api-integration/api_knowledge.md)。

## 請求內容的編碼 <a href="#encoding" id="encoding"></a>

請求與回應的 JSON 一律是 UTF-8。角色指令（`instructions`）、訊息內容等欄位可以直接放中文與其他 Unicode 字元，不需要轉成 ASCII 或跳脫；請在請求帶 `Content-Type: application/json`，並確認您的 HTTP 用戶端以 UTF-8 編碼送出。回應的 JSON 也以 UTF-8 原文回傳，不會轉成 `\uXXXX`。更新後用對應的 `GET` 讀回即可確認儲存的內容；後台畫面顯示的是同一份資料。

## 請求大小與 413 <a href="#request-size" id="request-size"></a>

* 平台 API 本身不會回 `413`。JSON 請求本體超過 Django 的預設上限（2.5 MB）時回 `400`；這個上限只算 JSON 與表單欄位，不算 multipart 的檔案本體。
* 檔案大小由 `GET /api/v1/allowed-file-specs/` 的上限決定，超過時在取得上傳網址或登記檔案的步驟回 `400`，見[上傳大小上限](#upload-limits)。
* 收到 `413` 代表整個請求被平台前方的代理層（CDN／負載平衡）擋下，通常是把檔案以 Base64 放進 JSON，或直接以 multipart 送大檔。請改用 [Presigned 檔案上傳模式](/tech/api-integration/api_knowledge.md)：檔案直接傳到儲存空間，API 請求只帶檔案的 key。

## 常見問題 <a href="#faq" id="faq"></a>

金鑰一直回 `401`、能不能限制金鑰的呼叫範圍、`/api/` 與 `/api/v1/` 的差別，見 [API 共通規範：常見問題](https://docs.maiagent.ai/api/preparation/conventions#faq)。


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.maiagent.ai/tech/api-integration/api-conventions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
