Skip to content

Client

This content is for the 0.2.0-alpha.3 version. Switch to the latest version for up-to-date documentation.

import { Client } from "@muhkoo/connect";
const client = new Client(options);
OptionTypeRequiredDefaultDescription
apiKeystringrecommendedApp key (mk_…). Sent as X-Muhkoo-Key.
baseUrlstringnothe hosted acceleratorAbsolute accelerator URL. Trailing slash optional.
circuits{ wasmUrl: string; zkeyUrl: string }no${baseUrl}/circuits/build/preimagePoK{.wasm,_0001.zkey}ZK circuit assets for login proofs.
sessionStoreSessionStorenoin-memorySession-token persistence.
fetchtypeof fetchnoglobalThis.fetchCustom fetch.
logLevelstringnoSDK log verbosity.

All options are optional — new Client() targets the hosted accelerator with an in-memory session.

PropertyTypeDescription
client.authAuthNamespaceAuthentication — see client.auth.
client.storageStorageNamespaceEncrypted KV — see client.storage.
client.messageMessageNamespaceRealtime pub/sub — see client.message.
client.spaceSpaceNamespaceGroup channels — see client.space.
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.

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 MemorySessionStore (the default). Provide your own to persist across reloads — see Installation.