Installation & pairing
From zero to a paired connection. You will not push any data here — that happens in Syncing.
Requirements
- Node.js 18+ (the client uses built-in
node:cryptoand the globalfetch). - Your site's publishable key (
pk_…), copied from the Monoverse cabinet. It is bound to your shop's domain. - A public https:// origin on that domain. While pairing, Monoverse fetches
/monoverse/pair-challengethere to prove you serve it. - A full-entropy 32-byte
encryptionKey— generate one withopenssl rand -hex 32and keep it in your secret manager.
1. Install
npm install @monoverse/node
Zero runtime dependencies — the package ships its own .d.ts and is TypeScript-strict.
2. Construct the client
import { MonoverseClient } from '@monoverse/node';
export const client = new MonoverseClient({
baseUrl: 'https://api.monoverse.tech',
siteUrl: 'https://shop.example.com', // the https:// origin the pk_ key is bound to
// publicKey: 'pk_…' — read from MONOVERSE_PUBLIC_KEY when omitted
encryptionKey: process.env.MONOVERSE_ENCRYPTION_KEY!,
credentialsFile: './.monoverse-credentials.enc', // omit for in-memory only
});
| Option | Purpose |
|---|---|
baseUrl | Ingest base URL. Must be https:// (localhost allowed for dev). |
siteUrl | The https:// origin of your shop, on the domain the key is bound to. Sent as site_url when pairing and as X-Monoverse-Site-Url on every signed request. |
publicKey | The site's pk_… key. Falls back to the MONOVERSE_PUBLIC_KEY environment variable. |
encryptionKey | Encrypts the stored shared secret at rest (AES-256-GCM). |
credentialsFile | Path for the encrypted credential envelope. Omit to keep credentials in memory only. With it set, the pairing nonce waits beside it, at <credentialsFile>.pair-nonce. |
baseUrl must be https://. Plaintext http:// is rejected (an HMAC over http would leak the
signature to a network attacker). localhost / 127.0.0.1 / ::1 are the only exceptions (local dev).
3. Get your publishable key
In your Monoverse cabinet, copy the site's publishable key. It starts with pk_ and is bound to
your shop's domain. It is not a secret and it cannot read anything: it only starts a pairing on that
domain. Put it in the MONOVERSE_PUBLIC_KEY environment variable, or pass it as publicKey.
4. Mount the handler
Pairing proves you serve the domain. Before the server hands out a shared secret it sends one
GET https://shop.example.com/monoverse/pair-challenge?challenge=… and expects the HMAC of the
challenge, keyed by a nonce only your app holds. The client answers for you: mount its handler on
your site, before any body parser.
import express from 'express';
import { client } from './monoverse';
const app = express();
app.use(client.nodeHandler()); // BEFORE express.json()
app.use(express.json());
nodeHandler() serves Express, Connect and node:http. For the Next.js app router, Hono, Bun and
Deno use fetchHandler(), which is (Request) => Promise<Response>:
const handler = client.fetchHandler();
export { handler as GET, handler as POST };
The same handler also receives the signed POST /monoverse/sync and POST /monoverse/forms/submit
calls the server makes once you are paired. It verifies them over the raw request body, so a body a
parser already consumed is refused with 400 raw_body_unavailable.
5. Pair
app.listen(3000, async () => {
const paired = await client.ensurePaired();
console.log(paired.action, 'tenant', paired.tenantId); // 'paired' | 'declared' | 'none'
});
ensurePaired() is the one call to make at start-up. It pairs only when no credentials are stored.
With a stored connection it leaves the pairing alone ('none'), and it declares again ('declared')
when the stored declaration came from another SDK version or your handlers changed. pair() takes
no argument and pairs whether or not credentials are stored.
Pairing draws a fresh 32-byte nonce, holds it for 10 minutes, and sends it to
POST /api/v1/ingest/pair-by-key with your key, your siteUrl and the callbacks and stack it
detected. The server fetches your challenge route, checks the proof, and answers with your
tenant id and shared secret. The client stores both encrypted, clears the nonce, and returns
a secret-free summary (tenantId, ingestUrl, optional account). The shared secret is
never returned to the caller.
Pairing stores the secret encrypted (AES-256-GCM, keyed by your encryptionKey) and the Credentials
object redacts it from JSON.stringify, console.log, and toString. There is no API that reveals it.
If you lose the key, re-pair.
When pairing is refused
A refusal is a PairingError whose code is the server's, and whose message says what to fix:
code | What it means | What to do |
|---|---|---|
invalid_key | The key is unknown or inactive. | Copy the site's pk_ key from the cabinet again. |
key_has_no_domain | The key is bound to no domain yet. | Bind it to your shop's domain in the cabinet, then pair again. |
domain_mismatch | siteUrl is not on the key's domain. | Set siteUrl to https:// plus the domain the cabinet shows beside the key. |
domain_unproven | The server could not read your /monoverse/pair-challenge with this pairing's proof. | Mount the handler on the deployed site, over https, and pair from a process that shares its pairing store. |
Pairing from a CLI, and several instances
The nonce must reach the process that answers the challenge. With credentialsFile set, the client
writes it beside the credentials, at <credentialsFile>.pair-nonce (mode 0600, kept at most 10
minutes), so ensurePaired() can run from a CLI while the web process answers. With several
instances behind one domain, put that file on a volume they all share, or pair while a single
instance runs. Without credentialsFile the nonce stays in memory; pass pairingStore to use your
own.
Already paired?
The client loads stored credentials on first use, so a restart pairs nothing again. To switch
tenants, unpair() first, then call ensurePaired() again.
How signing works
You normally never touch the signer — every authenticated method on MonoverseClient signs for you. This
is what happens on the wire, so you can debug a 401 bad_signature.
Every authenticated ingest request is signed with HMAC-SHA256 over a canonical string. The
shared secret comes from pairing (32 random bytes, base64 in the pair-by-key response — decode to raw
bytes before signing). The string-to-sign is exactly five fields joined by \n:
canonical = METHOD + "\n" + PATH + "\n" + TIMESTAMP + "\n" + NONCE + "\n" + SHA256_HEX(BODY)
signature = HEX( HMAC_SHA256(shared_secret_raw, canonical) )
METHOD— uppercase HTTP verb (POST,PUT,GET).PATH—request.url.pathonly: no host, no query string.TIMESTAMP— the exact string sent inX-Monoverse-Timestamp(Unix epoch seconds, UTC). Do not reformat it.NONCE— the exact string sent inX-Monoverse-Nonce(base64url of 16 random bytes). Do not decode it.SHA256_HEX(BODY)— lowercase hex SHA-256 of the raw request body bytes you actually send. For an empty body this is the well-known constante3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.
The bytes you hash must be byte-for-byte the bytes you transmit — re-serializing the JSON after signing changes the hash and the server rejects it.
POST
/api/v1/ingest/init
1747396800
aGVsbG8td29ybGQtYTFiMmMz
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
A lowercased method, an extra trailing newline, a re-encoded nonce, or hashing a re-serialized body
all produce a different signature and a 401 bad_signature. Build the canonical string from the
exact header strings and the exact body bytes.
Replay protection. Each request also carries a fresh timestamp and nonce. The server accepts a
timestamp within ±300s of its clock (stale_timestamp otherwise) and a nonce only once per
600s window per tenant (nonce_replay otherwise). Because the nonce is consumed even on an error,
do not blindly retry a signed request — see retry semantics in your endpoint reference.
The request headers
Every authenticated request carries these headers. They are the inputs to the signature, the tenant binding, and the replay window.
| Header | Value | Notes |
|---|---|---|
X-Monoverse-Tenant-Id | UUID | Returned by pair-by-key. Identifies the tenant whose secret signed the request. |
X-Monoverse-Timestamp | integer (Unix epoch seconds, UTC) | Replay window is ±300s of server time. |
X-Monoverse-Nonce | base64url of 16 random bytes | Single-use; tracked for 600s per tenant. Fresh value per request. |
X-Monoverse-Signature | 64-char lowercase hex | HMAC_SHA256 of the canonical string. |
X-Monoverse-Protocol-Version | 2 | Always the integer 2 on the wire. Required on every request including pair-by-key. |
X-Monoverse-Plugin-Version | semver string | Producer version, e.g. 2.1.0. Telemetry only. |
X-Monoverse-Site-Url | URL | Origin of the sending site. Heartbeat upserts the site row from it. |
Content-Type | application/json or application/x-ndjson | Per endpoint; NDJSON upload may be gzipped. |
Idempotency-Key | UUID | Optional on /events; required on full-snapshot /finalize. Response cached 24h. |
The wire header X-Monoverse-Protocol-Version must always be the integer 2 — a mismatch is
rejected with 426 Upgrade Required. The JSON body of the pair-by-key response currently reports
"protocol_version": 1, which is a server-side field value, not the version you send. Ignore the
response field for signing purposes and keep sending 2 on the wire.
The client always sends X-Monoverse-Protocol-Version: 2 (the PROTOCOL_VERSION constant). The pairing
response body reports protocol_version: 1 — that field is stale; never read it for signing.
Sending anything but 2 on the wire is rejected with 426 Upgrade Required.
The encrypted credential store
The client persists credentials through a CredentialStore. You get the right one automatically from
the constructor options:
| You pass | Store used | Where the secret lives |
|---|---|---|
credentialsFile + encryptionKey | FileCredentialStore | AES-GCM envelope on disk (0600) |
encryptionKey only | MemoryCredentialStore | AES-GCM envelope in process memory |
credentialStore | your implementation | wherever you put it (e.g. a KMS-backed store) |
Use a full-entropy 32-byte key. A 32-byte Buffer, 64-char hex, or 32-byte base64 string is used
directly; a plain passphrase is an explicit weaker fallback (scrypt-stretched) and must be at least
16 chars — anything shorter is rejected.
Next
- Map your data — build canonical entities with the typed builders.
- Sync — push the baseline snapshot, then deltas.