The app runtime API

The app's own door: /api/apps/*, opened by the app's key.

Authorization: Bearer cak_…

The key is printed once by cartbase app deploy (the first deploy) or by a rotation, and lives in the environment where the app's server code runs, CARTBASE_APP_<KEY>_KEY. Here it reaches this app's settings, secrets, record types and fields on this store, with no scope at all: the app's own data is its own. A member's token, a CLI token or a session is refused here.

The same key opens the store's admin API within the manifest's scopes; see the admin API with the app's key. A function the platform runs for the app calls both doors through cartbase.* with a token of its own; see Functions.

Every answer is JSON. Errors follow the platform's envelope: { error, code, details? }.

Status Code When
401 unauthenticated No key, or a revoked one
403 app_disabled The merchant switched the app off
403 app_scope The record type belongs to another app; at the admin API, a call outside the app's scopes
404 not_found A record type or field the manifest does not declare, a record that is not this type's, an object that is not this store's
400 invalid_data A record that does not match its fields; details.errors: [{key, code, message}]
429 rate_limited More than 600 calls in a minute on one key, at either door

GET /api/apps/me

Who am I, on which store, with which settings.

{
  "app": { "id": "app_…", "key": "store-locator", "name": "Store locator", "version": "1.0.0", "status": "live" },
  "store": { "client_id": "…" },
  "settings": { "maps_key": "AIza…", "default_zoom": 7 },   // secrets DECRYPTED, here only
  "records": [
    { "type": "location", "store_type": "store-locator.location", "name": "Location", "visibility": "public", "fields": [ … ] }
  ],
  "fields": [
    { "id": "…", "object": "product", "key": "store-locator.available_at", "field_key": "available_at", "name": "Available at", "kind": "metaobject_reference", "list": true, "visibility": "public", "record": "location", … }
  ],
  "scopes": [
    { "area": "products", "label": "Products", "access": "view", "reason": "Shows which shops carry each product", "covers": "Products, variants, collections, …" }
  ],
  "functions": [ { "key": "notify", "name": "Notify", "entry": "functions/notify.ts", "events": ["order.placed"], "schedules": [], "input": null, "output": null } ],
  "schedules": [ { "key": "nightly", "cron": "0 3 * * *", "function": "sync", "timezone": "Europe/Sofia" } ],
  "egress": ["api.courier.example"],
  "tools": [
    { "name": "store-locator.check_stock", "kind": "function", "title": "Check stock", "description": null,
      "input": { "product_id": { "type": "string", "required": true } }, "output": {},
      "call": { "method": "POST", "path": "/api/admin/apps/store-locator/functions/check_stock/run", "body": "{ \"input\": { ...the parameters } }; the answer's run.output is the result" } },
    { "name": "store-locator.location.list", "kind": "records", "title": "List locations", "description": "The app's locations, newest first",
      "input": { "q": { "type": "string", "description": "Searches the records' names and handles" }, "limit": { "type": "number", "description": "Records per page, 50 at most" }, "offset": { "type": "number", "description": "Records to skip" } },
      "output": { "records": { "type": "array", "description": "The records, each with id, handle, display_name, status and fields" }, "count": { "type": "number", "description": "How many match in all" } },
      "call": { "method": "GET", "path": "/api/apps/records/location" } },
    { "name": "store-locator.location.get", "kind": "records", "title": "Get a location", "description": "One of the app's locations by id",
      "input": { "id": { "type": "string", "required": true, "description": "The record's id" } }, "output": { "record": { "type": "object", "description": "The record: id, handle, display_name, status and fields" } },
      "call": { "method": "GET", "path": "/api/apps/records/location/{id}" } }
  ],
  "viewports": [ { "key": "shops", "page": "product", "title": "Shops that carry it", "records": { "type": "location", "by": "product", "columns": ["city"] }, "actions": [] } ]
}

store_type is what the storefront asks the store API for. tools is the catalogue derived from the manifest (app-manifest.md § Tools): every function with declared input, and list and get for every record type, each naming the door it is called on. viewports are the cards the app puts on the store's own pages, as declared.

GET /api/apps/records/:type

The app's records of one type. Query q, status (draft | active), limit (default 50, up to 200), offset.

{ "records": [ { "id": "mobj_…", "handle": "ay-gross-mega", "display_name": "AY GROSS MEGA", "fields": { "city": "Пловдив", … }, "status": "active", … } ], "count": 40, "offset": 0, "limit": 50 }

POST /api/apps/records/:type

Create a record. Body { handle?, display_name?, fields?, status? }. The handle is derived from the display name when absent. required fields are enforced when status is active. Answers 201 { record }.

GET /api/apps/records/:type/:id

One record with its fields resolved: { record, resolved_fields }.

POST /api/apps/records/:type/:id

Partial update, same body as create. The merged field set is validated, so a patch cannot dodge a required field on activation. Answers { record }.

DELETE /api/apps/records/:type/:id

Answers { id, object: "record", deleted: true }.

GET /api/apps/fields

The app's fields on the store's own objects, as deployed: { fields, count }, each { id, object, key, field_key, name, description, kind, list, pinned, visibility, options, record }. key is the stored key, <app key>.<field key>, the name the store API serves the value under.

GET /api/apps/fields/:object/:key

The values of one field across the store's objects, newest first: { values: [{ entity_id, value, updated_at }], count, offset, limit }. Query entity_ids (comma-separated) narrows to those objects; limit (default 50, up to 200), offset. :object is product, collection, customer or order; :key is the field key as the manifest declares it.

GET /api/apps/fields/:object/:key/:entity_id

The value on one object: { value: { entity_id, value, updated_at } }, or 404 when none is set.

POST /api/apps/fields/:object/:key/:entity_id

Write the value: body { value }. 201 when born, 200 when changed, { value } either way. The object must be this store's (404 otherwise); a metaobject_reference value must be an id (or ids, for a list) of the record type the field names.

DELETE /api/apps/fields/:object/:key/:entity_id

Clears the value: { entity_id, object: "field_value", deleted: true }.

The admin API with the app's key

The manifest's scopes open the store's admin API, /api/admin/*, to the same bearer: view Products answers GET /api/admin/products and every other read in the area, manage Orders accepts the order writes. The routes, their bodies and their answers are the admin API's own, the ones the CLI's cartbase <resource> commands wrap; nothing is duplicated here.

The app is its own principal at that door: no person, no membership, its scopes its policies, checked on every call by the code that checks a member's roles. A call outside the scopes answers 403 app_scope naming the resource, the area and the level to declare, and a route the admin's policy matrix does not name is closed to apps. An app with no scopes reaches nothing at the admin API.

Functions: the code the platform runs

The manifest's functions are the app's server side, hosted by the platform (app-manifest.md § A function). A function is a file exporting one default function; cartbase app deploy compiles it and the platform runs it on the declared events, on the declared schedules, or now, on request. Every run is one row on the store's execution ledger.

// functions/notify.ts
export default async function (input, context) {
  const order = await cartbase.get(`/api/admin/orders/${input.data.id}`)   // within the scopes
  const res = await fetch("https://api.courier.example/shipments", {         // a host in egress
    method: "POST",
    headers: { authorization: `Bearer ${context.settings.courier_key}` },    // a secret, decrypted here only
    body: JSON.stringify({ order: order.order.display_id }),
  })
  console.log("booked", res.status)                                          // the run's log
  return { booked: res.ok }                                                  // the run's result
}

The arguments. input is the event's body { id, name, created_at, data } (the same body a webhook is posted), the schedule's tick { schedule, at }, or whatever cartbase app run --input or the admin's Run sent. context is { trigger: "event" | "schedule" | "manual", event?, schedule?, app: { key, name, version }, function, run_id, settings }; settings carries every declared setting with its value, a secret decrypted, and nothing of it is written to the ledger.

The four doors, and nothing else. The function runs in its own sandbox (QuickJS in WebAssembly): no file system, no process, no clock but the platform's, no network but these.

Door What it does
cartbase.get(path), .post(path, body), .patch, .put, .delete(path), .request(method, path, body) Calls this store's API as the app: /api/admin/* within the scopes, /api/apps/* for the app's own records, settings and fields. Answers the parsed JSON; a refusal throws a CartbaseError with status, code, message, details. The run calls with a token of its own, minted under the app's key, alive for the run; a rotation kills it
fetch(url, init) Reaches the hosts in the manifest's egress, https only, redirects not followed. Any other host is refused by name before a connection opens. The answer has status, ok, headers.get(), text(), json()
console.log, .info, .warn, .error The run's log on the ledger, with every secret setting's value blanked as [secret]
the return value The run's result, JSON

Ceilings, per run: 30 seconds wall clock, 64 MB, 50 fetches and 100 platform calls of 10 seconds each, 100 log lines of 1,000 characters, a 64 KB result. Per app: 5,000 runs a day, whatever starts them, and 600 API calls a minute on its key. A run past a ceiling fails with the ceiling named; a run past the day's budget waits for the next day.

Events. A function on an event is a subscriber of the store's bus with the webhooks worker's rules: drained every minute, a failed run retried on the ladder (5 minutes, 30, 2 hours, 12, 24) and then dead, twenty dead in a row switching the function's events off until the next deploy, nothing from before the deploy replayed. The app's home shows the row's health.

Schedules. A cron line in its timezone; the platform's clock runs what is due once. Twenty failures in a row switch a schedule off until the next deploy.

Runs. GET /api/admin/apps/<key>/runs?limit=&function= lists the latest { id, function, trigger, source, state, duration_ms, error, created_at }; GET /api/admin/apps/<key>/runs/<id> adds input, output and logs; POST /api/admin/apps/<key>/functions/<function>/run with { input } runs now and answers { run: { state, output, error, logs, duration_ms, transaction_id } }. The CLI: cartbase app run <function> --input '{…}', cartbase app runs.

Tools. A function that declares input in the manifest is a tool: the run door checks the body's input against the declaration before the sandbox starts, and a missing required parameter, an undeclared one, a wrong type or a value outside the enum answers 400 invalid_input naming the parameter. The catalogue, tools on GET /api/apps/me, lists every tool with its call.

Viewports. A function named as a viewport's action runs on the object when the merchant clicks the button: its input is { page, id, viewport } (the page the card sits on, the object's id, the viewport's key), and the card shows what it answers, a string as is, an object's message, or the answer as JSON. The same call is POST .../functions/<function>/run with { input: { page, id }, viewport: "<key>" }. GET /api/admin/apps/viewports?page=<page>&id=<id> answers every app's cards for an object.

The storefront's reads

Public record types answer to the publishable key at the store API, as any metaobject does:

GET /api/store/metaobjects/<key>.<type>
GET /api/store/metaobjects/<key>.<type>/<handle>

Active records only; a private type is 404. The package wraps both: listAppRecords and getAppRecord in @cartbase/storefront/api/apps.

A public field on a product, collection or customer is served on that object as metafields["<key>.<field>"], beside the merchant's own fields; a private one never leaves the admin and the app.