Skip to content

Installation

npm install @muhkoo/connect@alpha

ZK login generates a Groth16 proof, which needs snarkjs. It’s imported lazily — you only need it installed if your app calls client.auth.zk.login() / register():

Terminal window
npm install snarkjs

Apps that only read public data or act as a relay don’t need it.

@muhkoo/connect ships three builds, selected automatically by your bundler via the package’s exports map:

EnvironmentNotes
BrowserThe primary target. Needs WebCrypto (every modern browser).
Node 20+globalThis.fetch + WebCrypto are built in.
Edge / serverless runtimesA trimmed build (the edge can’t run snarkjs); use it for verification + relay logic, not ZK proof generation. Selected by the workerd export condition, or import @muhkoo/connect/workers explicitly. It exports far fewer symbols than the browser and Node builds, so check its type declarations before relying on one.

The browser build reaches for a few Node globals (Buffer, process) on its crypto paths. In a Vite app with no polyfills, the build succeeds and the app loads, then throws process is not defined the first time one of those paths runs, such as hosted sign-in. Add vite-plugin-node-polyfills:

vite.config.ts
import { nodePolyfills } from "vite-plugin-node-polyfills";
export default defineConfig({
plugins: [
nodePolyfills({
include: ["buffer", "process", "util", "stream"],
globals: { Buffer: true, process: true, global: true },
}),
],
});

The browser build is large (several MB before compression). If first paint matters, load it with a dynamic import("@muhkoo/connect") so visitors who never sign in never download it.

import { Client } from "@muhkoo/connect";
const client = new Client({
apiKey: "mk_test_pk_…", // app / publishable key
// baseUrl is optional — it defaults to the hosted accelerator
// (https://api.muhkoo.dev). Set it only to target staging or self-hosting.
});
OptionTypeRequiredDescription
apiKeystringrecommendedApp key. Authenticates + meters the app. See app keys.
accessTokenstringnoA scoped, expiring access token (mk_<env>_at_…) for server-side and CI callers. Takes precedence over apiKey.
baseUrlstringnoAbsolute URL of the accelerator. Defaults to the hosted accelerator (https://api.muhkoo.dev).
authBaseUrlstringnoThe hosted sign-in app used by client.auth.hosted. Defaults to https://auth.muhkoo.dev.
functionsHostSuffixstringnoZone HTTP functions are served from, used by client.functions.invoke. Defaults to fns.muhkoo.dev.
circuitsCircuitUrlsnoZK circuit assets (wasmUrl, zkeyUrl, plus pinned SHA-256 digests). Defaults to ${baseUrl}/circuits/build/preimagePoK_js/preimagePoK.wasm and ${baseUrl}/circuits/build/preimagePoK_0001.zkey.
sessionStoreSessionStorenoToken persistence. Defaults to localStorage in a browser, and in-memory everywhere else.
offline{ enabled?, store?, cacheShards?, maxQueueBytes? }noOffline support. On by default in browsers, a no-op elsewhere.
p2p{ enabled?, workerFactory?, iceServers?, maxPeers?, debug? }noOpt-in peer-to-peer file-block exchange between Space members (browser only).
fetchtypeof fetchnoCustom fetch (tests, proxies).
logLevelstringnoSDK log verbosity.

In a browser the session is kept in localStorage (under keys starting muhkoo.session.) by default, so users stay signed in across reloads. Outside a browser, or where localStorage is blocked (private windows, sandboxed iframes), it falls back to memory for the life of the process. Pass your own sessionStore to change where it lives. The SDK exports LocalStorageSessionStore and MemorySessionStore, or you can write one:

import { Client, type SessionStore, type StoredSession } from "@muhkoo/connect";
const localStorageStore: SessionStore = {
load: () => {
const raw = localStorage.getItem("muhkoo.session");
return raw ? (JSON.parse(raw) as StoredSession) : null;
},
save: (s) => localStorage.setItem("muhkoo.session", JSON.stringify(s)),
clear: () => localStorage.removeItem("muhkoo.session"),
};
const client = new Client({ apiKey, sessionStore: localStorageStore });

On boot, call await client.auth.zk.restore() on boot — see Authentication.