#!/bin/sh # ------------------------------------ UPSCALE API ------------------------------------- # ------------------ UP-SCALES.COM JSON API OF THE SCALE WITH CURL (SHELL) -------------- # -------------------------------------------------------------------------------------- # ASK AN UP↑SCALE SCALE IN YOUR WIFI NETWORK FROM THE COMMAND LINE: THE PUBLIC STATUS, A # NONCE, THE PLAIN WEIGHT - AND SIGNED (HMAC) REQUESTS: WEIGHT, STATUS, SENSORS, THINGS, # RECORDS, ACTIONS AND THE PUSH OF ROOM VALUES TO A SENSOR SOURCE OF THE SCALE. # -------------------------------------------------------------------------------------- # ^^^ A WORKING EXAMPLE - COPY IT AND CHANGE IT AS NEEDED! # -------------------------------------------------------------------------------------- # JSON API OF THE SCALE: https://up-scales.com/manual.html#WiFiApi # SENSORS OF THE SCALE (PUSH): https://up-scales.com/manual.html#WiFiSensors # ALL EXAMPLES FOR YOUR IT: https://up-scales.com/hardware/integration-lims-webhook-api-examples.html#examples # -------------------------------------------------------------------------------------- # (C) Johann Anderson, 2025-2026 http://farm23.es # -------------------------------------------------------------------------------------- # MIT License - Copyright (c) 2025-2026 Johann Anderson # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to the following # conditions: The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE # FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # -------------------------------------------------------------------------------------- # # 1st: What you need: # # 1.) A shell with curl and openssl (Linux, macOS, Windows with WSL or Git Bash). # 2.) The scale in your WiFi network; for the signed requests: ENTERPRISE, in the app # LINK -> πŸ”— Webhook & JSON API -> πŸ”Œ JSON API & weight streams -> JSON API ON, and in the box # πŸ”— Webhook export the security "HMAC-signed" (or "TLS": the API then uses the # HMAC rules) and a secret. With the security "sealed envelope" every request must be # sealed with AES-256-GCM - openssl on the command line can not do that: use # upscale-api.py (Python) next to this file. # Actions also need "Remote actions" ON; refused in LEGAL MODE. # 3.) UPSCALE_HOST = the address of the scale, UPSCALE_KEY = the secret of the webhook box: # # UPSCALE_HOST=192.168.1.50 sh upscale-api.sh public # UPSCALE_HOST=192.168.1.50 UPSCALE_KEY=my-secret sh upscale-api.sh get /api/weight # ... sh upscale-api.sh get "/api/activity?since=1200&count=20" # ... sh upscale-api.sh get "/api/sales?day=2026-11-27&from=0" # ... sh upscale-api.sh post /api/do '{"do":"tare"}' # ... sh upscale-api.sh post "/api/sensor?source=1" '{"temperature":21.4,"humidity":45}' # # The signature: sha256=hex(HMAC-SHA256(secret, time "." nonce "." METHOD " " # PATH-WITH-QUERY "." body)) - the nonce from GET /api/nonce, one use, 60 s. # The answer is signed over (its time "." the nonce "." its body): checked below. # -------------------------------------------------------------------------------------- HOST="${UPSCALE_HOST:?set UPSCALE_HOST to the address of the scale}" KEY="${UPSCALE_KEY:-}" # the value of a key in a flat JSON answer (enough for "nonce" / "n"; use jq for more) json_value() { sed -n 's/.*"'"$1"'": *"\{0,1\}\([^",}]*\).*/\1/p' } hmac_hex() { # stdin = the text, $1 = the secret -> 64 hex openssl dgst -sha256 -hmac "$1" -r | cut -d' ' -f1 } signed() { # $1 GET | POST, $2 the path with the query, $3 the body (POST) [ -n "$KEY" ] || { echo "set UPSCALE_KEY to the secret of the webhook box" >&2; exit 1; } NONCE=$(curl -s -m 8 "http://$HOST/api/nonce" | json_value nonce) [ -n "$NONCE" ] || { echo "no nonce: is the JSON API on?" >&2; exit 1; } T=$(date +%s) SIG=$(printf '%s.%s.%s %s.%s' "$T" "$NONCE" "$1" "$2" "$3" | hmac_hex "$KEY") HDRS=$(mktemp); BODY=$(mktemp) if [ "$1" = "POST" ]; then CODE=$(curl -s -m 8 -o "$BODY" -D "$HDRS" -w '%{http_code}' -X POST "http://$HOST$2" \ -H "Content-Type: application/json" -H "X-Upscale-Time: $T" -H "X-Upscale-Nonce: $NONCE" \ -H "X-Upscale-Signature: sha256=$SIG" --data-binary "$3") else CODE=$(curl -s -m 8 -o "$BODY" -D "$HDRS" -w '%{http_code}' "http://$HOST$2" \ -H "X-Upscale-Time: $T" -H "X-Upscale-Nonce: $NONCE" -H "X-Upscale-Signature: sha256=$SIG") fi echo "HTTP $CODE" cat "$BODY"; echo # check the signature of the answer (refusals before the check - wrong signature, used nonce - are not signed) RT=$(tr -d '\r' < "$HDRS" | sed -n 's/^[Xx]-[Uu]pscale-[Tt]ime: //p') RS=$(tr -d '\r' < "$HDRS" | sed -n 's/^[Xx]-[Uu]pscale-[Ss]ignature: sha256=//p') if [ -n "$RS" ]; then WANT=$( (printf '%s.%s.' "$RT" "$NONCE"; cat "$BODY") | hmac_hex "$KEY") if [ "$WANT" = "$RS" ]; then echo "(answer signature OK)"; else echo "(ANSWER SIGNATURE WRONG - not from the scale with this secret)"; fi else echo "(answer not signed)" fi rm -f "$HDRS" "$BODY" } case "$1" in public) # without the secret: status (name, Device ID, page, API on, security 2 HMAC / 3 sealed, a secret set) and a nonce curl -s -m 8 "http://$HOST/api/status"; echo curl -s -m 8 "http://$HOST/api/nonce"; echo # the weight without the secret - only with "GET /api/weight without the secret" ON (box πŸ”Œ JSON API & weight streams) curl -s -m 8 "http://$HOST/api/weight"; echo ;; get) signed GET "${2:-/api/weight}" "" ;; post) [ -n "$3" ] || { echo "post needs the path and the JSON body" >&2; exit 1; } signed POST "$2" "$3" ;; *) echo "usage: sh upscale-api.sh public | get | post ''" >&2 exit 1 ;; esac