---
title: "Data: sources, spacemix.call and the store"
summary: Where a widget's data comes from - its sources and which tools qualify, web sources for public data, how data arrives in the frame, the per-viewer data cache and refreshSeconds, spacemix.call for on-demand reads, saving data in the widget's store, handling errors, and why a widget runs as the viewer.
audience: both
stability: experimental
since: protocol 1
---

# Data: sources, spacemix.call and the store

A widget gets data two ways. Its **sources** are tool calls SpaceMIX runs for it, caches and hands to `spacemix.onData`. **`spacemix.call`** is a tool call the widget's own script makes on demand, for a filter, a page or a detail. Both take only read-only tools of installed extensions, and both run as whoever is looking. For public data that no extension has, a source can also be a [web source](#web-sources): a fixed URL the server fetches.

Put the work in the sources. They are cached; `spacemix.call` isn't.

Both only read. For data the widget keeps itself (a checklist's ticks, a note, a choice), it has its own [store](#saving-data).

## Sources

A source is one tool call:

```json
{ "key": "tasks", "tool": "tasks_items_list", "args": { "where": { "done": false }, "limit": 50 } }
```

| Field | Type | What it is |
|---|---|---|
| `key` | string | The name the result has in `spacemix.data`. Matches `/^[a-zA-Z_][a-zA-Z0-9_]*$/`; unique within the widget. |
| `tool` | string | A read-only tool of an installed extension, by its tool name, or `web-get` for a [web source](#web-sources). |
| `args` | object | The tool's arguments, as the assistant would pass them; `{}` when left out. Checked against the tool's input schema when the source is set; an MCP tool's arguments only need to be an object. |
| `extension` | `{ id, name }` | Filled in by SpaceMIX when the source is set: the extension the tool came from. Used to name a missing extension. Web sources have none. |

The tools that qualify are the ones that change nothing:

| Tool | Name | Read-only when |
|---|---|---|
| A collection's `list` and `get` | `<ns>_<collection>_list`, `<ns>_<collection>_get` | always, if the collection's `operations` offer them |
| An action | `<ns>_<action>` | the action is declared `readOnly: true` |
| An MCP tool | `<ns>_<tool>` | the server annotates it `readOnlyHint: true` |

Never qualifying: the core's own `widget_*` and `dashboard_*` tools, and the core's other tools (`extension_*`, `project_*`), which aren't an installed extension's. A tool that changes data can't be made a source by any route: the tools refuse it when the source is set, import refuses it, and the runtime refuses it again on every run. How the tool names are formed is in [Tool names](../reference/manifest.md#tool-names) and [Tools derived from a collection](../reference/manifest.md#tools-derived-from-a-collection).

A source's result is exactly the tool's JSON. A collection's `list` gives `{ total, items }`, newest first, with `limit` defaulting to 20, so show `total` for a count and pass a `limit` for the list (see [Collections](../guide/giving-the-assistant-data.md#collections)).

## Web sources

A web source brings public data from the web (a price, an exchange rate, the weather) into a widget whose frame still has no network. Its `tool` is `web-get` (`WEB_SOURCE_TOOL` in `@spacemix/sdk`) and its `args` are only `{ url }`:

```json
{ "key": "btc", "tool": "web-get", "args": { "url": "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd" } }
```

When someone views the widget, SpaceMIX's server fetches the URL, anonymously, and the result lands in `spacemix.data[key]` like any source's: the parsed JSON, or the text. A failure is `{ error }` naming the host, for example `api.coingecko.com answered 429.`

- **The URL is fixed.** It is set with the source and stored normalized (lower-case host, no `#fragment`). The widget's script can't choose another: `spacemix.call` refuses `web-get`, so build any filter or page into the URL.
- **The person approves it.** When the assistant's `widget_create` or `widget_update` adds a URL or changes one, the chat asks first and shows the URL in full; an international host is called out so a look-alike stands out. Importing a widget file shows its URLs in the import confirm.
- **Only public `https:` APIs.** `GET` on the default port, no user name or password, no spaces or control characters (percent-encode them), at most 2,048 characters; no headers, keys or cookies; public addresses only; at most 3 redirects, 10 seconds and 1 MB. At most 5 web sources per widget.
- **Shared and limited.** The server keeps each answer 30 seconds for everyone, and fetches only URLs that a widget on the server declares. Pick a `refreshSeconds` that suits the API's rate limits: 60 or more for prices, longer for slow data.
- **It shows.** A widget with web sources has a **Web** tag by its name, in the Widgets gallery and Studio, on Dashboards and on Team Chat cards; hovering it lists the hosts. The requests are listed under **Widgets** in **Settings → Extensions → Data leaving** for people with Settings access (`server:settings`); anyone may switch them off there, and turning them back on takes Settings access.
- **The site sees viewing.** Each fetch tells the site when the widget was viewed, from the server's address. See [Known gaps](../reference/widgets.md#known-gaps).

Prefer an installed extension's tool when one has the data. The full rules, limits and error texts are in [Web sources](../reference/widgets.md#web-sources).

## How data arrives

1. The widget comes on screen. If the viewer's cache has fresh results for it, those are used; otherwise its sources run, all at once, each with 15 seconds.
2. The frame gets the HTML and the data together, inserts the markup and runs the scripts. `spacemix.data` is set by then.
3. `spacemix.onData(fn)` calls `fn(data)` right away, and again after every later update. Always render from it.

Each source's result lands under its key. A source that fails, times out (`<tool> took longer than 15 seconds.`) or names a tool that isn't usable gets `{ error: "<message>" }` under its key instead, and the widget's status says `<key>: <message>`. When data is already showing, a failed run leaves it up and only the status changes.

A source whose extension isn't installed for the viewer gets `{ error: "Extension uninstalled." }`, and the widget shows an "Extension uninstalled" pill in its place.

A source whose extension is installed but that the viewer may not use gets `{ error: "<Name> isn't available to you." }`, and one whose extension is a paid one turned off on the server until it's paid for gets `{ error: "<Name> is turned off on this server until it's paid for." }` (for viewers who may use it). The widget still renders; show the error where the data would be. See [watch](../reference/widgets.md#watch).

The sources run again when the cached data expires (while the window is visible), shortly after data of an extension the widget reads changes (debounced 400 ms), on **Refresh**, after **Purge cache**, and when the sources change. The full list is in [watch](../reference/widgets.md#watch).

## The data cache and refreshSeconds

What the sources return is cached for each viewer, in memory and in the core's `widgetcache` namespace, with the time it was fetched. A widget coming on screen shows the cached results at once and fetches again only once they are older than its `refreshSeconds`. So a widget with heavy sources (an external API, a big list) costs one fetch per lifetime, not one per visit.

| | Value |
|---|---|
| Default | 86,400 seconds (a day) |
| Minimum | 30 seconds; anything lower is raised to 30 |
| Set by | `refreshSeconds` in `widget_create` or `widget_update`, or in a widget file; the Studio has no control for it |

Choosing a lifetime:

- Leave the default for most widgets: totals, history, configuration and CRM records change slowly, and people can press **Refresh**.
- Use 3600 (an hour) for data that moves within the day and must be current: queues, today's counts, live status. Less is rarely right.
- Data of extensions that keep it in SpaceMIX (their collections) re-runs the widget on every change anyway, whatever the lifetime. Data an extension fetches from outside (an external API) changes only when the widget fetches.

How the cache behaves:

- It is keyed by the sources. Changing them drops it; changing only the HTML keeps it, so iterating on the code doesn't fetch again.
- Only a run in which every source succeeded is cached. An entry larger than 1 MB as JSON stays in memory for the session and isn't stored.
- Another window of the same person that fetches or purges hears about it and takes the newer data.
- When a source's extension is no longer installed, the cached data is dropped rather than shown.

**Refresh** fetches again now while the data shown stays up. **Purge cache** drops the viewer's entry; the widget goes back to loading and fetches afresh. On a dashboard both are in the dropdown in the widget's top-right corner, which also says when the data was fetched and how long it is kept; in the gallery they are **Refresh data** and **Purge cache** in the widget's menu. See [The corner menus](placing.md#the-corner-menus).

## spacemix.call

`spacemix.call(tool, args)` runs one more read-only tool from the widget's script and resolves with its result. Use it for what the person does in the widget: switching a tab, paging, opening a detail.

```html
<div class="op-widget">
  <div class="op-tabs" id="tabs"><button aria-selected="true" data-done="false">Open</button><button data-done="true">Done</button></div>
  <ul class="op-list op-scroll" id="list"></ul>
</div>
<script>
  const { html, render, empty, error } = spacemix.ui;
  const show = (items) => render("#list", items.length ? items.map((t) => html`<li class="op-item"><span class="op-item-title">${t.title}</span></li>`) : empty("None."));
  spacemix.onData(({ tasks }) => (tasks.error ? render("#list", error(tasks)) : show(tasks.items)));
  document.getElementById("tabs").addEventListener("click", async (e) => {
    const b = e.target.closest("button");
    if (!b) return;
    for (const x of b.parentElement.children) x.setAttribute("aria-selected", String(x === b));
    try {
      const r = await spacemix.call("tasks_items_list", { where: { done: b.dataset.done === "true" }, limit: 20 });
      show(r.items);
    } catch (err) {
      render("#list", error(err.message));
    }
  });
</script>
```

- It takes the same tools sources do, except `web-get`, which always rejects. Anything else rejects with `"<tool>" isn't a read-only tool of an added extension.`
- It rejects with the tool's own error when the tool fails or returns `{ error }`, and with `<tool> took longer than 10 seconds.` after 10 seconds.
- Its results aren't cached: every call runs the tool. Don't call it on every render for data a source could fetch.
- After a call, a change to that extension's data can also re-run the widget's sources.

## Handling errors

Every widget handles three states besides data: a failed source, no data, and its own script failing.

- **A failed source.** `spacemix.ui.failed(result)` is the error message or `null`; `spacemix.ui.error(result)` renders it. Check every source before reading its fields.
- **No data.** `spacemix.ui.empty(text)` renders a quiet empty state; without `text` it says "Nothing here yet.".
- **A rejected `spacemix.call`.** Catch it and render `error(err.message)`.
- **Script errors.** A throw, or a promise rejected with no handler, is reported as the widget's error: cut to 1,000 characters on dashboards and in Team Chat (through `host.widgets.reportError`), whole in the Widgets gallery and the Studio. It shows in the widget's status until the widget's code changes, and the Studio offers **Ask to fix**.

Render the whole widget from one root on every update, as the starters do (`render("#w", …)`), so an error or empty state doesn't leave later data with nowhere to go.

## Saving data

Sources and `spacemix.call` read data that lives elsewhere. A widget that keeps data of its own saves it in its **store** (widget frame 2): one JSON object per widget on the server, shared by everyone who sees it, last write wins. It is also where the values of the widget's [settings](building.md#settings-and-saved-data) live.

| Use | For |
|---|---|
| Sources | Data from the person's tools or the web, read fresh for each viewer, cached. |
| `spacemix.call` | A read on demand: a filter, a page, a detail. |
| The store | What the widget itself saves and shows to everyone: ticks, notes, choices, settings. |

Read it from `spacemix.store`, save with `spacemix.save(value)` (replaces it) or `spacemix.save(value, { merge: true })` (sets only the top-level keys passed; `null` removes one), and redraw on `spacemix.onStore(fn)` when someone else changes it (the frame doesn't call it for its own saves). A complete checklist that saves its ticks, with a settings field and a `defaultStore`, is the last example in [`spacemix` in the widget](../reference/widgets.md#spacemix-in-the-widget).

- **Shared, not per viewer.** Unlike sources, the store is the same for everyone. Don't put one person's data in it.
- **Anyone who sees the widget can write it**, from the widget, its settings panel, or the assistant's `widget_store_set` (with approval). Treat what's in it as input, not as trusted.
- **Limits.** At most 256 KB as JSON. The server takes one write per widget every 200 ms and the app sends at most two a second from a window, folding saves in between; a write that meets another viewer's is retried. `spacemix.save` rejects with the reason, or after 10 seconds.
- **The viewer may be asked first.** Where widgets in the window hold data from a tool, a save needs the viewer's OK for this widget's code ([The viewer's OK](../reference/widgets.md#the-viewers-ok)), so it can reject while they decide or if they say no. Keep what they did on screen and say so; the next save works once they've said yes:

  ```js
  spacemix.save({ done }, { merge: true }).catch((err) => {
    note.textContent = `Not saved: ${err.message}`;
  });
  ```
- **Not part of the widget.** The store isn't in the versions or in a widget file; in the Studio a draft's preview works on a copy.

The exact rules, the error texts and the frame messages are in [The widget's store](../reference/widgets.md#the-widgets-store) (owned by `apps/desktop/src/lib/widgetStore.ts` and `apps/desktop/src-tauri/src/widget_store.rs`).

## Runs as the viewer

A widget's definition belongs to the team; its data belongs to whoever is looking. Sources and `spacemix.call` run in that person's client, with their access, and without asking for approval. (A web source is the exception: the server fetches it, with nothing of the viewer, so everyone sees the same answer.) So:

- Two people can see different data in the same widget, for example a widget over `chat_mentions` shows each reader their own mentions.
- A widget someone else made can read anything you can read with read-only tools, not only its sources. That is why opening someone else's never-saved widget, or importing a file, first shows what it reads and says it runs with your access.
- The cache is per person: one person's **Purge cache** doesn't touch anyone else's.

## Stability

**Stability:** experimental. What the frame hands the widget (`spacemix.data`, `spacemix.onData`, `spacemix.call` and its 10-second limit, and from version 2 `spacemix.store`, `spacemix.save` and `spacemix.onStore`) is the stable frame surface; see [`spacemix` in the widget](../reference/widgets.md#spacemix-in-the-widget). When sources run, the cache and its defaults are host behaviour in `apps/desktop/src/lib/widgetRuntime.ts` and may change; the reference for them is [The data cache](../reference/widgets.md#the-data-cache).
