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 04

Events, and a receiver that verifies.

Read what changed in a project with a cursor, and write the one function every webhook receiver needs: the check that a delivery really comes from STARCO and is not a replay.

time 5 minuses events · webhook signatures (HMAC-SHA256) 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#

Every change is an event with a seq that only grows: ask for the events after the last one you processed. A webhook pushes the same events to your https address with X-Starco-Signature: t=<unix time>,v1=<hex>, where v1 is the HMAC-SHA256 of <t>.<raw body> under the secret shown once when the webhook was created. The receiver must check the signature on the raw body, in constant time, and reject old timestamps.

Subscribing a webhook needs a public https address, so this example signs a sample delivery itself, with a made-up secret, to exercise the verification. The events part talks to the API.

The whole program#

export API="https://api.starco.dev/v1"
AUTH="Authorization: Bearer $STARCO_KEY"
JSON="Content-Type: application/json"
TWIN=$(curl -s -X POST "$API/twins" -H "$AUTH" -H "$JSON" \
  -d '{"template": "daily_movement", "layers": ["wellness"], "label": "events"}' | jq -r .twin.id)
curl -s -X PUT "$API/twins/$TWIN/state/wellness.mood_level" -H "$AUTH" -H "$JSON" -d '{"value": 4}' > /dev/null
curl -s "$API/events?after=0&twin_id=$TWIN" -H "$AUTH" | jq -r '.data[] | "\(.type) \(.measure) \(.version)"'

# The check a receiver makes, with openssl: recompute v1 from t and the raw body, then compare.
SECRET="whsec_example"; BODY='{"type":"state.changed"}'; T=$(date +%s)
SIG="t=$T,v1=$(printf '%s.%s' "$T" "$BODY" | openssl dgst -sha256 -hmac "$SECRET" | sed 's/^.* //')"
T2=${SIG#t=}; T2=${T2%%,*}; V1=${SIG##*v1=}
MINE=$(printf '%s.%s' "$T2" "$BODY" | openssl dgst -sha256 -hmac "$SECRET" | sed 's/^.* //')
AGE=$(( $(date +%s) - T2 ))
if [ "$MINE" = "$V1" ] && [ "$AGE" -ge -300 ] && [ "$AGE" -le 300 ]; then echo "signature ok"; else echo "signature refused"; fi
Output of the curl tabillustrative · synthetic
twin.created null null
state.changed wellness.mood_level 1
True False False

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

What just happened

The cursor returned the two events of the twin in order. The verification accepted the delivery signed with the secret, and refused the same signature over a changed body and over another secret.

Go further#