跳转到内容

HTTP API

Plystra Core 暴露 /api/v1 HTTP API。当前文档站发布生成的 OpenAPI 文件:

/openapi/plystra.v0.0.1.yaml
/openapi/plystra.v0.0.1.json

这些文件由当前 Go API contract 通过 swaggest/openapi-go 生成,包含 request bodies、response envelopes、security schemes、endpoint tags 和 route groups。

如果你的目标是把现有应用接进来,而不是逐个浏览 endpoint,先看 接入你的应用。本页是操作层 API 指南。

单对象成功响应:

{
"data": {},
"request_id": "req_..."
}

列表响应:

{
"data": [],
"pagination": {
"limit": 50,
"cursor": null,
"has_more": false
},
"request_id": "req_..."
}

错误响应:

{
"error": {
"code": "VALIDATION_FAILED",
"message": "Request body is invalid JSON.",
"details": null,
"request_id": "req_..."
},
"request_id": "req_..."
}

API 接受 X-Request-ID。未提供时,middleware 会生成 request ID。

Layer适用范围机制
Public operationshealth、ready、version不需要 token。
Session authlogin、refresh、logout、actor context、switch-memberOpaque bearer tokens;以 HMAC hash 存储。
Admin grant protection非公开 Core APIAuthorization: Bearer <access_token>,User 需要 active AdminGrant。
API key protection服务端到服务端 Core APIX-Plystra-API-Key: <api_key>Authorization: Bearer ply_ak_...;key 携带 scope 和 permission keys。
Metrics token启用后的 /metricsMETRICS_TOKEN / PLYSTRA_METRICS_TOKEN,或拥有 metrics:read 的 Bearer session。

受保护路由没有有效凭证时返回 AUTHENTICATION_REQUIRED。凭证有效但缺少权限时返回 ADMIN_PERMISSION_REQUIRED

MethodPath
GET/api/v1/health
GET/api/v1/ready
GET/api/v1/version

/metrics 会进入 public route 分支,让 handler 返回 FEATURE_DISABLED 或验证 metrics token。Metrics 默认关闭。

MethodPath说明
POST/api/v1/auth/register受保护 native registration。默认关闭。
POST/api/v1/auth/login接收 emailpassword;返回 access 和 refresh tokens。
POST/api/v1/auth/refresh接收 refresh_token;轮换 access 和 refresh tokens。
POST/api/v1/auth/logout通过 bearer access token 或 body refresh token 撤销。
GET/api/v1/actor/context需要 access token。返回 active actor 和 available members。
POST/api/v1/actor/switch-member需要 access token。切换 active Member/UserMember binding。

Core 有意只保留最小认证面:受保护注册、密码登录、session refresh/logout 和 actor context。Email verification code 和 magic-link sign-in 位于独立 Complete Auth plugin repo。该插件发送邮件时,依赖独立 email contracts repo 和 SMTP 或 Cloudflare Email Sending 等 provider plugin。

普通 Core 注册会在单个 Simple Mode 默认 Space space_default 内创建 User、default Member、default UserMember、session 和 Space admin grant。它不会创建 instance super admin。公开 user-only Core 注册更窄,只创建 User。

本地 demo 凭据:

[email protected] / plystra-demo
[email protected] / plystra-demo

这些路由属于独立 Complete Auth plugin,不属于 Core:

MethodPath说明
POST/api/v1/auth/email-code创建短生命周期 email verification-code challenge,并通过配置的 email provider 发送。
POST/api/v1/auth/email-code/verify校验 6 位 code,消费 challenge;当绑定 active User 时标记 User email metadata 已验证。
POST/api/v1/auth/magic-link创建短生命周期 magic-link challenge,并通过配置的 email provider 发送。
POST/api/v1/auth/magic-link/consume消费 magic-link token,并为 active User 创建 Core-compatible session。

Plugin challenges 都是一次性的。插件只保存已发送 code/token 的 HMAC hash。发送和验证尝试会按 normalized email 和 source IP 限流。生产必须使用外部 email capability endpoint;development log mode 在生产会被拒绝。Magic-link redirect_url 必须使用 HTTPS 并匹配插件 allowlist。

Complete Auth 的非敏感运行时设置保存在 plugin_auth_settings,包括 public registration、delivery mode、capability URL、sender address、redirect allowlist、TTL、rate limits、max body size 和 trusted proxy CIDRs。Secrets 保持环境变量或 secret-manager values。

MethodPath说明
POST/api/v1/authz/check执行决策并写 audit。
POST/api/v1/authz/explain执行相同决策路径,并返回完整 explain output。

Managed-mode request 示例:

Terminal window
curl -X POST http://localhost:8080/api/v1/authz/check \
-H "Content-Type: application/json" \
-H "X-Plystra-API-Key: $PLYSTRA_API_KEY" \
-d '{
"actor": {
"user_id": "user_alice",
"member_id": "member_finance_reviewer",
"user_member_id": "um_alice_finance_reviewer",
"space_id": "space_acme"
},
"resource_type": "invoice",
"resource_id": "invoice_001",
"action": "approve"
}'

HTTP authz 会忽略 body 中的 canonical request metadata,例如 request_idipuser_agent。Core 从请求和 middleware 派生这些值。

API key 调用必须包含嵌套 actor。Bearer session 调用可以省略 actor;Core 会使用 session active actor。

以下 route groups 都需要 Authorization: Bearer <access_token> 加 active AdminGrant,或 X-Plystra-API-Key: <api_key> 加匹配 permission keys。

GroupRoutes
Admin grantsGET /api/v1/admin/meGET/POST /api/v1/admin/grantsGET /api/v1/admin/grants/{id}POST /api/v1/admin/grants/{id}/revoke
API keysGET/POST /api/v1/api-keysGET /api/v1/api-keys/{id}POST /api/v1/api-keys/{id}/revoke
OverviewGET /api/v1/console/overview
AuditLogGET /api/v1/audit/logsGET /api/v1/audit/logs/{id} 和 Space-scoped variants
Resource RegistryGET/POST /api/v1/resource-types、resource type detail、actions 和 mapping routes
UsersGET/POST /api/v1/users、detail、update、disable、restore
SpacesGET/POST /api/v1/spaces、detail、update、disable、restore
Space nested objects/api/v1/spaces/{space_id}/groups/members/user-members/roles/member-roles/resources/audit-logs
Direct detailsGET /api/v1/groups/{id}GET /api/v1/members/{id}GET /api/v1/user-members/{id}GET /api/v1/roles/{id}
PermissionsGET/POST /api/v1/permissions、detail/update/disable,以及 role-permissions binding routes
ResourcesGET/POST /api/v1/resourcesGET /api/v1/resources/{resource_type}/{resource_id}
Plugin metadata previewmanifest validation、metadata install、lifecycle flags、settings、resources、permissions、audit events、admin menus
Template metadata previewcatalog、detail、preview-install、install metadata routes

完整 path list 见 Core API 参考。Route permission matrix 和防提权规则见 Admin Auth 与安全边界

创建 API key 时明文 api_key 只返回一次。Core 只保存 HMAC hash。

Data Console 不是稳定 Core blocking surface,默认关闭:

DATA_CONSOLE_ENABLED=false

显式启用并通过 Bearer session/API key 权限保护后:

MethodPath
GET/api/v1/data/tables
GET/POST/api/v1/data/rows/{resource_type}
GET/PATCH/DELETE/api/v1/data/rows/{resource_type}/{resource_id}

当前 mutations 支持通过 internal_table resource mappings 访问 Core resources 表。Mutations 会运行授权检查,并返回变更后的 row 和 authorization decision。

Metrics 默认关闭:

METRICS_ENABLED=false

启用后,/metrics 返回 Prometheus text,并要求 metrics token 或拥有 metrics:read 的 Bearer session。

Core 中的 plugin 和 template HTTP routes 是 preview metadata surfaces。它们不表示 stable hot-loaded plugin runtime、third-party marketplace、cloud hosting 或完整 capability certification system 已经实现。Backend OS Alpha 使用 plystractl templates create 作为可检查 scaffold path;HTTP template routes 仍用于 manifest、preview 和 admin metadata APIs。