Beta. STARCO Dev works with synthetic data only: do not send real personal or health data. Every feature shows its real status (Available Beta Beta · in development Coming).

Documentation menu

Tutorial 08

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.

time 15 minlevel intermediateyou need curl and jq, or Python 3 with requestsuses layer schemas Beta · derived measures Beta · state Betatabs curl · Python · TypeScript · CLI (the SDK and the command line are not published as packages yet)
  1. 1Schema
  2. 2Twin
  3. 3Inputs
  4. 4Derived
  5. 5No input

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"
Expected outputillustrative · synthetic
c0bb4ccb-2725-4f9e-b948-98fa2f28d9c3 1 3

An 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)
Expected outputillustrative · synthetic
[{"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}}'
Expected outputillustrative · synthetic
200 { "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"
Expected outputillustrative · synthetic
{ "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}'
Expected outputillustrative · synthetic
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"
Expected outputillustrative · synthetic
{ "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.

water_ml · 1750 ml weight_kg · 70 kg round(water / weight, 1) 25 ml/kg

Next steps#