Repository navigation
zh dev star guides plugin views web api
独立插件页面和常驻工作台页面通过 Bridge 调用这些接口。消息组件与消息创建的工作台页面使用 @filter.ui_action,见消息交互组件。
接口按当前登录用户鉴权。业务数据的归属和权限仍由插件校验;涉及会话时,根据 request.username 验证会话归属,不能只信任前端提交的 session_id。
使用 context.register_web_api(route, view_handler, methods, desc) 注册插件 API。
context.register_web_api(
f"/{PLUGIN_NAME}/items/<item_id>",
self.get_item,
["GET"],
"Get item",
)路由需要包含插件名作为前缀。视图端的 bridge endpoint 不需要包含插件名:
await bridge.apiGet("items/123");Dashboard 会把它转发到:
/api/v1/plugins/extensions/<plugin_name>/items/123
注册路由 /<plugin_name>/items/<item_id> 会匹配该请求,item_id 作为 handler 的关键字参数传入:
async def get_item(self, item_id: str):
return json_response({"item_id": item_id})支持的动态片段:
-
<name>:匹配单个路径片段。 -
<path:name>:匹配后续多级路径。
推荐导入:
from astrbot.api.web import requestrequest 是当前请求的上下文代理,只能在插件 Web API handler 执行期间访问。常用字段和方法:
| API | 说明 |
|---|---|
request.method |
HTTP 方法,例如 GET、POST
|
request.path |
当前 Dashboard API 路径 |
request.plugin_name |
从扩展路径解析出的插件名 |
request.username |
当前 Dashboard 用户名,可能为 None
|
request.headers |
请求头 |
request.cookies |
请求 cookies |
request.content_type |
请求 Content-Type |
request.client_host |
客户端地址 |
request.path_params |
路由动态参数字典 |
request.query |
query 参数,支持 get() 和 getlist()
|
await request.body() |
原始请求体 bytes |
await request.json(default={}) |
JSON 请求体,解析失败返回 default |
await request.form() |
表单字段,不含上传文件 |
await request.files() |
上传文件 |
query 参数使用示例:
limit = request.query.get("limit", 20, type=int)
tags = request.query.getlist("tag")JSON 请求体使用示例:
payload = await request.json(default={})
enabled = bool(payload.get("enabled"))文件上传使用示例:
from pathlib import Path
# AstrBot v4.30.0 之前的导入方式:
# from astrbot.core.utils.astrbot_path import get_astrbot_plugin_data_path
from astrbot.api.util import get_astrbot_plugin_data_path
from astrbot.api.web import PluginUploadFile, error_response, json_response, request
async def import_file(self):
form = await request.form()
files = await request.files()
upload: PluginUploadFile | None = files.get("file")
if not isinstance(upload, PluginUploadFile):
return error_response("missing file")
target_dir = (
Path(get_astrbot_plugin_data_path())
/ (request.plugin_name or "unknown_plugin")
/ "imports"
)
target_dir.mkdir(parents=True, exist_ok=True)
target = target_dir / Path(upload.filename).name
await upload.save(target)
return json_response(
{
"filename": upload.filename,
"content_type": upload.content_type,
"tag": form.get("tag"),
}
)request.form() 和 request.files() 会缓存解析结果,可以在同一个 handler 中各调用一次。
推荐从 astrbot.api.web 导入响应 helper:
from astrbot.api.web import (
error_response,
file_response,
json_response,
stream_response,
)JSON 响应:
return json_response({"saved": True})错误响应:
return error_response("invalid threshold", status_code=400)文件下载响应:
return file_response(
export_path,
filename="export.json",
content_type="application/json",
)SSE 响应:
import json
from astrbot.api.web import stream_response
async def stream_events(self):
async def events():
yield f"data: {json.dumps({'state': 'started'})}\n\n"
yield f"data: {json.dumps({'state': 'done'})}\n\n"
return stream_response(events())直接返回 dict、list、(body, status_code) 或底层 Response 对象仍然可用;文档和新插件推荐优先使用 astrbot.api.web helper,让插件代码和 Dashboard 内部框架解耦。
为了兼容旧插件,通过 context.register_web_api() 注册的 handler 仍会进入 Quart 兼容请求上下文。旧代码可以继续使用:
from quart import jsonify, request新插件和新文档推荐使用:
from astrbot.api.web import json_response, request不要在同一个 handler 中混用两个 request 代理,迁移时按 handler 逐步替换即可。
- 首页
- 文档入口
- Top Level
- community events
- config
- deploy
- dev
- others
- platform
- 接入 OneBot v11 协议实现
- 接入钉钉 DingTalk
- 接入 Discord
- 接入 Kook
- 接入飞书
- 接入 LINE
- 接入 Matrix
- 接入 Mattermost
- 接入 Misskey 平台
- 接入 QQ 官方机器人平台
- 通过 QQ官方机器人 接入 QQ (Webhook)
- 通过 QQ官方机器人 接入 QQ (Websockets)
- 接入 Satori 协议
- 接入 server-satori (基于 Koishi)
- 接入 Slack
- 接入消息平台
- 接入 Telegram
- 接入 VoceChat
- AstrBot 接入企业微信
- 接入企业微信智能机器人平台
- AstrBot 接入微信公众平台
- 接入个人微信
- providers
- use
- Home
- Docs Entry
- Top Level
- config
- deploy
- Deploy AstrBot on 1Panel
- Deploy AstrBot on BT Panel
- Deploy AstrBot on CasaOS
- Deploy AstrBot from Source Code
- Community-Provided Deployment Methods
- Deploy via Compshare
- Deploy with AstrBot Desktop Client
- Deploy AstrBot with Docker
- Deploy AstrBot with Kubernetes
- Deploy AstrBot with AstrBot Launcher
- Other Deployments
- Script Deployment
- Installation via System Package Manager
- Preface
- dev
- AstrBot Configuration File
- API Scope–Endpoint Reference
- AstrBot HTTP API
- AstrBot Plugin Market JSON Specification
- AstrBot Plugin Market JSON Specification
- Developing a Platform Adapter
- plugin
- AI
- Agents
- Sessions and Conversations
- Models
- Other APIs
- Tools
- Documentation moved
- Text to Image
- Handling Message Events
- Plugin Configuration
- Plugin Internationalization
- Documentation moved
- Shared Bridge API
- Interactive message component
- Standalone plugin page
- Backend Web APIs
- Workbench page
- Sending Messages
- Session Control
- Minimal Example
- Plugin Storage
- AstrBot Plugin Development Guide 🌠
- Publishing Plugins to the Plugin Marketplace
- ospp
- others
- platform
- Connect OneBot v11 Protocol Implementations
- Connect to DingTalk
- Connecting to Discord
- Connect to KOOK
- Connecting to Lark
- Connecting to LINE
- Connecting to Matrix
- Connecting to Mattermost
- Connecting to Misskey Platform
- Connect QQ Official Bot
- Connect QQ via QQ Official Bot (Webhook)
- Connect QQ via QQ Official Bot (Websockets)
- Connect to Satori Protocol
- Connect server-satori (Koishi)
- Connecting to Slack
- Messaging Platforms
- Connecting to Telegram
- Connect to VoceChat
- Connect AstrBot to WeCom
- Connect to WeCom AI Bot Platform
- Connect AstrBot to WeChat Official Account Platform
- Connect Personal WeChat
- providers
- Connect 302.AI
- Agent Runners
- Built-in Agent Runner
- Connect to Coze
- Connect to Alibaba Cloud Bailian Application
- Connect to DeerFlow
- Connect to Dify
- Connect AIHubMix
- coze
- dashscope
- DeepSeek STT Integration
- DeepSeek TTS Integration
- dify
- Model input images
- Large Language Model Providers
- Connect MiraRouter
- NewAPI
- Connect PPIO Cloud
- Connect LM Studio to Use DeepSeek-R1 and Other Models
- Integrating Ollama
- Connect ShengSuanYun
- Connecting to SiliconFlow
- Connecting Model Services
- Connecting to TokenPony
- use
- Agent Execution Mode {#agent-runner}
- Agent Sandbox Environment
- astrbot sandbox
- AstrBot CLI {#cli-commands}
- Docker-based Code Interpreter
- Built-in Commands
- computer
- Context Compression
- Session Configuration Mappings (formerly Custom Rules)
- AstrBot Knowledge Base
- MCP
- AstrBot Plugins {#astrbot-star}
- Proactive Capabilities
- Skills {#anthropic-skills}
- Subagents {#agent-handsoff-and-subagent}
- Unified Webhook Mode
- Web Search
- DeepSeek Search API
- SerpApi
- WebUI