Skip to content
AstrBot
Main Navigation 主页博客路线图HTTP API

简体中文

English

简体中文

English

切换日光/暗黑模式

简介和部署接入消息平台接入 AI使用开发
Sidebar Navigation

简介

关于 AstrBot

社区

常见问题

部署

脚本部署

雨云一键云部署

桌面客户端部署

启动器一键部署

Docker 部署

Kubernetes 部署

宝塔面板部署

1Panel 部署

源码部署

其他部署方式

CasaOS 部署

优云智算 GPU 部署

社区提供的部署方式

支持我们

接入消息平台

快速接入指南

QQ 官方机器人

Websockets 方式(推荐)

Webhook 方式

OneBot v11

企微应用

企微智能机器人

微信公众号

个人微信

飞书

钉钉

Telegram

LINE

Slack

Mattermost

Misskey

Discord

KOOK

Satori

接入 Satori

使用 server-satori

社区提供

Matrix

VoceChat

接入 AI

✨ 接入模型服务

MiraRouter

NewAPI

胜算云

AIHubMix

PPIO 派欧云

硅基流动

小马算力

302.AI

DeepSeek TTS

DeepSeek STT

Ollama

LMStudio

⚙️ Agent 执行器

内置 Agent 执行器

Dify

扣子 Coze

阿里云百炼应用

DeerFlow

使用

WebUI

AstrBot CLI

内置指令

插件

插件

MCP

技能 Skills

AstrBot 内置 AI

Agent 沙盒环境

SubAgent 编排

主动型能力

网页搜索

SerpApi

DeepSeek Search API

知识库

上下文压缩

自定义规则

Agent 执行方式

统一 Webhook 模式

开发

插件开发

🌠 从这里开始

最小实例

接收消息事件

发送消息

插件配置

插件可视化视图

概览

独立插件页面

消息交互组件

工作台页面

共享 Bridge API

后端 Web API

插件国际化

AI

模型

Agent

工具

会话与对话

其他

存储

文转图

会话控制器

杂项

发布插件

插件指南(旧)

插件市场规范

版本列表

2026-11-25

2026-06-27

接入平台适配器

AstrBot HTTP API

API Scope 与接口对照

AstrBot 配置文件

其他

异常诊断

自部署文转图

插件下载不了?试试自建 GitHub 加速服务

社区活动

开源之夏 2025

目录

后端 Web API ​

独立插件页面和常驻工作台页面通过 Bridge 调用这些接口。消息组件与消息创建的工作台页面使用 @filter.ui_action,见消息交互组件。

接口按当前登录用户鉴权。业务数据的归属和权限仍由插件校验;涉及会话时,根据 request.username 验证会话归属,不能只信任前端提交的 session_id。

路由注册 ​

使用 context.register_web_api(route, view_handler, methods, desc) 注册插件 API。

python
context.register_web_api(
    f"/{PLUGIN_NAME}/items/<item_id>",
    self.get_item,
    ["GET"],
    "Get item",
)

路由需要包含插件名作为前缀。视图端的 bridge endpoint 不需要包含插件名:

js
await bridge.apiGet("items/123");

Dashboard 会把它转发到:

text
/api/v1/plugins/extensions/<plugin_name>/items/123

注册路由 /<plugin_name>/items/<item_id> 会匹配该请求,item_id 作为 handler 的关键字参数传入:

python
async def get_item(self, item_id: str):
    return json_response({"item_id": item_id})

支持的动态片段:

  • <name>:匹配单个路径片段。
  • <path:name>:匹配后续多级路径。

请求对象 ​

推荐导入:

python
from astrbot.api.web import request

request 是当前请求的上下文代理,只能在插件 Web API handler 执行期间访问。常用字段和方法:

API说明
request.methodHTTP 方法,例如 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.queryquery 参数,支持 get() 和 getlist()
await request.body()原始请求体 bytes
await request.json(default={})JSON 请求体,解析失败返回 default
await request.form()表单字段,不含上传文件
await request.files()上传文件

query 参数使用示例:

python
limit = request.query.get("limit", 20, type=int)
tags = request.query.getlist("tag")

JSON 请求体使用示例:

python
payload = await request.json(default={})
enabled = bool(payload.get("enabled"))

文件上传使用示例:

python
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:

python
from astrbot.api.web import (
    error_response,
    file_response,
    json_response,
    stream_response,
)

JSON 响应:

python
return json_response({"saved": True})

错误响应:

python
return error_response("invalid threshold", status_code=400)

文件下载响应:

python
return file_response(
    export_path,
    filename="export.json",
    content_type="application/json",
)

SSE 响应:

python
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 内部框架解耦。

Quart 兼容 ​

为了兼容旧插件,通过 context.register_web_api() 注册的 handler 仍会进入 Quart 兼容请求上下文。旧代码可以继续使用:

python
from quart import jsonify, request

新插件和新文档推荐使用:

python
from astrbot.api.web import json_response, request

不要在同一个 handler 中混用两个 request 代理,迁移时按 handler 逐步替换即可。

发现文档有问题?在 GitHub 上编辑此页

Last updated:

Pager
上一篇共享 Bridge API
下一篇插件国际化

Deployed on Rainyun Logo