Skip to content
AstrBot
Main Navigation HomeBlogRoadmapHTTP API

English

简体中文

English

简体中文

Toggle dark mode

Intro & DeployMessaging PlatformsAI IntegrationUsageDevelopment
Sidebar Navigation

Introduction

What is AstrBot

Community

FAQ

Deployment

Script Deployment

Desktop Client

One-click Launcher

Docker

Kubernetes

BT Panel

1Panel

Source Deployment

Other Deployments

CasaOS

Compshare GPU

Community-provided Deployment

Support Us

Messaging Platforms

Quick Start

QQ Official Bot

Websockets

Webhook

OneBot v11

WeCom Application

WeCom AI Bot

WeChat Official Account

Personal WeChat

Lark

DingTalk

Telegram

LINE

Slack

Mattermost

Misskey

Discord

KOOK

Satori

Connect Satori

Using server-satori

Community-provided

Matrix

VoceChat

AI Integration

✨ Model Providers

MiraRouter

NewAPI

ShengSuanYun

AIHubMix

PPIO Cloud

SiliconFlow

TokenPony

302.AI

DeepSeek TTS

DeepSeek STT

Ollama

LMStudio

⚙️ Agent Runners

Built-in Agent Runner

Dify

Coze

Alibaba Bailian

DeerFlow

Usage

WebUI

AstrBot CLI

Built-in Commands

Plugins

Plugins

MCP

Skills

AstrBot Built-in AI

Agent Sandbox

SubAgent Orchestration

Proactive Capabilities

Web Search

SerpApi

DeepSeek Search API

Knowledge Base

Context Compression

Custom Rules

Agent Execution Mode

Unified Webhook Mode

Development

Plugin Development

🌠 Getting Started

Minimal Example

Listen to Message Events

Send Messages

Plugin Configuration

Plugin Views

Overview

Standalone plugin page

Interactive message component

Workbench page

Shared Bridge API

Backend Web APIs

Plugin Internationalization

AI

Models

Agents

Tools

Sessions and Conversations

Other APIs

Storage

HTML to Image

Session Control

Publish Plugin

Plugin Market Specification

Versions

2026-11-25

2026-06-27

Platform Adapter Integration

AstrBot HTTP API

API Scope–Endpoint Reference

AstrBot Configuration File

Others

Diagnostics

Self-hosted HTML to Image

Open Source Summer

OSPP 2025

On this page

Shared Bridge API ​

All views use window.AstrBotPluginView. The host injects the SDK automatically. Use an external type="module" script and await bridge.ready() before calling its methods.

Capabilities ​

APIStandalone pageMessage component / message-created workbenchResident workbench
ready, getContext, onContextSupportedSupportedSupported
Theme CSS tokens and localizationSupportedSupportedSupported
view.subscribe, view.action—Supported—
apiGet, apiPost, file transfers, SSESupported—Supported
chat.fill, chat.send——Supported

Unsupported operations return UNSUPPORTED_CAPABILITY. AstrBotPluginPage remains a compatibility alias; use AstrBotPluginView in new code.

TypeScript declarations ​

Download astrbot-plugin-view.d.ts into your frontend project's src/ directory, or another path covered by tsconfig.json's include. It adds types for window.AstrBotPluginView; the host still injects the runtime SDK, so no JavaScript package import is needed.

These are all public method signatures. The following sections define their data structures. Every method exists on the bridge object, but availability depends on host capabilities. __setInitialContext is internal to the host, not a plugin API.

ts
export interface AstrBotPluginViewBridge<TViewData = JsonObject> {
  ready(): Promise<BridgeContext<TViewData>>;
  getContext(): BridgeContext<TViewData> | null;
  getLocale(): string;
  getI18n(): I18nResources;
  t(key: string, fallback?: string): string;
  onContext(handler: (context: BridgeContext<TViewData>) => void): Unsubscribe;

  view: {
    subscribe(
      handler: (snapshot: ViewSnapshot<TViewData>) => void,
    ): Unsubscribe;
    action(actionId: string, data?: JsonValue): Promise<void>;
  };

  chat: {
    fill(text: string): Promise<ChatResult>;
    send(text: string): Promise<ChatResult>;
  };

  apiGet<TResult = unknown>(
    endpoint: string,
    params?: QueryParams,
  ): Promise<TResult>;
  apiPost<TResult = unknown>(
    endpoint: string,
    body?: JsonValue,
  ): Promise<TResult>;
  upload<TResult = unknown>(
    endpoint: string,
    file: UploadFile,
  ): Promise<TResult>;
  download(
    endpoint: string,
    params?: QueryParams,
    filename?: string,
  ): Promise<DownloadResult>;
  subscribeSSE<TData = unknown>(
    endpoint: string,
    handlers?: SSEHandlers<TData>,
    params?: SSEParams,
  ): Promise<string>;
  unsubscribeSSE(subscriptionId: string): Promise<UnsubscribeSSEResult>;
}
Base types
ts
export type JsonValue =
  | string
  | number
  | boolean
  | null
  | JsonValue[]
  | { [key: string]: JsonValue };
export type JsonObject = { [key: string]: JsonValue };
export type Unsubscribe = () => void;
export type QueryParams = Record<string, JsonValue | undefined>;
export type SSEParams = Record<
  string,
  string | number | boolean | null | undefined
>;
export type I18nResources = Record<string, JsonObject>;

Initialize the bridge ​

The View iframe cannot directly access Dashboard cookies, LocalStorage, or the parent DOM. View scripts must use window.AstrBotPluginView to call backend APIs and read context.

js
const bridge = window.AstrBotPluginView;

In TypeScript, specify your snapshot and business-response types:

ts
import type { AstrBotPluginViewBridge } from "./astrbot-plugin-view";

type CounterData = { count: number };
const bridge = window.AstrBotPluginView as AstrBotPluginViewBridge<CounterData>;
const context = await bridge.ready();
if (context.view) {
  bridge.view.subscribe(({ data }) => console.log(data.count));
} else {
  const result = await bridge.apiGet<{ message: string }>("ping");
  console.log(result.message);
}

Context ​

ready() waits for the parent page to send the initial context and returns Promise<BridgeContext>. Wait for it during page initialization.

js
const context = await bridge.ready();
ts
export interface ThemeColors {
  primary?: string;
  secondary?: string;
  "on-primary"?: string;
  primaryText?: string;
  "on-surface"?: string;
  secondaryText?: string;
  background?: string;
  surface?: string;
  border?: string;
  error?: string;
  [name: string]: string | undefined;
}

export interface BaseContext {
  pluginName: string;
  displayName: string;
  pageName: string;
  pageTitle: string;
  locale: string;
  i18n: I18nResources;
  isDark: boolean;
  themeColors: ThemeColors;
}

export interface ViewSnapshot<TData = JsonObject> {
  id: string;
  revision: number;
  data: TData;
}

export interface StandalonePageContext extends BaseContext {
  target: "plugin_page";
  sessionId: null;
  view?: never;
}

export interface MessageViewContext<TData = JsonObject> extends BaseContext {
  target: "chat_message" | "chat_workbench" | "chat_workbench_with_launcher";
  view: ViewSnapshot<TData>;
  sessionId?: never;
}

export interface ResidentWorkbenchContext extends BaseContext {
  target: "chat_workbench_with_launcher";
  sessionId: string | null;
  view?: never;
}

export type BridgeContext<TData = JsonObject> =
  | StandalonePageContext
  | MessageViewContext<TData>
  | ResidentWorkbenchContext;

Use both target and view to identify the host:

HosttargetviewsessionId
Standalone pageplugin_pageAbsentnull
Message componentchat_messageViewSnapshotAbsent
Message-created workbenchchat_workbench / chat_workbench_with_launcherViewSnapshotAbsent
Resident workbenchchat_workbench_with_launcherAbsentActive session ID, or null without a session

view contains only id, revision, and data, not the name or version fields of Python's UIInteraction.view. Read the registered view name from context.pageName. i18n is keyed by locale, for example { "en-US": { "views": { "counter": { "title": "Counter" } } } }. Additional themeColors keys come from the host theme; prefer the CSS tokens listed below for styling.

Context APIs:

APIReturnsDescription
ready()Promise<BridgeContext>Waits until the bridge is ready and returns the current context
getContext()BridgeContext | nullSynchronously reads the latest context
getLocale()stringCurrent WebUI locale, defaulting to zh-CN
getI18n()I18nResourcesCurrent plugin i18n resources
t(key, fallback)stringReads a dot-separated translation key, returning fallback when missing
onContext(handler)() => voidListens for context changes and returns an unsubscribe function

onContext() invokes the handler immediately when context is available, then on every context update. Its unsubscribe function removes only that subscription. getContext() returns null before initialization. t() searches the current locale, then zh-CN, then en-US; if no value exists, it returns fallback or an empty string.

Respond to locale or theme changes:

js
function render() {
  document.title = bridge.t("views.bridge-demo.title", "Bridge Demo");
  document.getElementById("locale").textContent = bridge.getLocale();
}

await bridge.ready();
render();

const off = bridge.onContext(render);
window.addEventListener("beforeunload", off);

Plugin Web APIs ​

Request and return rules ​

The endpoint used by apiGet, apiPost, upload, download, and subscribeSSE is a plugin-local relative path, such as stats, settings/save, or files/export. Prefer not to start it with /; the current bridge strips leading / for compatibility.

endpoint must not be empty, contain \, contain a URL scheme, contain query strings or fragments, or contain empty, ., or .. path segments.

Do not append query strings to endpoint:

js
await bridge.apiGet("stats", { limit: 20 });

Bridge JSON calls use this compatibility rule:

  • For successful responses, a non-null data field is unwrapped, regardless of whether status is "ok". For example, { "data": 0 } resolves to 0.
  • If data is absent or null, the complete response is returned. Both { "message": "pong" } and { "data": null } are returned unchanged.
  • If the backend returns { "status": "error", "message": "..." }, or the HTTP request fails, the Promise rejects with Error.

For View-only APIs, prefer returning plain business JSON:

python
return json_response({"message": "pong"})

Use this for errors:

python
return error_response("missing file", status_code=400)

Handle errors on the View:

js
try {
  await bridge.apiPost("settings/save", { enabled: true });
} catch (error) {
  console.error(error.message);
}

apiGet(endpoint, params) ​

ts
apiGet<TResult = unknown>(endpoint: string, params?: QueryParams): Promise<TResult>

Sends a GET request. params are sent as query parameters.

js
const stats = await bridge.apiGet("stats", { limit: 20, tag: "today" });

Backend:

python
async def stats(self):
    limit = request.query.get("limit", 20, type=int)
    tag = request.query.get("tag")
    return json_response({"limit": limit, "tag": tag})

apiPost(endpoint, body) ​

ts
apiPost<TResult = unknown>(endpoint: string, body?: JsonValue): Promise<TResult>

Sends a POST JSON request.

js
const result = await bridge.apiPost("settings/save", {
  enabled: true,
  threshold: 0.8,
});

Backend:

python
async def save_settings(self):
    payload = await request.json(default={})
    return json_response({"saved": True, "enabled": payload.get("enabled")})

upload(endpoint, file) ​

ts
upload<TResult = unknown>(endpoint: string, file: UploadFile): Promise<TResult>

Uploads one file as multipart/form-data. The field name is always file.

js
const input = document.querySelector("input[type=file]");
const file = input.files[0];
const result = await bridge.upload("files/import", file);

Backend:

python
from astrbot.api.web import PluginUploadFile, error_response, json_response, request


async def import_file(self):
    files = await request.files()
    upload: PluginUploadFile | None = files.get("file")
    if not isinstance(upload, PluginUploadFile):
        return error_response("missing file", status_code=400)
    return json_response({"filename": upload.filename})

If you need extra structured fields, send them through a separate apiPost call or use query parameters to select import behavior. The current upload() bridge method sends one file.

download(endpoint, params, filename) ​

ts
download(endpoint: string, params?: QueryParams, filename?: string): Promise<DownloadResult>

Requests a plugin backend file endpoint and triggers a browser download. params are sent as query parameters. filename is optional; when omitted, the bridge tries to read it from response headers.

js
await bridge.download("files/export", { format: "json" }, "export.json");

Backend:

python
async def export_file(self):
    fmt = request.query.get("format", "json")
    return file_response(
        export_path,
        filename=f"export.{fmt}",
        content_type="application/json",
    )

download() resolves to:

json
{ "filename": "export.json" }

subscribeSSE(endpoint, handlers, params) ​

ts
subscribeSSE<TData = unknown>(
  endpoint: string,
  handlers?: SSEHandlers<TData>,
  params?: SSEParams,
): Promise<string>
ts
export interface SSEMessage<TData = unknown> {
  raw: string;
  parsed: TData | string;
  eventType: string;
  lastEventId: string;
}

export interface SSEHandlers<TData = unknown> {
  onOpen?: () => void;
  onMessage?: (event: SSEMessage<TData>) => void;
  onClose?: () => void;
  onError?: () => void;
}

Subscribes to plugin backend SSE and returns Promise<subscriptionId>. handlers may include onOpen, onMessage, onClose, and onError.

js
const subscriptionId = await bridge.subscribeSSE(
  "events",
  {
    onOpen() {
      console.log("SSE opened");
    },
    onMessage(event) {
      console.log(event.raw, event.parsed, event.lastEventId);
    },
    onClose() {
      console.log("SSE stream ended");
    },
    onError() {
      console.warn("SSE error");
    },
  },
  { topic: "logs" },
);

event.raw is the raw string. If the message is a JSON string, event.parsed is parsed automatically; otherwise it equals the raw string. event.eventType matches the SSE event: field and defaults to message.

onClose is called when the stream ends, and onError is called if reading it fails. Calling unsubscribeSSE triggers neither callback. The bridge does not reconnect automatically; subscribe again if needed. Initial subscription failures reject the returned promise.

The backend must return text/event-stream:

python
async def events(self):
    async def stream():
        yield 'data: {"message": "ready"}\n\n'

    return stream_response(stream())

Unsubscribe:

js
await bridge.unsubscribeSSE(subscriptionId);

Clean up on unload:

js
window.addEventListener("beforeunload", () => {
  bridge.unsubscribeSSE(subscriptionId);
});

View actions and chat controls ​

ts
view.subscribe(handler: (snapshot: ViewSnapshot<TViewData>) => void): Unsubscribe
view.action(actionId: string, data?: JsonValue): Promise<void>
chat.fill(text: string): Promise<ChatResult>
chat.send(text: string): Promise<ChatResult>

Call view.subscribe() after ready(). It immediately supplies a copy of the current snapshot and runs again when a snapshot with the same or a higher revision arrives. Unsubscribing removes the listener without closing the view. Notifications do not necessarily indicate a business-data change; mutating the copy does not save data.

The data argument of view.action() defaults to null. Success resolves to undefined, not a snapshot; read updated data through view.subscribe() or getContext().view. The promise settles after the callback and state commit, without waiting for subsequent model generation. Declare each action in views[].actions in metadata.

chat.fill() appends to the draft and focuses the composer. chat.send() sends to the current session while preserving the draft and attachments. Both resolve to { sessionId: string | null }. Sending requires an open session with no running reply, and resolves when the request is accepted rather than when the answer finishes. Text must not be blank or exceed 100,000 UTF-16 code units. See Workbench page for host requirements and examples.

Result and error types ​

ts
export interface UploadFile {
  arrayBuffer(): Promise<ArrayBuffer>;
  name?: string;
  type?: string;
  lastModified?: number;
}

export interface DownloadResult {
  filename: string;
}

export interface ChatResult {
  sessionId: string | null;
}

export interface UnsubscribeSSEResult {
  subscriptionId: string;
}

export interface BridgeError extends Error {
  code?: string;
}
  • Browser File and Blob objects satisfy UploadFile. The default filename is upload.bin; the default MIME type is application/octet-stream.
  • unsubscribeSSE(id) returns Promise<UnsubscribeSSEResult>, resolving to { subscriptionId: id } even if the connection has already ended.
  • Generics on apiGet, apiPost, and upload describe the unwrapped business response and default to unknown. They do not validate server data.
  • SSE parsed contains the JSON value when parsing succeeds, otherwise the original string. eventType defaults to message. lastEventId starts as an empty string and persists across events without an id: field. onOpen, onClose, and onError receive no arguments.
  • apiGet / download use Axios query serialization. subscribeSSE calls String(value) for each parameter and does not support repeated keys or arrays. Omit optional SSE parameters instead of passing undefined or null.
  • With an omitted body, null, false, 0, or an empty string, the current apiPost host sends {}. Prefer JSON object request bodies.

Failures are generally Error objects; code is optional. Message views can report codes such as UNSUPPORTED_CAPABILITY, ACTION_IN_PROGRESS, and REVISION_CONFLICT. Host-forwarded plugin Web API and chat-control errors carry only a message, not an HTTP status or additional backend fields.

ts
try {
  await window.AstrBotPluginView.view.action("reading_view:set_read", {
    book_id: "design",
    read: true,
  });
} catch (error: unknown) {
  if (error instanceof Error) {
    const code = (error as BridgeError).code;
    console.error(code ?? "BRIDGE_ERROR", error.message);
  }
}

Import the type with import type { BridgeError } from "./astrbot-plugin-view". view.subscribe() can throw synchronously; request methods reject their promises. A REVISION_CONFLICT synchronizes the latest snapshot before rejecting; do not blindly retry actions with side effects.

Light and dark themes ​

AstrBot syncs the current theme to Plugin Views. The bridge SDK maintains a data-theme attribute on <html>:

  • Light mode: <html data-theme="light">
  • Dark mode: <html data-theme="dark">

The bridge also synchronizes CSS color-scheme to light or dark, keeping transparent iframe backgrounds consistent with the host theme.

When Follow System is selected, the View still receives either light or dark.

Following user-selected colors ​

All targets use the same AstrBotPluginView bridge. It injects these CSS tokens on <html> and updates them on initialization, light/dark changes, and palette changes. No manual listener or CSS assignment is needed.

CSS tokenPurpose
--astrbot-color-primaryPrimary
--astrbot-color-secondarySecondary
--astrbot-color-on-primaryText on primary
--astrbot-color-textBody text
--astrbot-color-text-secondarySecondary text
--astrbot-color-backgroundPage background
--astrbot-color-surfaceSurface background
--astrbot-color-borderBorder
--astrbot-color-errorError
css
body {
  color: var(--astrbot-color-text);
  background: var(--astrbot-color-background);
}
button {
  color: var(--astrbot-color-on-primary);
  background: var(--astrbot-color-primary);
  border: 1px solid var(--astrbot-color-border);
}

The bridge only injects variables; it does not force a page background. Chat cards can keep background: transparent. Existing themeColors.primary / secondary remain available. All targets use bridge.onContext(({ isDark, themeColors, locale }) => { ... }) when additional JavaScript behavior is needed.

View metadata and localization ​

Define default view titles and descriptions under views in metadata.yaml. Put translations in .astrbot-plugin/i18n/<locale>.json under views.<name>, alongside the top-level metadata and config keys.

For example, localized metadata for the bridge-demo View is stored as follows:

text
astrbot_plugin_view_demo/
├─ views/
│  └─ bridge-demo/
│     └─ index.html
└─ .astrbot-plugin/
   └─ i18n/
      ├─ zh-CN.json
      └─ en-US.json

.astrbot-plugin/i18n/en-US.json:

json
{
  "views": {
    "bridge-demo": {
      "title": "Bridge Demo",
      "description": "Shows how a plugin page reads the WebUI locale and translations.",
      "heading": "Plugin View",
      "refresh": "Render again"
    }
  }
}

Field reference:

FieldRequiredPurpose
titleNoThe View name in the sidebar, WebUI shell, and plugin detail page.
descriptionNoThe View description on the plugin detail page.
Other fieldsNoView-specific translations, such as heading and refresh in this example; read them with bridge.t().

Add each language to its matching locale file, such as .astrbot-plugin/i18n/zh-CN.json for Chinese. Use views for new plugins; the older pages key remains only as a compatibility alias. Reload the plugin after editing locale files so the WebUI receives the latest metadata.

If the current locale has no title, the ungrouped sidebar displays the plugin name. In grouped mode it displays the View directory name because the group already shows the plugin name. Render other text inside the View with bridge.t() and react to locale changes with onContext():

js
function render() {
  document.title = bridge.t("views.bridge-demo.title", "Bridge Demo");
  document.getElementById("heading").textContent = bridge.t(
    "views.bridge-demo.heading",
    "Plugin View",
  );
}

await bridge.ready();
render();
bridge.onContext(render);

Static asset paths ​

Use normal relative paths:

html
<link rel="stylesheet" href="./style.css" />
<script type="module" src="./app.js"></script>
<img src="./assets/logo.svg" alt="" />

AstrBot authenticates view assets with a path token: the content_path returned by the entry config endpoint looks like /api/v1/plugins/<plugin>/views/<view>/_t/<token>/, and relative URLs inside the page resolve under that prefix and inherit the token through normal URL resolution. Content is served as-is, without rewriting. The token lives as long as the dashboard session and is scoped to exactly that plugin and view.

Do not hardcode content URLs, append asset_token yourself, or rely on .. to escape the View root. The legacy /api/plugin/page/content/...?asset_token=... form has been removed.

If you build a SPA, prefer hash routing. The static asset server resolves real file paths; with history routing, refreshing a page requires a real file at that path.

Security constraints ​

Plugin Views run inside a restricted iframe:

text
allow-scripts allow-forms allow-downloads

The View cannot directly access Dashboard cookies, LocalStorage, or the parent DOM, and it cannot bypass the bridge to reuse Dashboard auth. All operations that need Dashboard identity should go through the bridge.

Asset responses include security headers such as:

  • X-Frame-Options: SAMEORIGIN
  • Content-Security-Policy: frame-ancestors 'self'; object-src 'none'; base-uri 'self'
  • Cache-Control: no-store
  • X-Content-Type-Options: nosniff

Backend handlers must still validate input. Do not trust paths, filenames, formats, or numeric ranges sent by the View. Store files only in safe directories and prefer whitelisted or regenerated filenames.

Troubleshooting ​

  • View is missing: check that views/<view_name>/index.html exists, the plugin is enabled, and the plugin detail page has been refreshed.
  • Bridge is missing: make sure your script runs after the bridge SDK is injected; external type="module" scripts are recommended.
  • API is not matched: make sure the registered route includes the plugin name prefix, such as /{PLUGIN_NAME}/stats, while the View endpoint is stats.
  • Query or JSON is empty: pass GET values through apiGet(endpoint, params) and POST JSON through apiPost(endpoint, body).
  • Upload is empty: upload() always uses the field name file; read it with (await request.files()).get("file").
  • SSE has no messages: make sure the backend response is text/event-stream and each message ends with a blank line, such as data: ...\n\n.
  • SSE returns 401: do not call new EventSource("/api/v1/...") directly from the View. Native EventSource cannot send the Authorization header; call through bridge.subscribeSSE() instead.

View labels ​

Resolve each field independently: check the current locale, then zh-CN, then en-US. Within each locale, prefer views.<name> and fall back to the legacy pages.<name> key. A valid translation overrides the metadata title or description. Without a translation, use metadata; ultimately titles fall back to name and descriptions to an empty string. Blank or non-string translations do not override defaults.

Existing i18n files do not need to move. Legacy pages translations remain valid. You may move default-language labels into metadata, remove duplicate entries for that locale, and keep translations for other languages. Translation files cannot change target, actions, or versions.

Standalone page entries, workbench entries, and tabs share this resolution. An instance's event.view_result(title=...) overrides static labels for dynamic titles such as filenames.

Edit this page on GitHub

Last updated:

Pager
PreviousWorkbench page
NextBackend Web APIs

Deployed on Rainyun Logo