Skip to main content

Encrypt Monitorable Data

In this guide, we will register and update a monitorable with encrypted requests.

Why Encrypt Monitorable Data?​

Monitorables carry personally identifiable information: names, phone numbers, addresses, and emergency contacts. An encrypted request keeps that data unreadable to anything between your device and Hyperion, not only protected by HTTPS.

The request you encrypt is the same JSON you would send as plaintext. You encrypt it to your Hyperion encryption key and send it inside an envelope. Hyperion decrypts it and processes it normally, so the response is the same as for a plaintext request.

When to use encryption:

  • When requests come from devices or apps outside your own infrastructure
  • When regulatory compliance requires end-to-end encryption
  • When you want additional security guarantees beyond HTTPS

For the full construction of every encrypted request, see the Encrypted Requests concept page.

Getting Started​

Before encrypting data, you need:

  1. An application with the key:read and monitorable:write scopes. If you haven't created an application yet, see the Create Application guide.
  2. An HPKE encryption key. See Encryption Keys to create one. The examples use HPKE-XWING-SHA256-AES256GCM.

The examples are TypeScript for Node.js 24 or later and use the hpke package.

Step 1: Get the Current Encryption Key​

Fetch the key to encrypt to with the Get Current Encryption Key endpoint:

cURLGET

You need three fields from the response:

  • id - Sent as encryptionKeyId in the envelope
  • algorithm - Selects the HPKE cipher suite
  • publicKey - Base64 SPKI DER public key

Fetch the key again after its inactiveAt, when the next key takes over.

Step 2: Set Up the Helpers​

Every encrypted request is built from a few small helpers. The other encryption guides reuse them.

import { createHash } from "node:crypto";
import {
AEAD_AES_256_GCM,
CipherSuite,
KDF_HKDF_SHA256,
KEM_MLKEM768_X25519,
} from "hpke";

export interface EncryptionKey {
id: string;
publicKey: string;
}

const XWING_PUBLIC_KEY_LENGTH = 1216;
const encoder = new TextEncoder();

export const suite = new CipherSuite(
KEM_MLKEM768_X25519,
KDF_HKDF_SHA256,
AEAD_AES_256_GCM,
);

export const EMPTY = new Uint8Array(0);

export function toBase64(bytes: Uint8Array): string {
return Buffer.from(bytes).toString("base64");
}

export function fromBase64(value: string): Uint8Array {
return new Uint8Array(Buffer.from(value, "base64"));
}

export function sha256(bytes: Uint8Array): Uint8Array {
return new Uint8Array(createHash("sha256").update(bytes).digest());
}

export function concat(...parts: Uint8Array[]): Uint8Array {
return new Uint8Array(Buffer.concat(parts));
}

export function lp(value: Uint8Array | string): Uint8Array {
const bytes = typeof value === "string" ? encoder.encode(value) : value;
const length = new Uint8Array(2);
new DataView(length.buffer).setUint16(0, bytes.byteLength);
return concat(length, bytes);
}

export function canonicalTimestamp(date: Date = new Date()): string {
return `${date.toISOString().slice(0, 19)}Z`;
}

export function jsonBytes(value: object): Uint8Array {
return encoder.encode(JSON.stringify(value));
}

export function importEncryptionKey(encryptionKey: EncryptionKey) {
const spki = fromBase64(encryptionKey.publicKey);
return suite.DeserializePublicKey(
spki.subarray(spki.byteLength - XWING_PUBLIC_KEY_LENGTH),
);
}

export async function sealHpke(
encryptionKey: EncryptionKey,
plaintext: Uint8Array,
info: Uint8Array,
aad: Uint8Array,
): Promise<Uint8Array> {
const { encapsulatedSecret, ciphertext } = await suite.Seal(
await importEncryptionKey(encryptionKey),
plaintext,
{ info, aad },
);
return concat(encapsulatedSecret, ciphertext);
}

lp prefixes a value with its 2-byte length, and canonicalTimestamp produces the YYYY-MM-DDTHH:MM:SSZ form the envelopes require. An X-Wing publicKey is an SPKI that ends with the 1216-byte raw key, which importEncryptionKey passes to HPKE. sealHpke returns the encapsulated key followed by the ciphertext, which is the wire format Hyperion expects.

For other HPKE algorithms, build the matching CipherSuite and import the public key for that KEM. The info and aad values don't change.

Step 3: Register an Encrypted Monitorable​

Build the same create request you would send as plaintext. See Monitor Premises, Monitor People, or Monitor Vehicles for the fields of each type.

Encrypt it with an empty HPKE info and lp(requestTimestamp) as the aad:

const requestTimestamp = canonicalTimestamp();
const encryptedPayload = await sealHpke(
encryptionKey,
jsonBytes(createMonitorableRequest),
EMPTY,
lp(requestTimestamp),
);

const envelope = {
encryptionKeyId: encryptionKey.id,
requestTimestamp,
encryptedPayload: toBase64(encryptedPayload),
};

Send the envelope as the JSON body to the /monitorables endpoint:

cURLPOST

The response is the same monitorable response a plaintext request returns.

To give the monitorable a signing key at registration, add pkSigningKey and signature to the envelope. See Sign Requests.

Step 4: Update an Encrypted Monitorable​

An update is bound to the monitorable it changes. Encrypt the same update request you would send as plaintext, including its type, with lp(monitorableId) as both the HPKE info and aad:

const requestTimestamp = canonicalTimestamp();
const encryptedPayload = await sealHpke(
encryptionKey,
jsonBytes(updateMonitorableRequest),
lp(monitorableId),
lp(monitorableId),
);

const envelope = {
encryptionKeyId: encryptionKey.id,
requestTimestamp,
encryptedPayload: toBase64(encryptedPayload),
};

Send the envelope to the Update Monitorable endpoint:

cURLPATCH

If the monitorable has a signing key, an unsigned update returns 401, and the HPKE info must also include the key's hash. See Sign Requests.

Try It: Build an Encrypted Request​

Use the builder below to run the steps above in your browser. Paste your current encryption key, fill in the request, and select Encrypt request to get an envelope and a cURL command you can send.

The builder:

  • Encrypts register requests with an empty info, and update requests with lp(monitorableId)
  • Adds pkSigningKey, pkSigningKeyHash, and signature when you select a signing profile
  • Shows the exact request it encrypts next to the envelope

Generate a demo key to see the envelope format without an API call. Hyperion can't decrypt requests built with a demo key, so use your own key to send them.

Encrypted monitorable request builder

Fill in a register or update request and encrypt it to your encryption key.

Encryption key

Paste the id, algorithm, and publicKey from GET /v1/encryption-keys/current, or generate a demo key. Hyperion can't decrypt requests built with a demo key.

Monitorable signing key

Generated in this browser tab and kept only until you reload. Other builders on these pages sign with the same key.

No encryption key yet
Key no key ID · no signing key

Request

External identifier to represent this monitorable. Alternatively, send the optional query parameter externalId; a query value takes precedence over the body.

Personal identification code for the monitorable. Cannot be reused across contacts and the monitorable.

Optional base64-encoded SPKI DER monitorable signing public key. When stored, later encrypted requests for this monitorable must include a valid signature.

Type of premises being monitored

Request to encrypt
{}

Conclusion​

You've registered and updated a monitorable with encrypted requests. Hyperion stores the decrypted data the same way as for plaintext requests, so you read it back with the normal endpoints, or with an encrypted lookup.

Best Practices​

  • Use the current key: Fetch /encryption-keys/current and refresh it after inactiveAt
  • Send encryptionKeyId: Name the key you encrypted to, so a key rotation in flight doesn't break the request
  • Use canonical timestamps: Timestamps must be exactly YYYY-MM-DDTHH:MM:SSZ
  • Hash the raw bytes: Every hash in an envelope is over decoded bytes, not Base64 text
  • Add a signing key: A monitorable signing key proves each request came from your device

Learn More​

API Reference​