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

Example 02

A derived measure, always up to date.

Define a layer of your own with a measure computed from two others. Write the inputs; read the result. The server recomputes it in the same transaction, and refuses anyone who tries to write it.

time 4 minuses layer schemas · state · derived measures Betadata synthetic
Beta.The API this program calls is in beta and works with synthetic data only. The program is complete: with a project key in STARCO_KEY it runs as it is. The TypeScript and CLI tabs use the SDK and the command line, which are not published yet.

The idea#

A layer schema lists typed measures. One with an expression is derived: a formula over other measures of the same layer, in a small closed grammar (arithmetic and a few functions such as round). If an input has no value, the derived measure has none either.

The whole program#

export API="https://api.starco.dev/v1"
AUTH="Authorization: Bearer $STARCO_KEY"
JSON="Content-Type: application/json"
SCHEMA=$(curl -s -X POST "$API/schemas" -H "$AUTH" -H "$JSON" -d '{"layer": "hydration", "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)
TWIN=$(curl -s -X POST "$API/twins" -H "$AUTH" -H "$JSON" \
  -d '{"template": "food_hydration", "layers": ["wellness"], "schemas": ["'$SCHEMA'"]}' | jq -r .twin.id)
curl -s -X PATCH "$API/twins/$TWIN/state" -H "$AUTH" -H "$JSON" \
  -d '{"values": {"hydration.water_ml": 1750, "hydration.weight_kg": 70}}' > /dev/null
curl -s "$API/twins/$TWIN/state/hydration.ml_per_kg" -H "$AUTH" | jq -c '{measure, value, version}'
curl -s -X PUT "$API/twins/$TWIN/state/hydration.ml_per_kg" -H "$AUTH" -H "$JSON" -d '{"value": 30}' | jq -c '{code, inputs}'
Output of the curl tabillustrative · synthetic
{"measure":"hydration.ml_per_kg","value":25,"version":1}
{"code":"measure_is_derived","inputs":["hydration.water_ml","hydration.weight_kg"]}

The other tabs print the same facts in their own format. Ids, hashes and counts depend on your project.

What just happened

1750 ml over 70 kg is 25 ml/kg, computed by the server when the inputs were written. Writing the derived measure answered 422 measure_is_derived and named the inputs to change instead.

Go further#