Skip to content

QR labels for a production batch

List the published units of a batch by serial number prefix and save the QR code of each one as an SVG file for label printing.

The script lists the units whose serial number starts with a prefix (your batch, for example SV2027-0001) and whose passport is published, with listBatteries, then saves the QR code of each unit with getPassportQr as labels/<serial number>.svg. It needs a key with the scope read.

  • Other ways to select a batch: import_id (the units of one spreadsheet import) or model_version_id. Replace serial_prefix in the query.
  • Labels printed before publication: use passport_status=qr_locked instead. The QR code exists as soon as the identifiers are locked, and the link inside it never changes afterwards.
  • SVG scales for any label size. For a bitmap, request ?format=png&size=1024 (128 to 2048 pixels).
  • A / in a serial number becomes _ in the file name.
#!/usr/bin/env bash
# qr-labels.sh SV2027-0001: an SVG QR code per published unit of a batch (needs curl and jq)
set -euo pipefail
prefix="$1" # the batch: serial numbers that start with this, e.g. SV2027-0001
auth="Authorization: Bearer $BATTERYPASS_API_KEY"
mkdir -p labels
cursor=""
while :; do
  page=$(curl -sS --fail --retry 5 -G "$BATTERYPASS_URL/api/v1/batteries" -H "$auth" \
    --data-urlencode "serial_prefix=$prefix" -d passport_status=published -d limit=200 \
    ${cursor:+--data-urlencode "cursor=$cursor"})
  jq -r '.items[] | [.id, (.serial_number | gsub("/"; "_"))] | @tsv' <<<"$page" |
    while IFS=$'\t' read -r id serial; do
      curl -sS --fail --retry 5 -H "$auth" -o "labels/$serial.svg" \
        "$BATTERYPASS_URL/api/v1/batteries/$id/passport/qr?format=svg"
      echo "labels/$serial.svg"
    done
  cursor=$(jq -r '.next_cursor // empty' <<<"$page")
  [ -n "$cursor" ] || break
done
Output
labels/SV2027-000182.svg
labels/SV2027-000184.svg
labels/SV2027-000185.svg

Each unit costs one request, and all keys of your organisation share 600 requests per minute; the scripts wait when they get 429. More on the QR code and the other exports in Export passports.