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 07

Current state and safe updates.

Goal: keep the present of a twin (one current value per measure) without two apps overwriting each other: write with a version, see a stale write refused, and read who changed what.

time 15 minlevel intermediateyou need curl, or Python 3 with requestsuses state Beta · history Betatabs curl · Python · TypeScript · CLI (the SDK and the command line are not published as packages yet)
  1. 1Setup
  2. 2Measures
  3. 3Set
  4. 4Conflict
  5. 5Retry
  6. 6History

STEP 1 A key and a twin#

Use a project key with read and write (see the quickstart) and create a twin with the wellness layer.

export API="https://api.starco.dev/v1"
export STARCO_KEY="starco_dk_…"
TWIN=$(curl -s -X POST "$API/twins" -H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" \
  -d '{"template": "daily_movement", "layers": ["wellness"], "label": "state-07"}' | jq -r .twin.id)
echo "$TWIN"

The curl lines use jq to keep the id in a variable.

STEP 2 The twin's typed measures Beta#

Every measure is named layer.measure and has a type, a unit and the bounds a value must fit to be recorded (not a reference range).

curl -s "$API/twins/$TWIN/measures" -H "Authorization: Bearer $STARCO_KEY" | jq '.data[] | select(.measure == "wellness.mood_level")'
Expected outputillustrative · synthetic
{ "measure": "wellness.mood_level", "layer": "wellness", "name": "mood_level", "type": "integer",
  "unit": "rating_1_5", "accepted_min": 1, "accepted_max": 5, "enum": null, "description": null,
  "builtin": true, "has_series": true, "derived": false, "expression": null, "inputs": [] }

STEP 3 Set a value and read it back Beta#

curl -X PUT "$API/twins/$TWIN/state/wellness.mood_level" -H "Authorization: Bearer $STARCO_KEY" \
  -H "Content-Type: application/json" -d '{"value": 3}'
curl -i "$API/twins/$TWIN/state/wellness.mood_level" -H "Authorization: Bearer $STARCO_KEY"
Expected outputillustrative · synthetic
200 { "op": "set", "measure": "wellness.mood_level", "value": 3, "version": 1,
      "updated_at": "2026-11-20T22:10:13Z", "transaction_id": "5c26527e-…" }
ETag: "1"
{ "measure": "wellness.mood_level", "has_value": true, "value": 3, "version": 1,
  "updated_at": "2026-11-20T22:10:13Z", "updated_by": "starco_dk_fb7fa299" }

A value outside the bounds is refused with 422 value_out_of_accepted_bounds and "expected": [1, 5], and nothing changes. A measure that never had a value answers "has_value": false with "value": null: never a default.

STEP 4 A stale write is refused Beta#

Send the version you read as if_version (or the ETag as If-Match). If someone changed the measure in between, the write is refused and nothing changes. Here we pretend we read version 0.

curl -X PUT "$API/twins/$TWIN/state/wellness.mood_level" -H "Authorization: Bearer $STARCO_KEY" \
  -H "Content-Type: application/json" -d '{"value": 4, "if_version": 0}'
Expected outputillustrative · synthetic
409 { "success": false, "code": "version_conflict",
      "error": "The measure is not at the expected version: nothing was changed.",
      "op_index": 0, "measure": "wellness.mood_level",
      "expected_version": 0, "current_version": 1, "has_value": true }

The same rule with several writers at once. Every measure keeps its own version, and a write that arrives second is the one refused:

Four strands, one per measure of the wellness layer (mood_level, sleep_quality, sitting_breaks, hydration_glasses), run from the left and braid into a single line: the twin's current state. A dot travelling along a strand is a write, PUT /v1/twins/{twin}/state/{measure}, that carries the version it read as if_version. When it reaches the state, the version of that measure goes up by one and a mark with the new number appears. If another write to the same measure arrived first, the dot is refused with 409 version_conflict: nothing changes, and the answer carries current_version. The counters are computed in this page from these simulated writes.

One version per measure. Each dot is a PUT /v1/twins/{twin}/state/{measure} that carries the version it read as if_version. Accepted: that measure's version goes up by one. Arrived second: 409 version_conflict, and nothing changes. Simulated writes, counted in this page.

Simulated writes · counted in this page

STEP 5 Read again, then write on top Beta#

The fix is always the same: read the current value and version, decide, and write with that version. With If-Match only a strong ETag counts ("1", not W/"1"); if you send both, they must agree.

curl -X PUT "$API/twins/$TWIN/state/wellness.mood_level" -H "Authorization: Bearer $STARCO_KEY" \
  -H "Content-Type: application/json" -H 'If-Match: "1"' -d '{"value": 4}'
Expected outputillustrative · synthetic
200 "2" { "op": "set", "measure": "wellness.mood_level", "value": 4, "version": 2,
          "updated_at": "2026-11-20T22:10:13Z", "transaction_id": "e9233d98-…" }

Several measures at once? PATCH /v1/twins/{twin}/state with values and an optional watch is all or nothing, and POST /v1/twins/{twin}/tx runs a list of operations under one watch. Both are in the API explorer and in chapter 8 of the Academy.

STEP 6 Who changed it, and when Beta#

curl "$API/twins/$TWIN/history/wellness.mood_level" -H "Authorization: Bearer $STARCO_KEY"
Expected outputillustrative · synthetic
1 set None → 3 starco_dk_fb7fa299 2026-11-20T22:10:13Z
2 set 3 → 4 starco_dk_fb7fa299 2026-11-20T22:10:13Z

The refused write of step 4 is not in the history (nothing changed), but it is a row of the project's audit chain with "refused": "version_conflict". The history keeps the last 1000 changes of each measure; DELETE removes the value, not the past.

What just happened

The twin's present is a value per measure with a version. Every write raised the version, sent the new one back (body and ETag) and wrote a state.changed event. A write that named an old version changed nothing and told you the current one, so two apps never silently overwrite each other.

app A · reads v1 app B · reads v1 PUT if_version 1 → v2 PUT if_version 1 → 409 read v2 → write v3

Next steps#