Measures and validation.
Goal: record how many glasses of water a synthetic person drank with the catalogue's hydration_glasses kind, send mistakes on purpose to see validation at work, and read the result in the layer.
STEP 1 Find the kind and its rules Beta#
Every kind carries the contract each observation is checked against: its layer, one unit, the accepted bounds and whether it must be a whole number.
curl -s "$API/templates" -H "Authorization: Bearer $STARCO_KEY" | jq '.kinds[] | select(.kind == "hydration_glasses")'cat = requests.get(f"{API}/templates", headers=H).json()
print(next(k for k in cat["kinds"] if k["kind"] == "hydration_glasses"))import { Client } from '@starco/sdk';
const c = new Client({ baseUrl: "https://api.starco.dev/v1", apiKey: process.env.STARCO_KEY });
const twinId = process.env.TWIN; // the id of the twin: export TWIN=… as in tutorial 01
const cat = await c.templates.list();
console.log(cat.kinds.find((k) => k.kind === "hydration_glasses"));starco --json templates | jq '.kinds[] | select(.kind == "hydration_glasses")'{ "kind": "hydration_glasses", "layer": "wellness", "unit": "glasses",
"accepted_min": 0, "accepted_max": 20, "integer": true }| Rule | Value | Why |
|---|---|---|
layer | wellness | The twin must have this layer, or the line is refused with layer_not_enabled_on_twin. |
unit | glasses | One unit per kind, written exactly so. A line in another unit is rejected, never converted. |
integer | true | Whole glasses only. |
| bounds | 0 to 20 | Catches typing errors. The bounds check that a value is recordable; they say nothing about the person. |
STEP 2 Send valid observations Beta#
One observation per day: the glasses drunk that day, recorded in the evening.
curl -X POST "$API/twins/$TWIN/observations" -H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" -d '{
"observations": [
{"kind": "hydration_glasses", "value": 6, "unit": "glasses", "observed_at": "2026-11-16T21:30:00Z"},
{"kind": "hydration_glasses", "value": 8, "unit": "glasses", "observed_at": "2026-11-17T21:30:00Z"},
{"kind": "hydration_glasses", "value": 7, "unit": "glasses", "observed_at": "2026-11-18T21:30:00Z"} ] }'days = [("2026-11-16", 6), ("2026-11-17", 8), ("2026-11-18", 7)]
r = requests.post(f"{API}/twins/{twin_id}/observations", headers=H, json={"observations": [
{"kind": "hydration_glasses", "value": n, "unit": "glasses", "observed_at": f"{d}T21:30:00Z"} for d, n in days]})
print(r.status_code, r.json())const days = [["2026-11-16", 6], ["2026-11-17", 8], ["2026-11-18", 7]];
console.log(await c.observations.send(twinId, days.map(([d, n]) => (
{ kind: "hydration_glasses", value: n, unit: "glasses", observed_at: `${d}T21:30:00Z` }))));starco obs add <twin> hydration_glasses 6 glasses --at 2026-11-16T21:30:00Z
starco obs add <twin> hydration_glasses 8 glasses --at 2026-11-17T21:30:00Z
starco obs add <twin> hydration_glasses 7 glasses --at 2026-11-18T21:30:00Z200 { "batch_id": "7e0b2a91-…", "accepted": 3, "rejected": [], "recomputed_layers": ["wellness"] }STEP 3 Watch validation work Beta#
Now send five mistakes on purpose: millilitres instead of glasses, half a glass, a value far above the bound, a kind that does not exist and an extra field.
curl -X POST "$API/twins/$TWIN/observations" -H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" -d '{
"observations": [
{"kind": "hydration_glasses", "value": 250, "unit": "mL", "observed_at": "2026-11-15T21:30:00Z"},
{"kind": "hydration_glasses", "value": 1.5, "unit": "glasses", "observed_at": "2026-11-14T21:30:00Z"},
{"kind": "hydration_glasses", "value": 25, "unit": "glasses", "observed_at": "2026-11-13T21:30:00Z"},
{"kind": "water_ml", "value": 250, "unit": "mL", "observed_at": "2026-11-13T21:30:00Z"},
{"kind": "hydration_glasses", "value": 5, "unit": "glasses", "observed_at": "2026-11-12T21:30:00Z", "note": "after the gym"} ] }'bad = [
{"kind": "hydration_glasses", "value": 250, "unit": "mL", "observed_at": "2026-11-15T21:30:00Z"},
{"kind": "hydration_glasses", "value": 1.5, "unit": "glasses", "observed_at": "2026-11-14T21:30:00Z"},
{"kind": "hydration_glasses", "value": 25, "unit": "glasses", "observed_at": "2026-11-13T21:30:00Z"},
{"kind": "water_ml", "value": 250, "unit": "mL", "observed_at": "2026-11-13T21:30:00Z"},
{"kind": "hydration_glasses", "value": 5, "unit": "glasses", "observed_at": "2026-11-12T21:30:00Z", "note": "after the gym"}]
r = requests.post(f"{API}/twins/{twin_id}/observations", headers=H, json={"observations": bad})
print(r.status_code)
for rej in r.json()["rejected"]:
print(rej["index"], rej["reason"], rej.get("field"), rej.get("expected"))const bad = [
{ kind: "hydration_glasses", value: 250, unit: "mL", observed_at: "2026-11-15T21:30:00Z" },
{ kind: "hydration_glasses", value: 1.5, unit: "glasses", observed_at: "2026-11-14T21:30:00Z" },
{ kind: "hydration_glasses", value: 25, unit: "glasses", observed_at: "2026-11-13T21:30:00Z" },
{ kind: "water_ml", value: 250, unit: "mL", observed_at: "2026-11-13T21:30:00Z" },
{ kind: "hydration_glasses", value: 5, unit: "glasses", observed_at: "2026-11-12T21:30:00Z", note: "after the gym" }];
const refused = await c.observations.send(twinId, bad); // 422 with the result: the SDK returns it, it does not throw
for (const rej of refused.rejected) console.log(rej.index, rej.reason, rej.field, rej.expected);starco obs add <twin> hydration_glasses 250 mL --at 2026-11-15T21:30:00Z || echo "refused, exit status $?"422
0 unit_mismatch unit glasses
1 value_not_an_integer value None
2 value_out_of_accepted_bounds value [0, 20]
3 unknown_kind kind None
4 unknown_field note NoneThe answer is 422 because no line was accepted; its body has the same shape as a 200, with "accepted": 0 and "recomputed_layers": []. No rejected line is stored, partly or otherwise, and the layer is not recomputed. A field named like an identifier, such as email, would be refused as identifier_field_not_accepted. The full list of reasons is in Concepts.
STEP 4 Read it in the layer Beta#
curl -s "$API/twins/$TWIN/layers/wellness" -H "Authorization: Bearer $STARCO_KEY" \
| jq '.kinds[] | select(.kind == "hydration_glasses")'layer = requests.get(f"{API}/twins/{twin_id}/layers/wellness", headers=H).json()
print(next(k for k in layer["kinds"] if k["kind"] == "hydration_glasses"))const layer = await c.twins.layer(twinId, "wellness");
console.log(layer.kinds.find((k) => k.kind === "hydration_glasses"));starco --json layer <twin> wellness | jq '.kinds[] | select(.kind == "hydration_glasses")'{ "kind": "hydration_glasses", "unit": "glasses", "count": 3,
"latest": { "value": 7, "observed_at": "2026-11-18T21:30:00Z" },
"min": 6, "max": 8, "mean": 7, "mean_last_7_days": 7,
"first_observed_at": "2026-11-16T21:30:00Z", "last_observed_at": "2026-11-18T21:30:00Z" }STEP 5 Your own measures Beta#
A layer schema defines a custom layer with the measures the catalogue does not have, with the same rules you met in step 1: a name, a type, one unit and accepted bounds. Its numeric measures take observations and are validated exactly like the catalogue's kinds, with the same reason codes; a derived measure is calculated from others of its layer.
SCHEMA=$(curl -s -X POST "$API/schemas" -H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" -d '{
"layer": "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)
TWIN2=$(curl -s -X POST "$API/twins" -H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" \
-d '{"template": "food_hydration", "layers": ["wellness"], "schemas": ["'$SCHEMA'"]}' | jq -r .twin.id)
curl -s -X POST "$API/twins/$TWIN2/observations" -H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" \
-d '{"observations": [{"kind": "study.water_ml", "value": 1750, "unit": "ml", "observed_at": "2026-11-18T20:00:00Z"}]}'const schema = await c.schemas.create("study", [
{ 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)" }]);
const twin2 = await c.twins.create({ template: "food_hydration", layers: ["wellness"], schemas: [schema.id] });
console.log(await c.observations.send(twin2.id, [
{ kind: "study.water_ml", value: 1750, unit: "ml", observed_at: "2026-11-18T20:00:00Z" }]));A custom measure's observation kind is its full name (study.water_ml). A value with no kind on the twin is still refused with unknown_kind, never stored under another name.
What just happened
You sent three days of a catalogue kind, and the validator used its unit, integer rule and bounds to accept them and to reject five mistakes, each with a stable reason. The layer shows the numbers: 3 observations, from 6 to 8 glasses, a mean of 7. STARCO shows the numbers; it does not say whether they are enough.