Build an app

From a folder to an app running in a store's admin, in six steps. You need the cartbase CLI signed in to the store (cartbase login).

An app is a manifest, cartbase.app.json, and whatever storefront code the project needs under apps/<key>/. The app ships no admin code:

  • its records become record types the platform stores, validates and lists in the admin under the app, and serves to the storefront when public;
  • its settings become the app's Settings screen, committing as the merchant leaves each field; a secret is stored encrypted;
  • its screens appear under the app in the sidebar and on the Apps page;
  • its functions are compiled by the CLI and run by the platform on the events and schedules it declares, or on request; every run lands on the store's execution ledger;
  • its storefront files are recorded so an uninstall removes exactly them.

The reference chapters: app-manifest.md for every field, app-runtime-api.md for the app's own API, app-cli.md for the commands, apps.md for the storefront's reads.

1. Start the folder

# doc-noexec
cartbase app create store-locator --name "Store locator" --description "Partner shops on a map"

Inside a storefront project this writes apps/store-locator/; anywhere else ./store-locator/. Three files: cartbase.app.json (the app), AGENTS.md (the brief a coding agent reads) and README.md.

2. Describe the app

Edit cartbase.app.json. Every field is in app-manifest.md. A complete example:

{
  "key": "store-locator",
  "name": "Store locator",
  "description": "Partner shops on a map",
  "version": "1.0.0",
  "icon": "pin",
  "records": [
    {
      "type": "location",
      "name": "Location",
      "name_plural": "Locations",
      "visibility": "public",
      "fields": [
        { "key": "name", "name": "Name", "kind": "text", "required": true },
        { "key": "address", "name": "Address", "kind": "text", "required": true },
        { "key": "city", "name": "City", "kind": "text", "required": true },
        { "key": "point", "name": "Point", "kind": "coordinates", "required": true },
        { "key": "phone", "name": "Phone", "kind": "text" },
        { "key": "hours", "name": "Opening hours", "kind": "rich_text" }
      ],
      "columns": ["name", "city", "phone"],
      "max_records": 1000
    }
  ],
  "settings": [
    { "key": "maps_key", "name": "Maps API key", "kind": "secret", "description": "From your maps provider's console" },
    { "key": "default_zoom", "name": "Default zoom", "kind": "number", "default": 7 }
  ],
  "fields": [
    { "object": "product", "key": "available_at", "name": "Available at", "kind": "metaobject_reference", "list": true, "record": "location" }
  ],
  "scopes": [
    { "area": "products", "access": "view", "reason": "Shows which shops carry each product" },
    { "area": "orders", "access": "view", "reason": "Reads the order a pickup is booked for" }
  ],
  "functions": [
    { "key": "notify_shop", "entry": "functions/notify-shop.ts", "name": "Notify the shop" }
  ],
  "events": [
    { "event": "order.placed", "function": "notify_shop" }
  ],
  "egress": ["hooks.slack.com"],
  "storefront": {
    "files": ["apps/store-locator/storefront/StoreLocator.tsx"],
    "component": "apps/store-locator/storefront/StoreLocator.tsx"
  }
}

fields puts the app's own fields on the store's products, collections, customers or orders: "Available at" appears on every product's Custom fields card, the merchant or the app fills it, the storefront reads it as metafields["store-locator.available_at"], and it leaves with the app. scopes says what of the store the app may reach beyond its own data, as the admin's access areas at view or manage, each with the line the merchant reads. Leave both out and the app reaches its own records, settings and fields, and nothing else.

functions, events, schedules and egress are the app's server side, hosted by the platform: the code in functions/notify-shop.ts runs when an order is placed, may call the store's API within the scopes and may send data to hooks.slack.com and nowhere else. Write the file:

// apps/store-locator/functions/notify-shop.ts
export default async function (input, context) {
  const { order } = await cartbase.get(`/api/admin/orders/${input.data.id}`)
  const shops = await cartbase.get("/api/apps/records/location?status=active")
  const nearest = shops.records[0]
  if (!nearest) return { notified: false }
  await fetch("https://hooks.slack.com/services/" + context.settings.slack_path, {
    method: "POST",
    body: JSON.stringify({ text: `Order ${order.display_id} for ${nearest.display_name}` }),
  })
  console.log("told", nearest.display_name)
  return { notified: true, shop: nearest.handle }
}

cartbase.* is the store's API as the app, fetch reaches the declared hosts, console.* is the run's log, the return value its result. What the function sees and its ceilings are in app-runtime-api.md § Functions.

3. Validate

# doc-noexec
cartbase app validate apps/store-locator

A refused manifest prints one line per problem, the path first, in words that say what to change. Fix and run again.

4. Deploy

# doc-noexec
cartbase app deploy apps/store-locator

The CLI compiles each function's entry file into one module (written under .cartbase/functions/ beside the source) and sends it with the manifest; a function that does not compile, or does not export a default function, stops the deploy with the file and line. The app appears in the store's admin under Apps with its screens: a Locations list showing the columns the record declares, with Export and Import of the locations as a CSV file, a record page per location with the point on a map, a Settings screen with the two settings. The app's home shows its fields on your objects, its access (the scopes with their reasons), its functions with what runs them, and their latest runs. The first deploy prints the app's key once and writes it to the project's .env.local as CARTBASE_APP_STORE_LOCATOR_KEY. Deploy again after every manifest or function change; the version history is kept.

Try the function before an order arrives:

# doc-noexec
cartbase app run notify_shop --input '{"data":{"id":"order_01…"}}'
cartbase app runs

The first prints the run's log and result (or its error, with the ceiling or the refusal named); the second lists the latest runs, whatever started them.

The person deploying grants the scopes, and no more than they hold: a member whose own access lacks an area the app asks for is refused before anything is written. The owner holds everything.

5. Write the storefront half

The storefront reads the app's public records with the publishable key, through the package:

import { listAppRecords } from "@cartbase/storefront/api/apps"

const { metaobjects: locations } = await listAppRecords(client, "store-locator", "location", { limit: 100 })

Each record carries fields: [{ key, kind, value }]. Server code that needs a setting, a secret, or the app's field on a product calls the app's own API with the key from the environment, and the store's admin API within the scopes; see app-runtime-api.md. Ship the storefront with cartbase deploy from the project root, as always.

6. Publish

Every deploy keeps the app folder as the version's files, so publishing is one switch:

# doc-noexec
cartbase app publish apps/store-locator

The app is in the index, the list every store's Apps page shows, as author/key, where the author is your store's handle. Any store installs it, from the Apps page or the CLI, and its copy records where it came from:

# doc-noexec
cartbase app install their-store/store-locator
cartbase deploy

Every later deploy you make is offered to the stores that installed it as an update: cartbase app update store-locator shows them the diff (the manifest's changed paths, the files added, changed and removed), and --apply takes it. Nothing reaches an installed copy on its own.

A store that improves its copy offers the change back:

# doc-noexec
cartbase app propose apps/store-locator --note "Adds opening hours to the map pin"

The proposal lands on your app's home with its diff; accept it and it is the app's next version, the one every install is offered; refuse it and the store keeps its own copy, which it may publish under its own handle as a fork, filed under yours in the index.

cartbase app publish --private takes the app out of the index; the stores that installed it keep what they have. cartbase app share still mirrors the folder as a public repository under the Cartbase organisation, README and MIT licence added, and cartbase app install owner/name reads one.

7. A card on the store's pages

A viewport is a card the admin draws on the order, product, customer or collection page from the app's own records, joined to the object by one of their fields, with the app's functions as buttons. Give the location a reference_product field, declare the card, deploy:

{
  "records": [{ "type": "location", "name": "Location", "fields": [
    { "key": "city", "name": "City", "kind": "text", "required": true },
    { "key": "product", "name": "Product", "kind": "reference_product" }
  ]}],
  "viewports": [{
    "key": "shops", "page": "product", "title": "Shops that carry it",
    "records": { "type": "location", "by": "product", "columns": ["city"] },
    "actions": [{ "function": "check_stock", "label": "Check stock" }]
  }]
}

Every product page now carries "Shops that carry it": the locations whose product field holds that product, each a link to its record, and a Check stock button that runs the function with { page: "product", id } and shows what it answers. On an order or customer page the joining field is a text field holding the object's id, written by the app's own code. Nothing runs when the page opens; a button is a click, on the ledger like any run. The merchant places or removes the card like any other with Customize layout.

8. Tools

A function that declares its input is a tool: the platform checks what a caller sends against the declaration and lists it in the app's catalogue, beside a list and a get for every record type:

{ "key": "check_stock", "entry": "functions/check-stock.ts",
  "input": { "product_id": { "type": "string", "required": true } },
  "output": { "in_stock": { "type": "number" } } }

GET /api/apps/me answers tools, each with its name, its parameters and the door to call it on; the app's home shows them; cartbase app run check_stock --input '{"product_id":"prod_…"}' calls one and a missing or wrong parameter is refused by name before the function runs. An agent with the store's key reads the catalogue and calls the doors it names.

Rules

  • Records and settings are the platform's. Render what it answers; never keep a second copy.
  • A secret never reaches the browser. Read it in server code through the runtime API; the storefront's client bundle carries the publishable key and nothing else.
  • Ask for the access you use. Every scope is a line the merchant reads at install and a door the API opens; an app with no scopes is the easiest to install. The same for egress: every host is a line the merchant reads, and a function reaches no other.
  • A function does one thing and returns. It has 30 seconds, no state between runs but the app's records, and its log is the ledger: log what a merchant would need to see, never a secret.
  • The manifest is the app. A change is a new deploy. Removing a record type or a field that still holds data is refused; clear the data first.
  • Uninstall deletes. The merchant is shown what goes and offered the export; the click deletes the app and everything it held, at once.