# API 概覽

> 透過 REST 和 GraphQL 自動化 PentAGI — 建立 Bearer token、從 CI/CD 啟動 flows、將結果提取到您自己的工具中，並產生用戶端。

您在網頁 UI 中能執行的多數操作，皆可透過 API 完成——從 CI/CD 啟動 [flows](https://pentagi.com/zh-tw/docs/glossary/#flow)、將結果匯入您自己的工具，或建立整合。

| API     | 端點                | 互動式文件                        |
| ------- | ----------------- | ---------------------------- |
| GraphQL | `/api/v1/graphql` | `/api/v1/graphql/playground` |
| REST    | `/api/v1/...`     | `/api/v1/swagger/index.html` |

## [建立 token](https://pentagi.com/zh-tw/docs/api/overview/#create-a-token)

1. 在網頁 UI 中開啟 **Settings → API Tokens**。
2. 點擊 **Create Token**，可選擇為其命名，並設定介於 1 分鐘至 3 年之間的有效期。
3. 請立即複製 token——它僅會顯示一次。

Token 隸屬於您的帳號，並繼承您角色所擁有的權限。使用者、角色與 API token、flow [template](https://pentagi.com/zh-tw/docs/glossary/#template)、收藏夾以及您的設定，僅能從已登入的瀏覽器工作階段進行管理：token 在 REST `/users`、`/roles` 和 `/tokens` 路徑上會收到 403 錯誤，並在這些 GraphQL 操作中收到錯誤訊息。

## [呼叫 API](https://pentagi.com/zh-tw/docs/api/overview/#call-the-api)

```bash
curl -X POST https://your-pentagi-host:8443/api/v1/graphql \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"query": "{ flows { id title status } }"}'

curl https://your-pentagi-host:8443/api/v1/flows/ \
  -H "Authorization: Bearer YOUR_API_TOKEN"
```

使用 GraphQL mutation 啟動 flow：

```graphql
mutation CreateFlow {
  createFlow(modelProvider: "openai", input: "Test the security of https://example.com") {
    id
    title
    status
    createdAt
  }
}
```

## [產生用戶端](https://pentagi.com/zh-tw/docs/api/overview/#generate-a-client)

* **GraphQL** — schema 位於儲存庫中的 `backend/pkg/graph/schema.graphqls`；可使用 GraphQL Code Generator、genqlient 或 Apollo。
* **REST** — OpenAPI 規格提供於 `/api/v1/swagger/doc.json`：

```bash
openapi-generator-cli generate \
  -i https://your-pentagi-host:8443/api/v1/swagger/doc.json \
  -g python \
  -o ./pentagi-client
```