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.
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"import os, requests
API = "https://api.starco.dev/v1"
H = {"Authorization": f"Bearer {os.environ['STARCO_KEY']}"}
twin = requests.post(f"{API}/twins", headers=H, json={
"template": "daily_movement", "layers": ["wellness"], "label": "state-07"}).json()["twin"]["id"]
print(twin)import { Client, VersionConflictError } from '@starco/sdk';
const c = new Client({ baseUrl: "https://api.starco.dev/v1", apiKey: process.env.STARCO_KEY });
const twin = (await c.twins.create({ template: "daily_movement", layers: ["wellness"], label: "state-07" })).id;
console.log(twin);starco twins create --template daily_movement --layers wellness --label state-07The 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")'for m in requests.get(f"{API}/twins/{twin}/measures", headers=H).json()["data"]:
if m["measure"] == "wellness.mood_level":
print(m)console.log((await c.state.measures(twin)).find((m) => m.measure === "wellness.mood_level"));starco measures <twin> # <twin> is the id the step before printed{ "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"r = requests.put(f"{API}/twins/{twin}/state/wellness.mood_level", headers=H, json={"value": 3})
print(r.status_code, r.json())
r = requests.get(f"{API}/twins/{twin}/state/wellness.mood_level", headers=H)
print(r.headers["ETag"], r.json())
version = r.json()["version"]console.log(await c.state.set(twin, "wellness.mood_level", 3));
const read = await c.state.get(twin, "wellness.mood_level");
console.log(read.etag, read.value, read.version);starco twin set <twin> wellness.mood_level 3
starco twin get <twin> wellness.mood_level200 { "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}'r = requests.put(f"{API}/twins/{twin}/state/wellness.mood_level", headers=H, json={"value": 4, "if_version": 0})
print(r.status_code, r.json())try {
await c.state.set(twin, "wellness.mood_level", 4, { ifVersion: 0 });
} catch (e) {
if (!(e instanceof VersionConflictError)) throw e;
console.log(e.status, e.code, "current version", e.currentVersion); // 409 version_conflict current version 1
}starco twin set <twin> wellness.mood_level 4 --if-version 0 || echo "refused, exit status $?"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.
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}'cur = requests.get(f"{API}/twins/{twin}/state/wellness.mood_level", headers=H)
r = requests.put(f"{API}/twins/{twin}/state/wellness.mood_level",
headers={**H, "If-Match": cur.headers["ETag"]}, json={"value": 4})
print(r.status_code, r.headers["ETag"], r.json())const cur = await c.state.get(twin, "wellness.mood_level");
console.log(await c.state.set(twin, "wellness.mood_level", 4, { ifMatch: cur.etag }));starco twin set <twin> wellness.mood_level 4 --if-match '"1"'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"h = requests.get(f"{API}/twins/{twin}/history/wellness.mood_level", headers=H).json()
for c in h["data"]:
print(c["version"], c["op"], c["old_value"], "→", c["new_value"], c["actor"], c["changed_at"])for await (const h of c.history.iter(twin, "wellness.mood_level")) {
console.log(h.version, h.op, h.old_value, "→", h.new_value, h.actor, h.changed_at);
}starco history <twin> wellness.mood_level1 set None → 3 starco_dk_fb7fa299 2026-11-20T22:10:13Z
2 set 3 → 4 starco_dk_fb7fa299 2026-11-20T22:10:13ZThe 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.