Import a week and handle rejections.
Goal: send seven days of synthetic sleep and step observations in one batch, find the line that was rejected, fix it and resend the whole file without ever storing anything twice.
STEP 1 Prepare the week#
One observation per line (JSON Lines). The values are synthetic and written by hand. The night of the 14th was not recorded, so it has no line: that is the truth, not an error. Line 8 has a mistake on purpose: its observed_at is missing.
{"kind": "sleep_hours", "value": 7.2, "unit": "h", "observed_at": "2026-11-12T07:00:00Z"}
{"kind": "sleep_hours", "value": 6.8, "unit": "h", "observed_at": "2026-11-13T07:00:00Z"}
{"kind": "sleep_hours", "value": 7.5, "unit": "h", "observed_at": "2026-11-15T07:00:00Z"}
{"kind": "sleep_hours", "value": 7.1, "unit": "h", "observed_at": "2026-11-16T07:00:00Z"}
{"kind": "sleep_hours", "value": 8.0, "unit": "h", "observed_at": "2026-11-17T07:00:00Z"}
{"kind": "sleep_hours", "value": 7.7, "unit": "h", "observed_at": "2026-11-18T07:00:00Z"}
{"kind": "steps", "value": 8120, "unit": "steps", "observed_at": "2026-11-12T21:00:00Z"}
{"kind": "steps", "value": 6450, "unit": "steps"}
{"kind": "steps", "value": 9930, "unit": "steps", "observed_at": "2026-11-14T21:00:00Z"}
{"kind": "steps", "value": 7310, "unit": "steps", "observed_at": "2026-11-15T21:00:00Z"}
{"kind": "steps", "value": 5880, "unit": "steps", "observed_at": "2026-11-16T21:00:00Z"}
{"kind": "steps", "value": 10240, "unit": "steps", "observed_at": "2026-11-17T21:00:00Z"}
{"kind": "steps", "value": 7765, "unit": "steps", "observed_at": "2026-11-18T21:00:00Z"}STEP 2 Send it as one batch Beta#
The API takes JSON, not JSON Lines: wrap the lines in {"observations": [ … ]}. A batch holds up to 500 lines.
jq -s '{observations: .}' week.jsonl > week.json
curl -X POST "$API/twins/$TWIN/observations" \
-H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" -d @week.jsonimport json
lines = [json.loads(l) for l in open("week.jsonl") if l.strip()]
r = requests.post(f"{API}/twins/{twin_id}/observations", headers=H, json={"observations": lines})
result = r.json()
print(r.status_code, result["accepted"], "accepted,", len(result["rejected"]), "rejected")import { readFileSync } from 'node:fs';
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;
const lines = readFileSync("week.jsonl", "utf8").split("\n").filter((l) => l.trim()).map((l) => JSON.parse(l));
const result = await c.observations.send(twinId, lines); // the SDK wraps the lines in {"observations": [ … ]}
console.log(result.accepted, "accepted,", result.rejected.length, "rejected");jq -s '{observations: .}' week.jsonl > week.json
starco obs send <twin> week.json200 { "batch_id": "b5e81f3c-…", "accepted": 12,
"rejected": [ { "index": 7, "reason": "timestamp_invalid", "field": "observed_at",
"expected": "RFC 3339 with offset, e.g. 2026-11-19T07:30:00Z" } ],
"recomputed_layers": ["wellness"] }index counts from 0, so index 7 is line 8 of the file. The 12 good lines were stored together and the wellness layer was recomputed before the answer.
STEP 3 Read the rejections#
Each rejected line has one stable reason you can program against. The ones you will meet most:
| reason | Meaning |
|---|---|
timestamp_invalid | observed_at is missing or not RFC 3339 with an offset. |
timestamp_out_of_range | In the future (beyond five minutes) or before 2000. |
unknown_kind | No such kind in the catalogue. |
unit_mismatch | Not the kind's unit; expected gives it. Nothing is converted for you. |
value_not_a_number · value_not_an_integer | Missing value or text, or a decimal for an integer kind. |
value_out_of_accepted_bounds | Outside the accepted bounds; expected is [min, max]. |
duplicate_in_batch · duplicate_observation | The same kind and time twice in the batch, or already stored. |
The full list, in the order lines are checked, is in Concepts.
STEP 4 Fix and resend the whole file Beta#
Fix line 8 from your own records and send the whole file again. You do not need to pick out the failed lines: an observation with the same kind and time as a stored one is refused as duplicate_observation, never stored twice and never overwritten. The same holds if a network error leaves you unsure whether a batch arrived.
lines[7]["observed_at"] = "2026-11-13T21:00:00Z" # the time we left out, from our own records
r2 = requests.post(f"{API}/twins/{twin_id}/observations", headers=H, json={"observations": lines})
res = r2.json()
print(r2.status_code, res["accepted"], "accepted;", {x["reason"] for x in res["rejected"]})jq -c 'if .observed_at == null then .observed_at = "2026-11-13T21:00:00Z" else . end' week.jsonl \
| jq -s '{observations: .}' > week-fixed.json
curl -X POST "$API/twins/$TWIN/observations" \
-H "Authorization: Bearer $STARCO_KEY" -H "Content-Type: application/json" -d @week-fixed.jsonlines[7].observed_at = "2026-11-13T21:00:00Z"; // the time we left out, from our own records
const res = await c.observations.send(twinId, lines);
console.log(res.accepted, "accepted;", new Set(res.rejected.map((x) => x.reason)));jq -c 'if .observed_at == null then .observed_at = "2026-11-13T21:00:00Z" else . end' week.jsonl \
| jq -s '{observations: .}' > week-fixed.json
starco obs send <twin> week-fixed.json200 1 accepted; {'duplicate_observation'}STEP 5 Check the week Beta#
curl -s "$API/twins/$TWIN/layers/wellness" -H "Authorization: Bearer $STARCO_KEY" \
| jq '[.kinds[] | select(.count > 0) | {(.kind): .count}] | add'layer = requests.get(f"{API}/twins/{twin_id}/layers/wellness", headers=H).json()
print({k["kind"]: k["count"] for k in layer["kinds"] if k["count"] > 0})const layer = await c.twins.layer(twinId, "wellness");
console.log(Object.fromEntries(layer.kinds.filter((k) => k.count > 0).map((k) => [k.kind, k.count])));starco layer <twin> wellness{ "steps": 7, "sleep_hours": 6, "hydration_glasses": 3 }The same week as a picture, line by line: what was stored, the line that had to be fixed, and the night that has no observation.
Seven concentric rings, one per day from 2026-11-12 to 2026-11-18, each a 24-hour dial in UTC. On a ring, an arc is that night's sleep_hours: its length is the value and it ends at the line's observed_at. A dot is that day's steps, at its observed_at, larger for more steps. The ring of 2026-11-14 has no arc: that night has no line, so it is null. Line 8, the steps without an observed_at, cannot be placed on a ring: it waits outside, rejected as timestamp_invalid, until it is fixed and resent, and then it lands on 2026-11-13. First send: 12 accepted, 1 rejected. Resend: 1 accepted, 12 refused as duplicate_observation. Stored in the end: sleep_hours 6, steps 7.
The week, one ring per day. Arc: sleep_hours, as long as its value and ending at its observed_at (the API stores a value and a time, not a bedtime). Dot: steps at its observed_at. Dashed ring: no line for that night, so null, never zero. These are the 13 lines of week.jsonl from step 1.
Experimental Seven days with a value is also the least an illustrative scenario needs: steps has them, so GET /v1/twins/{twin}/scenarios/wellness.steps?declared_change=500&to=2026-11-18 answers a baseline and a band; sleep_hours has six nights, so the same question answers null with the reason not_enough_days.
What just happened
Thirteen lines went through the validator one by one. Twelve were stored and triggered a recompute; one bounced with a precise reason. You fixed it from your own records and resent the whole file: the twelve already stored were refused as duplicates and only the fixed line was added. hydration_glasses has the three days from tutorial 03; the night of the 14th has no sleep observation, and every kind never sent keeps count 0 and null values.