---
title: Storage
summary: "`host.storage`: a key/value store under the extension's own id, shared by everyone on the server who may use the extension and live across windows."
audience: both
stability: stable
since: protocol 1
---

# Storage

`host.storage` keeps small values under the extension's own id: settings, connection details, a cursor, which item
is selected, a draft. It is not for records. Every list of records goes in a [collection](collections.md), which is
a table of its own on the server, gets tools for the assistant and stays live in the view; the assistant can't see
`host.storage`, and a value is capped at 64 KB.

```ts
interface SpacemixHost {
  storage: {
    get<T>(key: string): Promise<T | undefined>;
    set<T>(key: string, value: T): Promise<void>;
    subscribe<T>(key: string, fn: (value: T | undefined) => void): () => void;
  };
}
```

**Permission:** none. Every extension has `host.storage`. (The `storage` permission exists in the `Permission`
type and manifests declare it by convention, but the host doesn't check it; see
[permissions.md](../permissions.md).)

**Scope.** Keys live in the extension's own namespace (its manifest id). Another extension can't read or write
them, and you can't reach anyone else's. The store belongs to the **server**, not to one person: everyone on the
team who uses the extension sees the same values. Someone the server doesn't let use the extension can't read or
write them at all (`You don't have access to <id>.`; see
[Who can use an extension](../permissions.md#who-can-use-an-extension)). Don't put one person's secrets in
`host.storage` unless the extension is meant to share them with the team. While a paid extension is turned off on
the server, `set` rejects with `<Name> is turned off on this server until it's paid for.` and the values stay
readable and kept (see [Turned off until it's paid for](../permissions.md#turned-off-until-its-paid-for)).

Values are stored as JSON (`JSON.stringify`). `undefined` fields disappear, `Date` becomes a string, `Map` and
`Set` become `{}`. Store plain data.

**How it's stored** (informational, not part of the contract): on the server, the extension's keys are rows of
its own table, `<extension id>.storage` (e.g. `ai.spacemix.twenty.storage`), next to its collections' tables
(`<extension id>.<collection>`), so every table an extension has carries its id. The one exception is the team
chat, whose tables carry the id `ai.spacemix.chat` but belong to the core (see
[chat.md](chat.md#notifications-and-events)). The owner sees them in Settings → Database.

## `host.storage.get`

```ts
get<T>(key: string): Promise<T | undefined>;
```

The stored value, or `undefined` if nothing was ever stored under `key`. Served from a shared cache in the
SpaceMIX window after the first read, so repeated reads are cheap.

Bridge method: `storage.get`. Errors: `The key must be a string.`

## `host.storage.set`

```ts
set<T>(key: string, value: T): Promise<void>;
```

Stores `value` under `key` and pushes it to every subscriber of the key, in this frame at once and in other windows
and browsers through the server's change feed.

- `set` resolves once the SpaceMIX window has the value. Writing it to the server happens in the background; a
  write the server refuses is logged by the host and `set` still resolves, but the write is rolled back: the key
  returns to the last value the server confirmed (or to nothing), and subscribers are called with that value (or
  `undefined`), see [`host.storage.subscribe`](#hoststoragesubscribe). `set` rejects only for the size error below.
- There is no delete. Store `null` (or an empty value) instead.
- The last write wins. Two windows writing the same key at the same time don't merge.
- **Size:** a value may be at most 64 KB as JSON (`JSON.stringify`, measured in UTF-8 bytes). A larger value makes
  `set` reject with `host.storage holds small values (settings, a selection, a draft) up to 64 KB; a list of records
  belongs in a collection, which is its own table.` and nothing is written.

Bridge method: `storage.set`. Errors: `The key must be a string.`, the size error above.

## `host.storage.subscribe`

```ts
subscribe<T>(key: string, fn: (value: T | undefined) => void): () => void;
```

Calls `fn` with the new value every time `key` is written: by this frame, by another window or browser, or by the
extension's own actions running elsewhere. It does not call `fn` with the current value; call `get` for that, or
use `useStored` from `@spacemix/sdk/react`, which does both (see [react.md](../react.md)). Returns the unsubscribe
function. Every reader of one key in a frame shares a single host subscription.

A write from this frame reaches this frame's subscribers immediately, before the host confirms it, and is not
echoed back a second time. If the server then refuses it, every subscriber of the key, in this frame too, is called
again with the value the server last confirmed, or with `undefined` when it had none. Only the latest write to a key
rolls back (`writeStored` in `apps/desktop/src/lib/useStored.ts`).

Channel: `stored` with `{ key }` (see [index.md](index.md#subscriptions)).

## Example

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

interface Settings {
  baseUrl: string;
  pageSize: number;
}

export async function loadSettings(): Promise<Settings> {
  return (await host.storage.get<Settings>("settings")) ?? { baseUrl: "", pageSize: 20 };
}

export async function saveSettings(s: Settings): Promise<void> {
  await host.storage.set("settings", s);
}

export function watchSettings(fn: (s: Settings | undefined) => void): () => void {
  return host.storage.subscribe<Settings>("settings", fn);
}
```

## Stability

Stable. The key/value layout behind it (namespace names, the `kv:` prefix the host adds) is not part of the
contract.
