Saving data: the box SDK
Every page on Makeship already has box loaded; you don't add a script tag. The first time the app saves or reads data, the visitor gets a guest account automatically.
The same code runs anywhere. Away from Makeship (a Claude preview, a file opened on your laptop) box saves to the browser's localStorage instead, so you can try an app before it's online. box.mode is "platform" on Makeship and "local" everywhere else.
// a collection is a named list of documents
const items = box.collection("groceries");
const milk = await items.add({ name: "Milk", done: false });
// -> { id, createdAt, updatedAt, name: "Milk", done: false }
const todo = await items.list({
where: { done: false },
orderBy: "-createdAt", // "-" for newest first
limit: 50,
});
await items.update(milk.id, { done: true }); // shallow merge
await items.get(milk.id); // the document, or null
await items.remove(milk.id);
// live: called now, then whenever the list changes (anyone's change, in a shared collection)
const stop = items.subscribe((docs) => render(docs), { where: { done: false } });
| Call | Does |
|---|---|
box.collection(name) | Opens a collection. Names use letters, numbers, - and _, up to 64 characters. Collections don't need creating first. |
add(data) | Saves a new document and returns it with id, createdAt and updatedAt added. |
list(options) | Returns documents, oldest first unless you set orderBy. |
get(id) | One document, or null. |
update(id, patch) | Merges the given fields into the document (top level only) and returns it. |
remove(id) | Deletes the document. |
subscribe(callback, options) | Calls callback with the documents (same options as list) right away and again whenever they change, including other visitors' changes to a shared collection. Returns a function that stops it. Add onError to the options to handle a failed refresh. |
list options
where: up to 10 fields. A value matches exactly ({ done: false });{ in: [...] }matches any of up to 50 values;{ gt, gte, lt, lte }compares numbers or strings.orderBy: a field name, with-in front for descending. Defaults tocreatedAt.limit: defaults to 100, at most 200.
Accounts
Every app shows a small account button in a corner. It opens a Makeship sheet where the visitor can add their email (to keep their account and open it on other devices), sign in, set a display name, sign out, delete their data, or report the app. The sheet is Makeship's own page, so the app can't change it.
const me = await box.account.current();
// -> { id, isGuest, email, displayName, isOwner }
box.account.open(); // open the sheet from your own Settings button
box.account.onChange((me) => render(me)); // after a claim, sign-in, sign-out or new name; null once signed out
Styling the account button
Make the button fit your app. Each call changes only the options it names.
box.overlay.configure({
position: "top-right", // bottom-right (default), bottom-left, top-left, top-right
offset: { x: 16, y: 12 }, // px from the corner, 0 to 200 (or one number for both)
size: 40, // px, 32 to 72; 44 by default
background: "#1d4ed8", // any CSS colour
color: "white", // icon and label
borderColor: "transparent",
label: "Account", // text next to the icon, up to 24 characters
shadow: false,
});
Using your own button instead
Turn the floating button off and open the sheet from your own menu, header or settings page. Start with it off by adding this to your page's <head> (nothing flashes on screen first):
<meta name="box:account-button" content="off">
Then pick any of these as the way in:
<a href="/_box/account">My account</a> <!-- opens the sheet; the full page if scripts are off -->
<button data-box-account>Settings</button> <!-- any element with data-box-account -->
menu.onclick = () => box.account.open(); // from your own code
box.overlay.hide() and box.overlay.show() do the same from code. Visitors must always be able to reach the sheet, because that's where they delete their data and report the app. If you hide the button, keep a visible way in on every screen, such as a menu item. Makeship's deploy check warns when the button is off and nothing in the app opens the sheet.
isOwneristruewhen you, the app's creator, are signed in with the email of your Makeship account. Use it to show editing screens, and mark the collections only you may change asowner-writein box.json. Your dashboard's app page has a link that signs you in. You can always get in, even when the app is full.- If the app doesn't call
onChange, the page reloads when the visitor switches to another account, so nobody sees the previous person's data. - Away from Makeship there is one local visitor, who counts as the owner, and no button.
Images visitors upload: box.files
For photos, avatars and other pictures, use box.files instead of putting images in documents:
const photos = box.files("photos");
const f = await photos.upload(input.files[0]);
// -> { id, url, name, type, size, createdAt, mine }
img.src = f.url; // served by Makeship from your app's address
await posts.add({ caption, photo: f.id }); // keep the id in a document
const all = await photos.list(); // newest first; { mine: true } for just the visitor's
await photos.remove(f.id);
- Images only: PNG, JPEG, GIF, WebP or AVIF, up to 10 MB each. Makeship checks the file itself, so a renamed file is refused, and SVG isn't allowed because it can carry code.
- Uploads count toward your plan's storage. A folder is private to each visitor unless box.json shares it under
"files". - You can delete any image in your app when you're signed in to it. If visitors see each other's images, put a Report link next to them:
/_box/report?file=plus the image's id. - Away from Makeship, uploads stay in the page's memory until it reloads.
Feedback from visitors: box.feedback
Give visitors a way to tell you what's broken or what they'd like. Call box.feedback from a form or button:
await box.feedback(text, { kind: "bug" }); // "feedback" (default), "bug" or "idea"
- You read it on your app's Feedback page in the dashboard, and your AI agent can read it with the connector's read_feedback. Makeship sees it too.
- Up to 4,000 characters and 10 messages a day per visitor. Pass
emailso they can hear back; a signed-in visitor's email is used if you don't. - When it isn't sent, it throws an error whose message says why, so show it.
- It isn't for safety reports: those go to Makeship through the account sheet's Report.
- Away from Makeship nothing is sent; the message shows in the browser console.
Good to know
- Store one document per item, not one big array, so two people editing at once don't overwrite each other.
- A document can be up to 64 KB, and an app can hold up to 10,000 documents.
id,createdAtandupdatedAtare set by Makeship and can't be written. - Every call returns a promise. A failed call throws an error whose message includes the reason, such as the app being full or the visitor saving too quickly.