# ctx.storage

Private JSON on this person’s Fave1 account, per key. Nobody else reads it — not other apps, not other people. It follows them to every site and to your app in app mode.

storage

## Calls

`ctx.storage`

```ts
await ctx.storage.get(key);          // the value, or undefined
await ctx.storage.set(key, value);   // any JSON value but null, up to 64 KB — delete() clears a key
await ctx.storage.delete(key);
await ctx.storage.list();            // the keys, sorted
await ctx.storage.usage();           // { bytes, limit } — how full your app’s share is
await ctx.storage.prefs.get();       // a small settings object — never asks
await ctx.storage.prefs.set(obj);    // up to 2 KB, readable on any page your app opens on
await ctx.storage.append(key, item); // add one row to a list — asks once for your app, not per site
await ctx.storage.askAppend();       // ask for that one yes now — true once it's given
await ctx.storage.forgetAppend();    // the next append() on a site asks again
```

## It lives on the account

What someone saves on etsy.com is there when your app opens on amazon.com, and on the app’s own page at `<id>.fave1.app` — one account, one store per app. That is what makes app mode possible: notes from every site, one list.

Nobody signed in means nowhere to save. `ctx.can('storage')` is `false` then — fold the feature and keep the rest of the app working.

Not even you can read it. There’s no developer view of what people save — your code reads it on their device, with their session.

## The first use on a site

The first time your app touches the account from a site — even just reading — Fave1 asks once, in your panel: **Use your Fave1 account here?** The button is Fave1’s own, so a page can’t press it and can’t read anyone’s notes. Every call after that on that site goes straight through, and the same answer covers [ctx.files](https://developers.fave1.com/docs/ctx-files.md) and [ctx.pub](https://developers.fave1.com/docs/ctx-pub.md). In app mode it never asks. To know before you call, check [ctx.user.granted](https://developers.fave1.com/docs/ctx-user.md#granted). Settings you want everywhere from the first second — a pen color, a switch — go in [prefs](https://developers.fave1.com/docs/ctx-storage.md#prefs), which never asks.

So the first call can take as long as a person takes to decide, and can reject with `cancelled`. Treat that as a normal answer.

To let one person hand a value to others by link — a note, a list — see [ctx.shared](https://developers.fave1.com/docs/ctx-shared.md); it rides on the same permission.

## Settings that never ask

`prefs` is one small object per app for settings — the pen color, a switch someone flipped, the last tab. It never asks, on any site, so your app can open with the person’s own settings everywhere, even on a site where they haven’t said yes yet.

`src/index.ts`

```ts
// Open with what this browser remembers, then catch up with the account.
let prefs = JSON.parse(localStorage.getItem('my-app.prefs') || '{}');
const saved = ctx.can('storage') ? await ctx.storage.prefs.get() : null;   // null if none or signed out
if (saved && (saved.t ?? 0) > (prefs.t ?? 0)) prefs = saved;

// Someone flips a switch:
prefs = { ...prefs, compact: true, t: Date.now() };
localStorage.setItem('my-app.prefs', JSON.stringify(prefs));
await ctx.storage.prefs.set(prefs);
```

> **Never put anything private in prefs.** Because it never asks, any page your app opens on can read it and change it. A color, a size, a tab — yes. A name, a note, an address, a token — no; those go in `get` · `set`.

> **Don’t keep anything you can’t afford to lose in prefs, either.** A page can also overwrite it — reset a score, empty a balance. Points, coins, levels and progress belong in `get` · `set`, where only a site the person said yes to can write. If you show them on every site from the first second, keep a copy in prefs for display and treat the private key as the real one.

| Rule | What it means | Error |
| --- | --- | --- |
| `2 KB` | One plain object, measured as JSON text. `set(null)` clears it. | `too-large · invalid` |
| `get()` | The value as it was when your app opened, plus what this tab `set()` since — a change from another tab or site shows up next time your app opens. `null` before the first `set`, when nobody is signed in, when storage was switched off in Settings, or when Fave1 couldn’t be reached — never a prompt, never a wait for a tap. Rejects when `storage` isn’t declared, or when your app opened with it already switched off — check `ctx.can('storage')` first | `denied` |
| `set()` | Needs a signed-in person who added your app, and the storage permission | `signed-out · not-installed · denied` |
| `usage()` | Not counted | — |

Clearing your app in Fave1 Settings › Storage clears prefs too, and removing the app deletes it. A DEV build keeps it in this browser.

## A history from every site

Some apps fill one list from wherever they’re used — a share history, a reading log, a list of things you sent. Asking on every new site would wear people out, so `append()` asks once for your app: **Keep your history in \<app>?** It can only add a row to the end of a list. It returns nothing and reads nothing, which is why one yes is safe for every site. Reading the list, editing it and deleting rows are ordinary `get` · `set` · `delete` — in app mode they never ask.

`src/index.ts`

```ts
// When your panel opens on a site — ask now, while they're looking at it:
const keep = await ctx.storage.askAppend({ what: 'share history' }).catch(() => false);

// Each time someone sends something:
if (keep) await ctx.storage.append('sent:2026-11', { url: location.href, to: 'x' }, { what: 'share history' });

// In app mode — read it back. Each row is { at, site, item }.
const rows = (await ctx.storage.get('sent:2026-11')) as { at: string; site: string | null; item: { url: string } }[] | undefined;
for (const r of rows ?? []) console.log(r.at, r.site, r.item.url);

// Someone turns the history off:
await ctx.storage.forgetAppend();
```

Ask with `askAppend()` when your panel opens, not inside the click that opens another window — the question shows up on the page they just left and nobody sees it. It returns `true` at once when the yes is already there (and always in app mode), `false` when they say not now, and rejects with `cancelled` if your panel closes before they answer. It asks about the history itself — even on a site that already allowed your app, where `append()` would go straight through — so call it once, when your history feature first matters.

Fave1 writes `at` (its own clock) and `site` (the host of the page the row came from — `null` in app mode, `'local'` for a file opened from their computer). Your code can’t set either. Because anyone’s page can add a row once the person said yes, check that a row’s link matches its `site` before you trust it.

| Rule | What it means | Error |
| --- | --- | --- |
| `4 KB` | Per row, measured as JSON text. Not `null`. | `too-large · invalid` |
| `64 KB` | The whole list, like any value — past it, the oldest rows drop off. A key per day or month keeps lists short. | — |
| `a list` | If the key already holds something that isn’t a list | `invalid` |
| `one yes` | Asked by `askAppend()`, or on the first `append()` on a site. `opts.what` names it in the prompt — up to 24 letters and spaces; anything else falls back to the default `history`. Turning storage off in Settings, or `forgetAppend()`, takes it back. A site that already allowed your app never asks. | `cancelled` |
| `their storage` | The list counts toward your app’s data and their Fave1 storage, like any value | `quota` |

## Patterns that hold up

- **One key** (blob) — `settings` → `{ theme, sort }` when the whole thing is small and always read together.
- **A key per item** (records) — `note:<id>` plus an `index` array when the list grows. Don’t rewrite a big blob on every save.
- **Ids, not bytes** (files) — Images and exports go in [ctx.files](https://developers.fave1.com/docs/ctx-files.md). Keep their ids here.

## Limits

| Limit | What happens | Error |
| --- | --- | --- |
| `64 KB` | Per value, measured as JSON text | `too-large` |
| `1 MB` | Per app, per person, on Free — 10 MB on paid plans; `usage()` tells you theirs. Plan for 1 MB: keep records small, and put files in [ctx.files](https://developers.fave1.com/docs/ctx-files.md). | `quota · app` |
| `25 MB` | Their app data on Fave1, every app together, on Free — 250 MB on paid plans | `quota · account` |
| `their storage` | Everything they save in Fave1 — app data, files and [shared](https://developers.fave1.com/docs/ctx-shared.md) values, every app. Their own amount, 1 GB to start. Reading and deleting always work, even when it’s full. | `quota · account` |
| `200 chars` | Per key | `invalid` |

## How full it is

`usage()` says where your app stands for this person. `bytes` is what your app keeps on their account, counted the way `set()` counts it — each value as JSON text. `limit` is your app’s share on their plan. Ask instead of writing 1 MB into your code: the share depends on their plan, and plans change.

`src/index.ts`

```ts
const { bytes, limit } = await ctx.storage.usage();
if (bytes > limit * 0.9) {
  // Almost full — drop the oldest entries now, before a save fails.
}
```

Or show it, so people know where they stand:

`src/index.ts`

```ts
const size = (n: number) =>
  n < 1024 * 1024 ? `${Math.round(n / 1024)} KB` : `${+(n / 1024 / 1024).toFixed(1)} MB`;

const { bytes, limit } = await ctx.storage.usage();
ctx.ui.render([ctx.ui.text(`${size(bytes)} of ${size(limit)}`)]); // 412 KB of 1 MB
```

Files don’t count here — they go to the person’s Fave1 storage, which you handle with `quota` (scope `'account'`) and `ctx.files.manage()`. And it only speaks for your app: not other apps, not their whole storage, not their plan.

Same rules as `list()`: it needs a signed-in person, asks once on a new site, and never asks in app mode. A DEV build reports this browser’s sandbox and 1 MB.

## When it’s full

A `quota` error says which limit with `err.scope`. `'app'` — your app’s own share is full: 1 MB on Free, 10 MB on paid plans. Let them delete something in your app first; on Free, a paid plan also lifts the share to 10 MB, and `ctx.files.manage()` opens their storage. `'account'` — their Fave1 storage is full, or their app data across every app is: offer `ctx.files.manage()`, where they can clear it. An older runtime may leave the scope out; read that as `'account'`.

`src/index.ts`

```ts
try {
  await ctx.storage.set(`note:${id}`, note);
} catch (err) {
  const e = err as Fave1Error;
  if (e.code !== 'quota') throw err;
  if (e.scope === 'app') {
    // Your app's own share is full — make room in your app first.
    ctx.ui.toast('Too many notes. Delete a few to keep going.', 'error');
  } else {
    // Their Fave1 storage is full.
    ctx.ui.render([
      ctx.ui.text('Your Fave1 storage is full.'),
      ctx.ui.button('Make room', { onClick: () => ctx.files.manage() }),
    ]);
  }
}
```

A write that’s the same size or smaller always goes through, even over a limit — someone who’s full can still trim a record or drop an item from an index. Deleting always works.

## They can clear it

People see what each app keeps in Fave1 Settings › Storage and can delete it whenever they like — your app isn’t told. A key you saved can come back `undefined`, your index included, and a [shared](https://developers.fave1.com/docs/ctx-shared.md) link they cleared answers `null` from then on. Treat that like a first run, not an error.

## Errors

| Code | What happened | Try |
| --- | --- | --- |
| `signed-out` | Nobody’s signed in to Fave1 here | fold the feature |
| `denied` | `storage` isn’t in your manifest, or they switched it off | ctx.perms.open() |
| `not-installed` | They haven’t added your app — a sideloaded run | say so |
| `cancelled` | They chose Not now, or closed the app while asked | leave it |
| `invalid` | A key over 200 characters, a value that isn’t JSON, or `append()` to a key that isn’t a list | fix it |
| `too-large` | A value over 64 KB, a row over 4 KB, prefs over 2 KB | split it |
| `quota` | Full — `err.scope` `'app'` or `'account'` ([When it’s full](https://developers.fave1.com/docs/ctx-storage.md#full)) | make room |
| `unreachable` | This page blocks Fave1 from reaching their account | say so |
| `timeout` | No answer in 15 seconds | retry |
| `server` | Something broke on our side | retry |

## Without the permission

Undeclared, or switched off in settings — every call rejects with a typed `denied` error. Check `ctx.can('storage')` and fold the feature; [Permissions](https://developers.fave1.com/docs/permissions.md) has the full contract. The same switch covers [ctx.files](https://developers.fave1.com/docs/ctx-files.md), [ctx.shared](https://developers.fave1.com/docs/ctx-shared.md) and [ctx.stickers](https://developers.fave1.com/docs/ctx-stickers.md). Switching it off keeps the data, files and links; removing the app deletes them.

## While you build

> **DEV builds start in a sandbox.** Run from `fave1 dev` or a review link, `ctx.storage` keeps values in this browser, on this site — nothing touches your real account, unless you switch Data to [My account](https://developers.fave1.com/docs/quickstart.md#dev-account). The sandbox holds your app to 1 MB — the Free share, the one to design for — so `quota` with scope `'app'` shows up as it would for real.

To try a full account, give the sandbox a size. Everything it holds on that site, files included, counts against it — past that, saves reject with `quota`, scope `'account'`.

`console — on the site you test on`

```ts
localStorage.setItem('fave1.dev.quota', '200000'); // bytes
localStorage.removeItem('fave1.dev.quota');         // back to no account limit
```
