SHC vNext QR proof of concept

For developers

What an app does to show a tracking history as one of these codes, and what an app does to read one.

The sending app fills in a small input model and calls the codec's encoder; it never builds FHIR or bytes by hand. The receiving app runs the same codec to get a FHIR Bundle. Both sides use one WebAssembly module, published here as codecs/1.wasm.

1. Fill in the input model

The input model is defined by a TypeScript file, model.d.ts (view as text), which is normative; a JSON Schema is derived from it. Every per-day series is a uniform flat array of { date, ... } entries using standard FHIR DateTime (YYYY-MM-DD or full date with time and UTC offset, accepting Z and :00 seconds); an omitted entry or absent field means "not recorded", never "no". Numbers are natural JSON integers or exact decimals (36.52, 12.5), and every top-level field is optional (including exportedAt, a FHIR instant mapped to Bundle.timestamp). Built-in slots use readable string keywords (SymptomKind, MedicationKind, ConditionKind, Mucus, Sex, Mood, Energy, ProcedureKind, IntervalKind, ExcludeReason, …) that expand on decode to canonical SNOMED CT, LOINC, or cycle-log codings. Escapes and custom[] use genuine FHIR R4 CodeableConcept objects ({ coding: [{ system, code, display? }], text? } or text-only { text } without fake app URIs), preserving display and text on any non-built-in escape concept for offline readability. What has no built-in slot goes in custom[], declaring its FHIR Observation.value[x] via valueType: "boolean" (valueBoolean), "coded" (valueCodeableConcept), "quantity" (valueQuantity decimal in UCUM unit), or "components" (component[], such as blood pressure) (core rules); vital-sign codes are allowed there. Multiple entries on one date (timed or untimed, even disagreeing) are simply additional elements in the array, and procedures and answers carry their own FHIR date directly. In FHIR, what a card can hold is stated by the codec's FHIR profiles: a Bundle that conforms to them lifts into the model exactly, and the decoder's output always conforms.

The smallest useful model, three months of bleeding days, published as minimal-teaching.model.json and explained line by line on the input model page:

{
  "bleeding": [
    { "date": "2026-01-05", "value": "moderate" },
    { "date": "2026-01-06", "value": "heavy" },
    { "date": "2026-01-07", "value": "moderate" },
    { "date": "2026-01-08", "value": "light" },
    { "date": "2026-01-09", "value": "spotting" },
    { "date": "2026-02-02", "value": "light" },
    { "date": "2026-02-03", "value": "heavy" },
    { "date": "2026-02-04", "value": "moderate" },
    { "date": "2026-02-05", "value": "light" },
    { "date": "2026-02-06", "value": "spotting" },
    { "date": "2026-03-03", "value": "moderate" },
    { "date": "2026-03-04", "value": "heavy" },
    { "date": "2026-03-05", "value": "moderate" },
    { "date": "2026-03-06", "value": "light" },
    { "date": "2026-03-07", "value": "spotting" }
  ]
}

The codec turns this into a card of 17 bytes (42 digits, a Version 4 QR code with this site's link in front), which decodes to a FHIR Bundle of 31 entries and 11,632 bytes. One model for each sample record is among the sample files.

2. Encode it and draw the QR code

This script fetches the example model, loads the codec module and the site's small library, lib/shc2.js (digits, card framing, a codec host and a QR encoder, built from the repository's src/), encodes the model, and draws the code. The last lines are the receiving side. It runs as shown with Bun or Node, once the library is saved next to it as shc2.mjs (the .mjs ending tells Node it is a module):

curl -o shc2.mjs https://joshuamandel.com/shc2-poc/lib/shc2.js
# save the code below as example.mjs, then:
node example.mjs      # or: bun example.mjs

Bun and Node cannot import a module from an https address, hence the download. In a browser, a <script type="module"> can import the library from its address instead: from "https://joshuamandel.com/shc2-poc/lib/shc2.js".

import { bytesToDigits, digitsToBytes, encodeQr, qrToSvg, pack, parse, runCodec } from "./shc2.mjs";

const SITE = "https://joshuamandel.com/shc2-poc/";

// 1. The input model: readable built-in keywords, uniform { date, ... }[] arrays, FHIR CodeableConcept escapes, and dateTime (see the Input model page).
//    Here, the published teaching example; an app builds this object from its own data.
const model = await (await fetch(SITE + "codecs/cycle-log/examples/minimal-teaching.model.json")).json();

// 2. Load the codec by its registry id (1, cycle-log) and encode the model.
const wasm = new Uint8Array(await (await fetch(SITE + "codecs/1.wasm")).arrayBuffer());
const { instance } = await WebAssembly.instantiate(wasm, {}); // no imports: it can only compute
const { memory, shc_alloc, shc_encode } = instance.exports;
const json = new TextEncoder().encode(JSON.stringify(model));
const ptr = shc_alloc(json.length);
new Uint8Array(memory.buffer, ptr, json.length).set(json);
const at = shc_encode(ptr, json.length); // a 12-byte record: status, output pointer, output length
const rec = new DataView(memory.buffer, at, 12);
const status = rec.getInt32(0, true);
const output = new Uint8Array(memory.buffer, rec.getUint32(4, true), rec.getUint32(8, true)).slice();
if (status !== 0) {
  // 1 MALFORMED, 2 UNSUPPORTED, 3 LIMIT; the message starts with the field at fault
  throw new Error(`refused (${status}): ${new TextDecoder().decode(output)}`);
}

// 3. The card: the codec reference (id 1 is the single byte 0x01), the codec's input, and a
//    2-byte check (the first 2 bytes of the SHA-256 of the rest), which pack() appends.
const card = pack({ codec: { id: 1 }, input: output });

// 4. The QR text: the site's address, "#shc2:/", then 17 digits for every 7 bytes.
const prefix = SITE + "#shc2:/";
const digits = bytesToDigits(card);
const qr = encodeQr([{ mode: "byte", data: prefix }, { mode: "numeric", data: digits }], { ecLevel: "L" });
const svg = qrToSvg(qr); // an <svg> string: dark modules on white, with the quiet zone

// Receiving: digits -> card -> codec by id -> FHIR Bundle.
const received = parse(digitsToBytes(digits)); // checks the check, then { codec: { id: 1 }, input }
const { bundle } = await runCodec(wasm, received.input); // checks the module, runs a fresh instance
console.log(`card ${card.length} bytes, ${digits.length} digits, QR Version ${qr.version}; read back: ${bundle.entry.length} entries`);

For a link to send as text rather than a QR code, toLinkText(card, SITE + "#") writes the same card as shc2:/b/ and base64url. It is shorter, but once sent it stays wherever the message is stored and cannot be taken back.

Run this example in the browser: the same code, loading the module and library from this site, with its result shown on the page.

3. Handle a refusal

The encoder refuses a model it cannot represent exactly; it never drops or approximates a fact. The status says what kind of problem it is and the message starts with the field at fault, so an app can point at the value. For the example above:

Change to the modelWhat shc_encode returns
bleeding: [{ date: "2026-01-01", value: "gushing" }]MALFORMED: bad bleeding
pain: [{ date: "2026-01-01", value: 11 }]MALFORMED: pain 0..10
bleeding: [{ date: "2026-02-30", value: "light" }]MALFORMED: invalid entry date

LIMIT means the data is valid but too much for one card: more than 2,875 bytes with this site's link in front (what Version 40 holds), a Bundle over 4 MiB, or more coding work than the format allows.

Two experimental tools sit beside the encoder for an app that starts from FHIR or needs a smaller code. The lifter reads an arbitrary FHIR Bundle into the input model, with a report saying for every resource whether it is carried exactly, carried with a named change, or left out and why. The planner reduces a model by named steps until its card fits a chosen QR code, and says what each step lost. Fit a FHIR Bundle runs both in the browser; Lifting and fitting documents them.

4. Read a code

  1. Take the text after shc2:/. An app that registers shc2:/, or recognises this site's link, ignores everything before it and shows the record in its own viewer, with no browser and nothing from this site: the mapping from card to FHIR is in the codec.
  2. Turn the text into bytes. From a QR code it is digits: every 17 digits are one 7-byte block, and a shorter last group holds the remaining 1 to 6 bytes (digitsToBytes). From a link that was pasted or sent it is b/ and base64url, about 45% shorter; cardBytesFromText reads either form. A QR code must use the digits, which every scanner returns unchanged.
  3. Check the card: its last 2 bytes must equal the first 2 bytes of the SHA-256 of the bytes before them. A card that fails was cut short or changed on the way; refuse it and ask for the code or link again (parse does this). Then read the codec reference: a registry id as a variable-length integer, or a zero byte and the module's SHA-256.
  4. Find the module for that id in the codec directory (or bundle it with the app) and check its hash.
  5. Run it as the codec interface describes: no imports, a fresh instance per card, the input through shc_alloc, then shc_decode (runCodec). Status 0 gives a FHIR R4 Bundle; any other status means the card cannot be read.
  6. Treat the result as unsigned, patient-reported data. The first entry is the Patient; it is empty unless the card carries a name or birth date.

A phone with no such app reads the code with its camera and opens this site's viewer, which does the same steps in the browser.

Limits and what is not available yet