Files
Auto_Bangumi/docs/en/dev/plugins/points/api-router.md
Estrella Pan 3258fcca4b docs(4.0-P7): 插件开发文档拆分为总览加每个扩展点一页,补齐中英日三语
- docs/dev/plugins.md 改为总览与导航,旧内容拆入 docs/dev/plugins/
- 新增核心概念、配置表单、事件、前端挂载点、命令行、签名与分发、内置插件、示例页
- 每个扩展点一页,侧边栏由页面表生成
- 改正旧文档中下载器配置、hardlink path_map 与补链按钮的过时描述
2026-10-08 14:51:00 +02:00

25 lines
1.3 KiB
Markdown

# REST Routes (api_router)
Adds HTTP endpoints to a plugin. They are often the backend of a frontend component. `@provider(points.API_ROUTER, id=...)` returns a `fastapi.APIRouter`.
```python
from fastapi import APIRouter
@provider(points.API_ROUTER, id="api")
def api(self):
router = APIRouter()
@router.get("/stats")
async def stats():
return {"count": await self.ctx.kv.get("count", 0)}
return router
```
- AB mounts the routes under `/api/v1/plugins/<plugin id>/`. The example above is `GET /api/v1/plugins/my-plugin/stats`. The `id` must be unique only inside the plugin.
- **All plugin routes require a login.** They use the same credentials as the other WebUI endpoints (a session cookie or an API token with `scope=api`). A plugin cannot register an anonymous endpoint. A request without a login gets 401.
- Routes appear when the plugin is enabled and disappear when it is disabled. No restart is necessary. One plugin can provide several `API_ROUTER` items. AB merges them under the same prefix.
- A handler that raises `HTTPException` returns that status code. Any other exception returns 500 and counts toward the breaker.
- Plugin routes are not in the OpenAPI document at `/docs`.
- Reserved paths: `web/` (frontend static files), `install` and `catalog` (see [Signing and distribution](/en/dev/plugins/signing)) cannot be route paths.