---
title: Widgets API
summary: "`host.widgets`: read, arrange and show the team's widgets and dashboards, with the full API under `widgets` and the read-and-show subset under `widgets:view`."
audience: both
stability: experimental
since: protocol 1
---

# Widgets API

Widgets are small views the assistant makes from read-only tool calls (and from public web URLs the server fetches,
see [Web sources](../widgets.md#web-sources)); they belong to the team, not to any extension. [widgets.md](../widgets.md) describes the model: `Widget`, `Dashboard`, `WidgetSource`, the grid
constants, the `<widget:ID|Name>` token, and the React helpers `WidgetFrame`, `useWidgetList` and
`useWidgetFeed`. This page is the host API that extensions reach them through.

```ts
interface SpacemixHost {
  widgets?: WidgetsApi; // present with `widgets` or `widgets:view`
}
```

`WidgetsApi` is declared in `packages/sdk/src/widgets.ts`.

**Permissions.**

- `widgets`: the whole API. For an extension that arranges dashboards (Dashboards).
- `widgets:view`: only reading saved widgets, showing them live and saving the store of a widget the frame is
  showing (`save`, only while it `watch`es that widget), never changing widgets or dashboards (`WidgetsViewApi`: the methods in `WIDGETS_VIEW_METHODS` plus `subscribe`
  and `watch`). For an extension that shows widgets inside its own content (Team Chat). The other methods are
  present on the object but reject with `<Name> doesn't have the "widgets" permission.`

Writes change the team's shared store, which every window, every account and the assistant's `widget_*` tools see.

**Creating takes `server:widgets`.** Your extension's permission doesn't stand in for the person's: `deleteWidget` and `openStudio` depend on the person using the frame holding [`server:widgets`](../permissions.md#who-may-create-extensions-and-widgets). Reading, arranging dashboards, showing a widget and saving its store (`save`) don't.

**Dashboards are the Dashboards extension's.** Their records belong to `ai.spacemix.dashboards`, so a person who
may not use that extension (see [Who can use an extension](../permissions.md#who-can-use-an-extension)) has no
dashboards: `dashboards()` and the `dashboards` of `snapshot()` are `[]`, and `createDashboard`,
`renameDashboard`, `deleteDashboard` and `addToDashboard` reject with `Dashboards isn't available to you.` With no
dashboard to act on, the other arranging methods change nothing. Widgets themselves work as usual. Access granted
later shows the dashboards without a reload.

## Reading

```ts
widgets(): Promise<Widget[]>;
dashboards(): Promise<Dashboard[]>;
snapshot(): Promise<{ widgets: Widget[]; dashboards: Dashboard[]; ready: boolean }>;
subscribe(fn: () => void): () => void;
```

- `widgets` (`widgets` or `widgets:view`): with `widgets`, every widget including ones still unsaved in the
  Studio (`unsaved`) and with `draft` and `history`. With only `widgets:view`, saved widgets only, without `draft`
  and with `history: []`. Bridge method: `widgets.widgets`.
- `dashboards` (`widgets`). Bridge method: `widgets.dashboards`.
- `snapshot` (`widgets`): both, and `ready`, false until they've been read from storage (both empty until then).
  Bridge method: `widgets.snapshot`.
- `subscribe` (either permission): called whenever widgets or dashboards change, from any window or the
  assistant. Read again. Channel: `widgets`.

## Arranging (`widgets` only)

```ts
createDashboard(name?: string): Promise<Dashboard>;
renameDashboard(id: string, name: string): Promise<void>;
deleteDashboard(id: string): Promise<void>;
reorderDashboards(ids: string[]): Promise<void>;
addToDashboard(dashboardId: string, widgetId: string, at?: { x: number; y: number }): Promise<void>;
removeFromDashboard(dashboardId: string, itemId: string): Promise<void>;
resizeItem(dashboardId: string, itemId: string, size: WidgetSize): Promise<void>;
setLayout(dashboardId: string, boxes: DashboardBox[]): Promise<void>;
deleteWidget(id: string): Promise<void>;
```

| Method | Bridge method | Behavior |
|---|---|---|
| `createDashboard` | `widgets.createDashboard` | A blank or missing name becomes "New dashboard". |
| `renameDashboard` | `widgets.renameDashboard` | Blank names are ignored. |
| `deleteDashboard` | `widgets.deleteDashboard` | Its widgets stay; only the arrangement goes. |
| `reorderDashboards` | `widgets.reorderDashboards` | These first, in this order; unlisted ones keep their place after them. |
| `addToDashboard` | `widgets.addToDashboard` | At the widget's own size, in the first free spot, or at cell `at` (integers) pushing what's there down. Rejects a widget that is still `unsaved`; it must be saved in the Studio first. |
| `removeFromDashboard` | `widgets.removeFromDashboard` | `itemId` is the placement's id, not the widget's. |
| `resizeItem` | `widgets.resizeItem` | Pushes whatever it now covers down. Missing sizes default to `MIN_SIZE`. |
| `setLayout` | `widgets.setLayout` | New positions and sizes for some or all items (after a drag). Sizes are clamped. |
| `deleteWidget` | `widgets.deleteWidget` | Deletes the widget everywhere: the gallery and every dashboard. Rejects when the person doesn't hold `server:widgets`, or when the write fails; the widget and its placements then stay. A widget already gone counts as deleted. |

## Leaving for the Studio or the gallery

```ts
openStudio(options?: { widgetId?: string; dashboardId?: string; text?: string }): Promise<void>; // `widgets`
openGallery(widgetId?: string): Promise<void>;                                                    // either
```

Both leave the extension's view. `openStudio` opens a widget session in a tab of the [Studio](../../guide/studio.md): a new widget for `dashboardId`, or
`widgetId` to change one; `text` is sent as the first message. Someone else's never-saved widget opens in the
Widgets gallery instead, where it is reviewed before it opens in the Studio. `openStudio` always resolves: for a person without
`server:widgets` it shows the Studio's notice that building needs a permission, and no session starts. Widgets are made and edited only in
the Studio, never through this API. `openGallery` opens the Widgets gallery, scrolled to `widgetId` if given. Bridge methods:
`widgets.openStudio`, `widgets.openGallery`.

## Showing a widget live (either permission)

```ts
frameUrl(): Promise<string>;
watch(widgetId: string, onUpdate: (update: WidgetUpdate) => void): () => void;
refresh(widgetId: string): Promise<void>;
purge(widgetId: string): Promise<void>;
call(widgetId: string, tool: string, args: unknown): Promise<unknown>;
reportError(widgetId: string, message: string): Promise<void>;
save(widgetId: string, value: WidgetStoreValue, options?: { merge?: boolean }): Promise<WidgetStore>;
```

A widget runs in an iframe of its own, nested in the extension's frame:
`<iframe sandbox="allow-scripts allow-forms" referrerpolicy="no-referrer" src={await frameUrl()}>`.
`WidgetFrame` from `@spacemix/sdk/react` does all of the following for you; use these only to build your own. If
you do, render on the frame's first `load` and stop at any later one (see [The document](../widgets.md#the-document)).

- `frameUrl`: the widget frame document's absolute URL for this window; the extension frame's policy allows it.
  Bridge method: `widgets.frameUrl`.
- `watch`: keeps the widget's sources running while watched (shared with every other place showing it) and calls
  `onUpdate` with its status, and with `data` whenever the sources ran or their cached results came in: right away
  if known, then on every change. An update with `purged: true` means the cache was dropped: forget the data shown.
  It follows the widget's current definition, so a change in the Studio re-runs it. `store` (widget frame 2) comes
  once the widget's [store](../widgets.md#the-widgets-store) has been read and again whenever it changes, from
  anywhere; an update without it keeps the store shown. A widget that doesn't
  exist, or is deleted while watched, gives one update with `status.state: "error"` (`This widget no longer
  exists.`, with `purged: true` if it was deleted) and stops. A widget that is still `unsaved`, or a saved one that
  turns unsaved, gets an error status (`This widget isn't saved yet.`) with `purged: true` and starts once it's
  saved. Channel: `widget-data` with `{ widgetId }`.
- `refresh`: fetches the sources again now; what's shown stays up until the results are in. A widget that is still
  `unsaved` rejects with `This widget isn't saved yet.` Bridge method: `widgets.refresh`.
- `purge`: drops the viewer's cached data for the widget; on screen it goes back to loading and fetches afresh.
  For a widget that is still `unsaved` it resolves and does nothing. Bridge method: `widgets.purge`. The cache
  itself is described in [Widget reference](../widgets.md#the-data-cache).
- `call`: answers the widget's own `spacemix.call(tool, args)`: runs a **read-only** tool (collection `list`/`get`,
  `readOnly` actions, read-only MCP tools) as the viewer, without approval. Any other tool rejects with
  `"<tool>" isn't a read-only tool of an added extension.`, and `web-get` (`WEB_SOURCE_TOOL`) always rejects: web
  data comes only from a widget's declared sources. A tool failure rejects with its message. A widget that is
  still `unsaved` rejects with `This widget isn't saved yet.` Bridge method: `widgets.call`.
- `reportError`: the widget's script threw; the message (cut to 1 000 characters) shows in its status until its
  code changes. For a widget that is still `unsaved` it resolves and does nothing. Bridge method:
  `widgets.reportError`.
- `save`: answers the widget's own `spacemix.save(value, { merge })`, with [the viewer's OK](../widgets.md#the-viewers-ok)
  where that's needed, and serves a settings
  panel: replaces the widget's [store](../widgets.md#the-widgets-store) with `value`, or with `merge` puts its
  top-level keys over it (a `null` removes one), and resolves with the `WidgetStore` after. With `widgets` any
  saved widget's store; with only `widgets:view`, only that of a widget this frame is watching right now, else it
  rejects with `<Name> can change a widget's data only while it shows that widget.` That rule stops writes in the
  background; it isn't a boundary against an extension that means harm, since a `widgets:view` extension can watch
  any widget it likes. Granting `widgets:view` means trusting the extension with the widgets' data. The server
  writes nothing but the store. Rejects
  when `value` isn't an object or is over `MAX_WIDGET_STORE_BYTES` (256 KB), with `This widget isn't saved yet.`,
  or with `This widget no longer exists.` Writes are folded into at most two a second per widget. Bridge method:
  `widgets.save`.

The nested frame speaks a small protocol of its own (not the extension protocol). Frame to parent:
`{ type: "ready" }`; `{ type: "call", id, tool, args }`, answered with `{ type: "result", id, result }` or
`{ type: "result", id, error }`; `{ type: "open", id }` to open an extension (use `host.shell.open`);
`{ type: "error", message }`; from widget frame 2, `{ type: "save", id, value, merge }`, answered with
`{ type: "result", id, result }` (the store after, `WidgetStore.value`) or `{ type: "result", id, error }` (use
`save`). Parent to frame: `{ type: "render", html, data, theme, store }` once loaded and data and the store are in
(`store` is `WidgetStore.value`), then `{ type: "data", data }` on every update, `{ type: "theme", theme }` when the
theme changes, and `{ type: "store", value }` when the store changed, where `theme` is
`{ mode: "light" | "dark", vars: { "--op-bg": "…", … } }`. `WidgetFrame` watches the store itself.

## Example

```ts
import { host, type Widget } from "@spacemix/sdk";

export async function savedWidgetNames(): Promise<string[]> {
  const widgets = host.widgets;
  if (!widgets) return [];
  const all: Widget[] = await widgets.widgets();
  return all.filter((w) => !w.unsaved).map((w) => w.name);
}
```

## Stability

Experimental: built for Dashboards and Team Chat and not yet exercised live by others. Note that `call` lets any
extension with `widgets:view` run any read-only tool of any extension, whether or not a widget uses it, and `save`
lets it write the store of any saved widget it shows.
