Your own layer and derived measures.
Goal: describe a small study as a layer schema, with one measure calculated from two others, and see the calculation follow its inputs, including when one of them goes away.
STEP 1 Define the layer Beta#
A layer schema has a name of your own (not a built-in one such as wellness) and typed measures: number, integer, boolean, string, enum or json. A numeric measure needs a unit (letters, digits, % / . _ -) and the bounds a value must fit to be recorded. A measure with an expression is derived: numbers, other measures of the same layer by their bare name, + - * /, parentheses and the functions min, max, abs and round (up to 200 characters), parsed by a closed grammar and never evaluated as code. A division by zero or a missing input gives no value.
export API="https://api.starco.dev/v1"
export STARCO_KEY="starco_dk_…"
SCHEMA=$(curl -s -X POST "$API/schemas" -H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" -d '{
"layer": "hydration", "description": "Synthetic hydration study",
"measures": [
{"name": "water_ml", "type": "integer", "unit": "ml", "min": 0, "max": 10000},
{"name": "weight_kg", "type": "number", "unit": "kg", "min": 1, "max": 400},
{"name": "ml_per_kg", "type": "number", "unit": "ml/kg", "expression": "round(water_ml / weight_kg, 1)"}
]}' | jq -r .schema.id)
echo "$SCHEMA"import os, requests
API = "https://api.starco.dev/v1"
H = {"Authorization": f"Bearer {os.environ['STARCO_KEY']}"}
schema = requests.post(f"{API}/schemas", headers=H, json={
"layer": "hydration", "description": "Synthetic hydration study",
"measures": [
{"name": "water_ml", "type": "integer", "unit": "ml", "min": 0, "max": 10000},
{"name": "weight_kg", "type": "number", "unit": "kg", "min": 1, "max": 400},
{"name": "ml_per_kg", "type": "number", "unit": "ml/kg", "expression": "round(water_ml / weight_kg, 1)"},
]}).json()["schema"]
print(schema["id"], schema["version"], schema["measure_count"])import { Client, ValidationError } from '@starco/sdk';
const c = new Client({ baseUrl: "https://api.starco.dev/v1", apiKey: process.env.STARCO_KEY });
const schema = await c.schemas.create("hydration", [
{ name: "water_ml", type: "integer", unit: "ml", min: 0, max: 10000 },
{ name: "weight_kg", type: "number", unit: "kg", min: 1, max: 400 },
{ name: "ml_per_kg", type: "number", unit: "ml/kg", expression: "round(water_ml / weight_kg, 1)" },
], "Synthetic hydration study");
console.log(schema.id, schema.version, schema.measure_count);cat > hydration.json <<'JSON'
{"layer": "hydration", "description": "Synthetic hydration study", "measures": [
{"name": "water_ml", "type": "integer", "unit": "ml", "min": 0, "max": 10000},
{"name": "weight_kg", "type": "number", "unit": "kg", "min": 1, "max": 400},
{"name": "ml_per_kg", "type": "number", "unit": "ml/kg", "expression": "round(water_ml / weight_kg, 1)"}]}
JSON
SCHEMA=$(starco --json schemas create hydration.json | jq -r .id)
echo "$SCHEMA"c0bb4ccb-2725-4f9e-b948-98fa2f28d9c3 1 3An expression that names a measure the layer does not have is refused before anything is stored: 422 validation_failed with "reason": "expression_unknown_input" and the input it could not find.
STEP 2 A twin with your layer Beta#
TWIN=$(curl -s -X POST "$API/twins" -H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" \
-d "{\"template\": \"daily_movement\", \"layers\": [\"wellness\"], \"schemas\": [\"$SCHEMA\"], \"label\": \"derived-08\"}" | jq -r .twin.id)twin = requests.post(f"{API}/twins", headers=H, json={
"template": "daily_movement", "layers": ["wellness"], "schemas": [schema["id"]], "label": "derived-08"}).json()["twin"]
print(twin["custom_layers"])
twin = twin["id"]const created = await c.twins.create({ template: "daily_movement", layers: ["wellness"], schemas: [schema.id], label: "derived-08" });
console.log(created.custom_layers);
const twin = created.id;starco twins create --template daily_movement --layers wellness --schema $SCHEMA --label derived-08[{"layer": "hydration", "schema_id": "c0bb4ccb-2725-4f9e-b948-98fa2f28d9c3", "version": 1}]STEP 3 Set the two inputs at once Beta#
PATCH …/state applies every value or none. The answer lists, under the input that triggered it, the derived value recomputed in the same transaction.
curl -X PATCH "$API/twins/$TWIN/state" -H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" \
-d '{"values": {"hydration.water_ml": 1750, "hydration.weight_kg": 70}}'r = requests.patch(f"{API}/twins/{twin}/state", headers=H, json={
"values": {"hydration.water_ml": 1750, "hydration.weight_kg": 70}})
print(r.status_code, r.json())console.log(await c.state.mset(twin, { "hydration.water_ml": 1750, "hydration.weight_kg": 70 }));starco twin mset <twin> hydration.water_ml=1750 hydration.weight_kg=70 # <twin> is the id the step before printed200 { "twin_id": "c2662df7-…", "committed": true, "transaction_id": "5594a1e3-…",
"results": [
{ "op": "set", "measure": "hydration.water_ml", "value": 1750, "version": 1, "updated_at": "…" },
{ "op": "set", "measure": "hydration.weight_kg", "value": 70, "version": 1, "updated_at": "…",
"derived": [ { "measure": "hydration.ml_per_kg", "value": 25, "version": 1 } ] } ] }STEP 4 Read the derived value, and try to set it Beta#
curl -s "$API/twins/$TWIN/state/hydration.ml_per_kg" -H "Authorization: Bearer $STARCO_KEY"print(requests.get(f"{API}/twins/{twin}/state/hydration.ml_per_kg", headers=H).json())console.log(await c.state.get(twin, "hydration.ml_per_kg"));starco twin get <twin> hydration.ml_per_kg{ "measure": "hydration.ml_per_kg", "has_value": true, "value": 25, "version": 1, … }A derived measure is computed, never written. Setting it is refused, and the answer names the measures to change instead:
curl -s -X PUT "$API/twins/$TWIN/state/hydration.ml_per_kg" -H "Authorization: Bearer $STARCO_KEY" \
-H "Content-Type: application/json" -d '{"value": 30}'r = requests.put(f"{API}/twins/{twin}/state/hydration.ml_per_kg", headers=H, json={"value": 30})
print(r.status_code, r.json())try {
await c.state.set(twin, "hydration.ml_per_kg", 30);
} catch (e) {
if (!(e instanceof ValidationError)) throw e;
console.log(e.status, e.code, e.details); // 422 measure_is_derived, with the inputs to change instead
}starco twin set <twin> hydration.ml_per_kg 30 || echo "refused, exit status $?"422 { "success": false, "code": "measure_is_derived",
"error": "This measure is calculated from others of its layer: change its inputs.",
"op_index": 0, "measure": "hydration.ml_per_kg",
"inputs": ["hydration.water_ml", "hydration.weight_kg"] }STEP 5 Take an input away Beta#
curl -X DELETE "$API/twins/$TWIN/state/hydration.weight_kg" -H "Authorization: Bearer $STARCO_KEY"
curl "$API/twins/$TWIN/state/hydration.ml_per_kg" -H "Authorization: Bearer $STARCO_KEY"print(requests.delete(f"{API}/twins/{twin}/state/hydration.weight_kg", headers=H).json())
print(requests.get(f"{API}/twins/{twin}/state/hydration.ml_per_kg", headers=H).json())console.log(await c.state.delete(twin, "hydration.weight_kg"));
console.log(await c.state.get(twin, "hydration.ml_per_kg"));starco twin del <twin> hydration.weight_kg
starco twin get <twin> hydration.ml_per_kg{ "op": "del", "measure": "hydration.weight_kg", "deleted": true, "version": 2,
"derived": [ { "measure": "hydration.ml_per_kg", "value": null, "version": 2 } ], "transaction_id": "8099ebfd-…" }
{ "measure": "hydration.ml_per_kg", "value": null, "has_value": false, "version": 2, … }Without the weight there is no ratio: the derived measure has no value. Not the old value, not a zero.
What just happened
Your study became a schema, with version 1. Setting an input recomputed the derived measure in the same transaction, so they can never disagree; the derived measure cannot be set by hand; and a missing input means no value. Each change has its version, its history and its state.changed event. Posting the same layer again creates version 2 of the schema; a twin keeps the version it was created with.