Skip to content

Client

import { Client } from "@muhkoo/connect";
const client = new Client(options);
OptionTypeRequiredDefaultDescription
apiKeystringrecommended—App key (mk_…). Sent as X-Muhkoo-Key.
baseUrlstringnohttps://api.muhkoo.devAbsolute accelerator URL. Trailing slash optional.
circuits{ wasmUrl: string; zkeyUrl: string; wasmSha256?: string; zkeySha256?: string }no${baseUrl}/circuits/build/preimagePoK_js/preimagePoK.wasm and ${baseUrl}/circuits/build/preimagePoK_0001.zkey, with pinned SHA-256 digestsZK circuit assets for login proofs. The default pins the digests so the assets cannot be swapped by the server they are meant to convince.
sessionStoreSessionStorenoLocalStorageSessionStore in browsers, MemorySessionStore where localStorage is unavailableSession-token persistence.
fetchtypeof fetchnoglobalThis.fetchCustom fetch.
logLevelstringno—SDK log verbosity (passed to the SDK logger’s setLevel).
offline{ enabled?, store?, cacheShards?, maxQueueBytes? }noauto (on in browsers)Offline cache + sync. On by default in browsers, no-op elsewhere.
accessTokenstringno—Machine credential (mk_<env>_at_…). Rides the same X-Muhkoo-Key header as apiKey and takes precedence over it — see access tokens.
authBaseUrlstringnohttps://auth.muhkoo.devOverride the hosted sign-in origin (staging/dev only).
functionsHostSuffixstringnofns.muhkoo.devOverride where client.functions resolves function URLs.
p2p{ enabled?, workerFactory?, iceServers?, maxPeers?, debug? }nooffPeer-to-peer shard exchange between members of a Space — see storage.

All options are optional — new Client() targets the hosted accelerator.

PropertyTypeDescription
client.authAuthNamespaceAuthentication — see client.auth.
client.kvKvNamespaceEncrypted key/value — see client.kv.
client.dbDbNamespaceScalable app database — see client.db.
client.storageStorageNamespaceFile storage — see client.storage.
client.vfsVfsNamespaceThe user’s filesystem: directories, paths, versions — see client.vfs.
client.messageMessageNamespaceRealtime pub/sub — see client.message.
client.spaceSpaceNamespaceGroup channels — see client.space.
client.offlineOfflineManagerOffline cache, status & snapshots — see client.offline.
client.agentsAgentsNamespaceProgrammable Agents — see client.agents.
client.functionsFunctionsNamespaceServerless functions — see client.functions.
client.accessTokensAccessTokensNamespaceScoped machine credentials — see access tokens.
client.appAppNamespaceWhat the signed-in user may do in this app — see client.app.
client.vcsVcsNamespaceVersion control over the VFS: commits, branches, merges, per project.
client.trust{ persistent; unpin(username); resetSpacePins(spaceId) }Pinned member identities shared by every Space. unpin(username) forgets a pinned user; resetSpacePins(spaceId) clears a Space’s pins and log heads.
PropertyTypeDescription
client.user{ username: string; commitment: string } | nullThe signed-in user (sync read).
client.isAuthenticatedbooleanWhether a session token is active (sync read).
client.baseUrlstringThe normalized accelerator URL.
access(): Promise<AppAccess>
interface AppAccess {
paidAccountsOnly: boolean; // the owner turned on "Paid Muhkoo accounts only"
allowed: boolean; // may the signed-in user add data now?
reason?: "user_plan_required" | "user_payment_past_due" | "owner_payment_past_due";
upgradeUrl?: string; // absolute portal URL to send the user to
}

Asks GET /api/app/access whether the signed-in user may add data in this app. Call it after sign-in, and again when the user returns from the portal. reason and upgradeUrl are set only when allowed is false (upgradeUrl only for user_plan_required and user_payment_past_due). Each allowed: true answer also sends any writes the offline queue kept after a payment refusal. Needs a session; throws HttpError otherwise (401 without a session, 404 against an accelerator without the route). A refused write throws PaymentRequiredError.

When a token-gated request comes back 401 (the session went stale), the client silently re-authenticates if it still holds the user’s identity (i.e. they logged in this session and never reloaded), then retries the request — so a transient expiry self-heals without surfacing an error.

MethodSignatureDescription
client.recoverSession()() => Promise<boolean>Attempt a silent re-auth now. Concurrent calls share one attempt. Resolves true on success. Call proactively if you like (e.g. on visibilitychange).
client.onSessionExpired(handler)(() => void) => () => voidFires when recovery isn’t possible — typically after a reload that kept only a now-stale token. This is your cue to route the user to the login screen. Returns an unsubscribe fn.
const off = client.onSessionExpired(() => router.push("/login"));
interface StoredSession {
token: string;
username: string;
commitment: string;
}
interface SessionStore {
load(): StoredSession | null | Promise<StoredSession | null>;
save(session: StoredSession): void | Promise<void>;
clear(): void | Promise<void>;
}

The SDK ships LocalStorageSessionStore (browser; the default there, with an optional key prefix, muhkoo.session. by default) and MemorySessionStore (the fallback in Node, Workers and SSR, or when localStorage throws). Provide your own to persist elsewhere — see Installation.