---
title: The host object
summary: What `host` from `@spacemix/sdk` is, how every call crosses the frame bridge, how permissions are checked, and the errors and timeouts all host APIs share.
audience: both
stability: stable
since: protocol 1
---

# The host object

An extension's code runs in a sandboxed iframe with no network and no access to the app. The only way out is
`host`, exported by `@spacemix/sdk`:

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

`host` has the type `SpacemixHost`. Every method on it sends a request over `postMessage` to the SpaceMIX window
around the frame, which checks it against the extension's **approved** permissions and then runs it. See
[protocol.md](../protocol.md) for the wire format; this section documents what each call does.

| Namespace | Present | Page |
|---|---|---|
| `host.storage` | always | [storage.md](storage.md) |
| `host.collection(name)` | always | [collections.md](collections.md) |
| action handlers (`ExtensionModule.actions`) | always | [actions.md](actions.md) |
| `host.net` | with any `net:` permission | [net.md](net.md) |
| `host.mcp` | always (calls need a `net:` permission) | [mcp.md](mcp.md) |
| `host.fs` | with `fs` | [fs.md](fs.md) |
| `host.git` | with `git` | [git.md](git.md) |
| `host.chat` | with `chat` | [chat.md](chat.md) |
| `host.notifications` | always | [notifications.md](notifications.md) |
| `host.events` | always (`emit`); the rest need `events` | [events.md](events.md) |
| `host.tools` | with `tools` | [tools.md](tools.md) |
| `host.ai` | with `ai:invoke` | [ai.md](ai.md) |
| `host.widgets` | with `widgets` or `widgets:view` | [widgets.md](widgets.md) |
| `host.shell` | always | [shell.md](shell.md) |
| `host.view` | always | [view.md](view.md) |
| `host.env`, `host.manifest` | always | [env.md](env.md) |
| `navigator.mediaDevices` | with `camera`, `microphone` or `screen` | [media.md](media.md) |

The React hooks in `@spacemix/sdk/react` (`useCollection`, `useStored`, `useRoute`, …) are built on these APIs;
see [react.md](../react.md).

## When `host` is usable

```ts
export const host: SpacemixHost;
```

The frame runtime connects `host` with the `init` message **before** it runs the extension's bundle. Inside a frame,
`host` works from the first line of extension code: module scope, React components, action handlers and the `mcp`
resolver all see the same object.

**The import-time rule.** Outside a frame, `host` is not connected and *any* property access on it throws
`The SpaceMIX host isn't connected: extension code only runs inside its frame.` This happens when something
imports your module outside a frame: the build script (`scripts/build-extensions.mjs`) runs the bundle with
stand-ins to read the manifest, and so does the Studio's probe frame. So:

- Keep the manifest free of `host` (build it with `defineExtension` from plain values).
- Don't touch `host` at module scope in files the manifest imports. Read it inside functions, components and
  handlers.

`"fs" in host` and `Object.keys(host)` also throw when not connected. A connected `host` reports an optional
namespace as present exactly when its value is defined (`"net" in host` is false without a `net:` permission).

## Optional namespaces follow approved permissions

The optional namespaces (`fs`, `git`, `chat`, `net`, `widgets`, `tools`, `ai`) are `undefined` unless the
permission is **approved** for this install. The host builds `host` from the installed record, not from what the
package asks for, and `host.manifest.permissions` is that approved list too. An update that asks for new
permissions runs with the old ones until someone who manages extensions on the server approves it (see
[permissions.md](../permissions.md#updates)).

Always reach optional namespaces with optional chaining or a check:

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

export async function listRoots(): Promise<string[]> {
  if (!host.fs) return [];
  return (await host.fs.roots()).map((r) => r.name);
}
```

## How a call is checked

Every method call becomes one bridge request: `{ type: "req", method, args }`, where `method` is the dotted name
(`storage.get`, `fs.read`, `collection.list`). The host then, in order:

1. Looks the method up in its table. Unknown: rejects with `SpaceMIX has no "<method>".`
2. Checks the method's permission against the approved permissions. Missing: rejects with
   `<Extension name> doesn't have the "<permission>" permission.` The permission is spelled `net:` for every
   `net:*` method, `ai:invoke` for `ai.*`, `camera` for every `media.*` method (any of `camera`, `microphone`,
   `screen` passes) and `widgets:view` for methods either widgets permission allows.
3. Runs the method. Its own checks (an undeclared collection, a host outside the `net:` permission, a file outside
   the server's files folder) reject with a readable sentence.

There is no other way in. Arguments and results must survive the structured clone: plain data, `ArrayBuffer`,
`Uint8Array` and (for `fs.import`) `File` objects. Functions, DOM nodes and class instances with methods fail with
`Can't send "<method>" to SpaceMIX: <reason>`. A result the host can't clone is sent as its JSON; if that fails
too the call rejects with `The result can't be sent to the extension: <reason>`.

Trailing `undefined` arguments are dropped before sending, so `host.fs.list(root)` and
`host.fs.list(root, undefined)` are the same request.

## Errors

Every host method returns a promise that rejects with a plain `Error` whose `message` is one readable sentence,
meant to be shown to the user or handed to the model as is. Error messages are **not** part of the contract: don't
parse them. The only exception is media, where errors are `DOMException`s with standard names (see
[media.md](media.md)).

A few methods throw synchronously instead of rejecting:

- `host.collection(name)` throws when the manifest declares no collection `name`.
- Any `host` access outside a frame (see the import-time rule).

## Timeouts

The frame gives each request 60 seconds before rejecting with `SpaceMIX didn't answer "<method>" within 60
seconds.` These methods get longer, because they wait on the user or the network:

| Method | Limit |
|---|---|
| `net.fetch` | none in the frame (the request itself has its own timeout, see [net.md](net.md#hostnetfetch)) |
| `fs.import`, `git.fetch`, `mcp.callTool`, `tools.call`, `ai.complete` | 10 minutes |

The other direction has a limit too: when the host runs one of the extension's actions, or asks for its MCP
servers, the handler must finish within 60 seconds (see [actions.md](actions.md)).

## Subscriptions

Live APIs (`host.storage.subscribe`, `host.fs.subscribe`, `host.git.subscribe`, `host.chat.subscribe`,
`host.fs.onDragDrop`, `host.events.subscribe`, `host.widgets.subscribe`, `host.widgets.watch`, and the hooks in
`@spacemix/sdk/react`, such as `useCollection` on the `records` channel) all use two bridge methods, `subscribe`
and `unsubscribe`:

```ts
// Bridge methods, not called directly by extensions:
// "subscribe"   (channel: SubscriptionChannel, params: Record<string, unknown>) → subscription id
// "unsubscribe" (id: number) → void
```

Each returns an unsubscribe function synchronously; the host subscription opens in the background. If opening
fails (for example `host.events.subscribe` without the `events` permission), **the failure is only logged to the
console** as `[spacemix] couldn't subscribe to "<channel>": <reason>`, and the callback is never called. Check the
permission yourself when it matters.

The channels, each with the permission it needs:

| Channel | Params | Payload | Needs |
|---|---|---|---|
| `stored` | `{ key }` | the key's new value | nothing |
| `records` | `{ name }` | one record change `{ kind, id, record? }`, or `null` after a resync | nothing (the collection must be declared) |
| `fs` | none | none | `fs` |
| `dragdrop` | none | `DragDropEvent` | `fs` |
| `git` | none | none | `git` |
| `chat` | none | `ChatEvent` | `chat` |
| `widgets` | none | none | `widgets` or `widgets:view` |
| `widget-data` | `{ widgetId }` | `WidgetUpdate` | `widgets` or `widgets:view` |
| `events` | none | `SpacemixEvent` | `events` |
| `media` | `{ session }` | `MediaWire["event"]` | `camera`, `microphone` or `screen` |

An unknown channel rejects with `There's no "<channel>" to subscribe to.` Subscriptions end when the frame is
dropped (uninstall, update, reload).

## The frame's lifetime

A frame runs only in the windows of people who may use the extension (see
[Who can use an extension](../permissions.md#who-can-use-an-extension)); when someone loses access, their windows
drop it. It is created the first time its view opens, the assistant (or `host.tools.call`) runs one of its actions, or
the assistant asks for its MCP servers. It then stays alive, hidden, for the life of the window, so module state
survives switching views. Extensions with the `events` permission are started in the background when someone who
may use them signs in, or gets access later. A frame is dropped and started fresh on uninstall, update and the store's Reload; pending calls then reject
with `<Extension name> was reloaded.`

Every open SpaceMIX window and browser runs its own copy of the frame. Code that must happen once (send a mail
for an event) has to coordinate; see `host.events.claim` in [events.md](events.md#hosteventsclaim).

## Low-level exports (internal)

`@spacemix/sdk/runtime` exports `request`, `subscribe`, `connect` and `hostIfConnected`, and
`@spacemix/sdk/protocol` exports the wire types. They are **internal**: the frame provides `@spacemix/sdk` and
`@spacemix/sdk/react` at runtime (see `HOST_MODULES` in [package-format.md](../package-format.md)), but not
`@spacemix/sdk/runtime` or `@spacemix/sdk/protocol`. A bundle that imports them gets its own, unconnected copy.
Use `host` and the hooks.

## Stability

`host`, `SpacemixHost` and the namespaces marked `stable` on their pages are covered by
[contract.md](../../contract.md). Pages marked `experimental` say why.
