shed
Reference / SDK reference
Reference

SDK reference

Every call your page can make. Load one script and your app gets the signed-in person, their role, a shared database and live updates.

View Markdown

Setup

Load the SDK

Every Shed app serves the SDK at /_shed/sdk.js, on its own address. Add the script tag before your own code and a global shed object is ready to use. There's nothing to install or configure, and no keys: the page already knows which app it's in and who is signed in.

The SDK is MIT-licensed, so your app's code doesn't inherit the server's license.

index.html
<script src="/_shed/sdk.js"></script>
<script type="module">
  const { user, role } = await shed.me();
</script>

Sign-in happens before your page

Shed signs people in before it serves a private app, so your code never needs a sign-in screen. On an app shared with anyone who has the link, visitors may not be signed in: shed.me() returns user: null and you can offer shed.login().

Everything returns a promise. Use await inside a <script type="module"> or an async function.

HTML
<script type="module">
  const { user } = await shed.me();
  if (!user) showSignInButton(); // only on public apps
</script>

People and roles

shed.me()

Who is looking at the page, their role in this app, and the app itself.

Fetched once per page and cached, so call it as often as you like.

FieldType
user{ email } or nullnull for visitors who haven't signed in, on public apps. Someone already signed in to Shed is recognised when they open a public app, so null means a real stranger: offer shed.login().
rolestringviewer, editor or owner.
app.slugstringThe app's name, as in its address.
app.namestringIts display name.
app.ownerstring or nullThe owner's email. Use it to tell people who to ask for access. null for visitors who haven't signed in, so a public app never shows them anyone's email.
JavaScript
const { user, role, app } = await shed.me();
Returns
{
  "user": { "email": "sam@example.com" },
  "role": "viewer",
  "app": { "slug": "standup", "name": "Standup", "owner": "ana@example.com" }
}

shed.can(action)

Whether the person may do something. Returns true or false.

actionTrue for
'view'viewers, editors and owners
'edit'editors and owners (the default)
'own'owners

Use it to hide controls the person can't use. The server checks the role on every write either way.

JavaScript
if (await shed.can('edit')) showForm();
else {
  const { app } = await shed.me();
  showNote(`To add your own, ask ${app.owner} for edit access.`);
}

shed.login()

Sends the visitor to sign in, then back to the page they were on. Only needed on apps shared with anyone who has the link.

JavaScript
signInButton.onclick = () => shed.login();

shed.logout()

Ends the person's session in this app and goes to the app's home page. On a private app, that passes through sign-in again, and someone still signed in to Shed comes straight back in. To sign out everywhere, sign out of Shed itself.

JavaScript
signOutButton.onclick = () => shed.logout();

Data

shed.db(name)

A collection of JSON records, shared by everyone with access to the app. It exists as soon as you write to it. Calling shed.db does no network work, so make one per collection at the top of your script.

Names are 1 to 64 letters, digits, _ or -.

JavaScript
const ideas = shed.db('ideas');
const votes = shed.db('votes');

The record object

Every record you get back has your fields, plus:

Field
idThe record's id: 1 to 128 letters, digits, _ or -.
_meta.created_byEmail of whoever created it, or null if an anonymous visitor did.
_meta.updated_byEmail of whoever last changed it.
_meta.created_atWhen it was created, in milliseconds since 1970.
_meta.updated_atWhen it last changed.

Visitors who haven't signed in see a pseudonym such as person-3f9a1c0e instead of each email in _meta, in records and in live updates. It is the same for the same person, so you can still group by it. Shed can't see inside your fields, so don't store emails in a record of a public app.

Shed maintains id and _meta; anything you send in them is ignored. A record is at most 256 KB of JSON.

A record
{
  "id": "dark-mode",
  "text": "Dark mode",
  "votes": 3,
  "_meta": {
    "created_by": "pam@example.com",
    "updated_by": "lee@example.com",
    "created_at": 1764720000000,
    "updated_at": 1764723600000
  }
}

collection.add(record)

Creates a record and returns it. Shed picks a random id unless the record has a valid id of its own. Needs edit access.

JavaScript
const idea = await ideas.add({ text: 'Dark mode', votes: 0 });
idea.id; // "3f1c9a2e-…"

await ideas.add({ id: 'dark-mode', text: 'Dark mode' });

collection.put(id, record)

Creates the record with this id, or replaces it entirely. Returns the record. Needs edit access.

Because the same id always means the same record, put is how you make "one per person" data, like a vote.

JavaScript
await votes.put(`${ideaId}--${userKey}`, { idea: ideaId });

collection.update(id, fields)

Changes only the fields you pass and keeps the rest. Returns the whole record. Fails with 404 if there's no such record. Needs edit access.

JavaScript
await ideas.update('dark-mode', { votes: 4 });

collection.remove(id)

Deletes a record. Fails with 404 if there's no such record. Needs edit access.

Any editor can remove any record, unless the collection has the author rule in shed.json: then only the person who added a record (or an owner) can change or remove it, and the server enforces it. See Only authors change their records. Hiding the button in your page isn't enough on its own, since any editor can call the API.

JavaScript
await ideas.remove('dark-mode');

collection.get(id)

One record, or null if there's no record with that id.

JavaScript
const idea = await ideas.get('dark-mode');
if (!idea) showMissing();

collection.list(options)

Every record in the collection, oldest first. Returns up to 1,000 records; pass limit for up to 5,000.

For a list that stays up to date, use subscribe instead.

JavaScript
const all = await ideas.list();
const more = await ideas.list({ limit: 5000 });

Only authors change their records

By default any editor can change or delete any record. To let editors add records but change only their own, name the collection in the app's shed.json and deploy:

Shed then refuses put, update and remove on an updates record from anyone but the person who created it, or an owner, with a 403 ("only the person who added a record can change it"). Other collections are unaffected. A deploy without rules keeps the current ones; "rules": null removes them. The page sees them in shed.me() as app.rules, so it can hide buttons people can't use:

shed.json
{ "name": "standup", "rules": { "updates": "author" } }
JavaScript
const me = await shed.me();
const canChange = (r) => me.role === 'owner' || r._meta.created_by === me.user?.email;

Live updates

collection.subscribe(fn)

Calls fn(records, change) straight away with every record, then again after every change by anyone, in any tab. Returns a function that stops it.

change is null the first time, then describes what happened:

Field
op'put' for a create or change, 'delete' for a delete.
collectionThe collection's name.
idThe record's id.
recordThe record after the change. Absent for deletes.
byEmail of whoever made the change, or null.

Your own changes come back through subscribe too, so render from it and you never need to update the screen by hand after a write.

JavaScript
const stop = ideas.subscribe((records, change) => {
  render(records);
  if (change?.op === 'put' && change.by !== myEmail) flash(change.id);
});

// later
stop();

shed.on(fn)

Calls fn(change) after every change to any collection in the app. Returns a function that stops it. Useful for an activity feed.

JavaScript
shed.on((change) => log(`${change.by} changed ${change.collection}/${change.id}`));

Errors

Error handling

Every call that fails throws an Error whose message is readable and whose status is the HTTP status. get returns null for a missing record instead of throwing.

statusMeaningWhat to do
400A bad collection name or id, or a record that isn't a JSON object.Fix the call.
401Not signed in.Call shed.login().
403Signed in, but a viewer tried to write.Hide the control; tell them who to ask.
404update or remove on a record that doesn't exist.Usually safe to ignore.
413The record is over 256 KB, or the app has 50,000 records.Store less per record, or clean up.
JavaScript
try {
  await ideas.add({ text });
} catch (e) {
  if (e.status === 403) showNote('You can view but not edit.');
  else showNote(e.message);
}

Without the SDK

The HTTP API

The SDK is a thin wrapper over a JSON API on the app's own address. Use it directly from any code that runs in the app's pages. Every request needs an X-Shed header, which keeps other websites from calling it.

RequestDoes
GET /_shed/meshed.me()
GET /_shed/db/<collection>?limit=list → { records: [...] }
GET /_shed/db/<collection>/<id>get
POST /_shed/db/<collection>add → 201
PUT /_shed/db/<collection>/<id>put
PATCH /_shed/db/<collection>/<id>update
DELETE /_shed/db/<collection>/<id>remove → { deleted: id }
GET /_shed/eventsServer-sent events, one data: line per change

Errors come back as { "error": "message" } with the status above.

JavaScript
const r = await fetch('/_shed/db/ideas', {
  method: 'POST',
  headers: { 'X-Shed': '1', 'Content-Type': 'application/json' },
  body: JSON.stringify({ text: 'Dark mode' }),
});
const idea = await r.json();

new EventSource('/_shed/events').onmessage = (m) => {
  const change = JSON.parse(m.data);
};

↑↓ to move, Enter to open