# API 概览

> 通过 REST 和 GraphQL 自动化 PentAGI — 创建 Bearer 令牌，从 CI/CD 启动 flows，将结果拉取到您自己的工具中并生成客户端。

您在 Web UI 中可以执行的大部分操作均可通过 API 实现——从 CI/CD 启动 [flows](https://pentagi.com/zh-cn/docs/glossary/#flow)、将结果拉取到您自己的工具中，或构建集成。

| API     | 端点                | 交互式文档                        |
| ------- | ----------------- | ---------------------------- |
| GraphQL | `/api/v1/graphql` | `/api/v1/graphql/playground` |
| REST    | `/api/v1/...`     | `/api/v1/swagger/index.html` |

## [创建令牌](https://pentagi.com/zh-cn/docs/api/overview/#create-a-token)

1. 在 Web UI 中打开 **Settings → API Tokens**。
2. 点击 **Create Token**，可选择为其命名，并设置 1 分钟至 3 年之间的有效期。
3. 请立即复制令牌——它仅显示一次。

令牌归属于您的账户，并继承您角色的权限。用户、角色和 API 令牌、flow 模板、收藏夹以及您的设置仅能从已登录的浏览器会话进行管理：令牌在 REST `/users`、`/roles` 和 `/tokens` 路由上会收到 403 错误，并在这些 GraphQL 操作中收到错误。

## [调用 API](https://pentagi.com/zh-cn/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 变更启动 flow：

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

## [生成客户端](https://pentagi.com/zh-cn/docs/api/overview/#generate-a-client)

* **GraphQL** — 模式位于代码仓库中的 `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
```