TypeScript SDK Reference
What this page covers
stigmem-ts is the official TypeScript client for the Stigmem REST
API. It covers facts, conflicts, federation, lint, recall, memory
cards, and subscriptions. Targets Node 20+ and is bundler-compatible
(esbuild, Rollup, Webpack, Vite).
Audience: TypeScript / Node.js developers integrating with a Stigmem node.
Installation
npm install stigmem-ts
# or
pnpm add stigmem-ts
Quick start
import { StigmemClient, sv, tv } from "stigmem-ts";
const client = new StigmemClient({
url: "http://localhost:8000",
apiKey: process.env.STIGMEM_API_KEY,
});
// Assert a fact
const fact = await client.assertFact(
"user:alice",
"memory:role",
sv("engineer"),
"agent:assistant",
{ scope: "local" },
);
// Hybrid recall
const recall = await client.recall("Alice's current role", { token_budget: 500 });
for (const sf of recall.facts) {
console.log(`[${sf.score.toFixed(3)}] ${sf.fact.relation}`);
}
// Entity memory card
const card = await client.getCard("user:alice");
console.log(card.summary);
StigmemClient
new StigmemClient(opts: {
url: string;
apiKey?: string;
timeoutMs?: number;
fetch?: typeof fetch; // inject a custom fetch (e.g. undici, node-fetch, test mocks)
})
All methods return promises and throw StigmemHTTPError subclasses on non-2xx responses.
Facts
client.assertFact(entity, relation, value, source, opts?: AssertOptions): Promise<Fact>
client.retract(entity, relation, scope, source, value?): Promise<Fact>
client.getFact(factId: string): Promise<Fact>
client.query(opts?: QueryOptions): Promise<FactPage>
AssertOptions:
confidencenumberscopeFactScopevalid_untilstringQueryOptions:
entity / relation / sourcestringscopeFactScopemin_confidencenumberinclude_contradictedbooleaninclude_expiredbooleancursor / afterstringlimitnumberUse page.cursor from the response for cursor-based pagination.
Recall
client.recall(query: string, opts?: RecallOptions): Promise<RecallResponse>
RecallOptions:
scopeFactScopetoken_budgetnumberdepthnumberweightsRecallWeightsmin_confidencenumberinclude_neighborsbooleanlimitnumberRecallResponse fields:
recall_idquery_hashfactstotal_scoredtoken_budget / tokens_usedtruncatedRecallWeights defaults: { lexical: 0.35, semantic: 0.35, graph: 0.15, source_trust: 0.10, recency: 0.05 }.
Memory cards
client.getCard(entityUri: string, opts?: CardOptions): Promise<MemoryCard>
CardOptions: scope (default "local"), refresh (default false).
import { StigmemNotFoundError } from "stigmem-ts";
try {
const card = await client.getCard("user:alice", { scope: "local" });
console.log(card.summary);
} catch (err) {
if (err instanceof StigmemNotFoundError) {
console.log("Entity has no live facts");
}
}
MemoryCard fields:
entity_uriscopesummaryfact_hashesavg_confidencerefreshed_atis_stalehas_contradictionsConflicts · Lint · Federation · Node info
client.listConflicts(opts?: ConflictListOptions): Promise<ConflictPage>
client.resolveConflict(conflictId: string, opts?: ResolveOptions): Promise<ConflictResolution>
client.lint(scope: FactScope, opts?: LintOptions): Promise<LintResult>
client.federationStatus(): Promise<Peer[]>
client.nodeInfo(): Promise<NodeInfo> // GET /.well-known/stigmem (no auth)
Subscribe (polling, async generator)
client.subscribeScope(
scope: FactScope,
opts?: SubscribeOptions,
): AsyncGenerator<Fact[]>
SubscribeOptions: intervalMs (default 30000), signal (AbortSignal).
const ac = new AbortController();
setTimeout(() => ac.abort(), 60_000);
for await (const batch of client.subscribeScope("local", { intervalMs: 5_000, signal: ac.signal })) {
console.log(`${batch.length} new fact(s)`);
}
The generator tracks a cursor internally.
Each batch contains only facts newer than the previous poll.
Value constructors
import { sv, tv, nv, bv, dtv, rv, nullv } from "stigmem-ts";
sv("hello") // StringValue { type: "string", v: "hello" }
tv("long text...") // TextValue { type: "text", v: "..." }
nv(3.14) // NumberValue { type: "number", v: 3.14 }
bv(true) // BooleanValue { type: "boolean", v: true }
dtv("2026-05-04T00:00:00Z") // DatetimeValue { type: "datetime", v: "..." }
rv("stigmem://company.example/x") // RefValue { type: "ref", v: "..." }
nullv() // NullValue { type: "null" }
Errors
StigmemHTTPErrorStigmemAuthErrorStigmemNotFoundErrorStigmemConflictErrorimport {
StigmemAuthError,
StigmemNotFoundError,
StigmemConflictError,
StigmemHTTPError,
} from "stigmem-ts";
try {
await client.getFact("unknown-id");
} catch (err) {
if (err instanceof StigmemNotFoundError) {
console.log("not found");
} else if (err instanceof StigmemAuthError) {
console.log("auth failed — check STIGMEM_API_KEY");
} else if (err instanceof StigmemHTTPError) {
console.log(`HTTP ${err.statusCode}: ${err.detail}`);
}
}
Custom fetch
import { StigmemClient } from "stigmem-ts";
import { fetch } from "undici";
const client = new StigmemClient({
url: "http://localhost:8000",
fetch: fetch as unknown as typeof globalThis.fetch,
});
Bundler smoke (esbuild)
cd sdks/stigmem-ts
pnpm smoke
Regenerating types from the OpenAPI spec
make sdk-ts-generate # just generation
make sdk-ts # full pipeline: generate → build → test → pack
The ergonomic types in src/types.ts (tagged-union FactValue, value constructors) are hand-authored on top of the generated layer to match the Stigmem wire format described in spec §2.1.
Advanced features
Content-addressed fact IDs (§25)
const factByUuid = await client.getFact("550e8400-e29b-41d4-a716-446655440000");
const factByCid = await client.getFact("sha256:a3f2b8c901d4...");
Asserting the same (entity, relation, value, source, scope) tuple twice returns the existing fact.
CID-based deduplication is automatic.
Time-travel queries (§24)
The experimental as_of parameter is available on the fact query and recall
endpoints only when stigmem-plugin-time-travel is registered and the
corresponding operator gate is enabled. Default installs fail closed. Use the
HTTP client directly:
const res = await fetch(
`${url}/v1/facts?entity=user:alice&as_of=2026-04-15T00:00:00Z`,
{ headers: { Authorization: `Bearer ${apiKey}` } },
);
const { facts } = await res.json();
See also
Tutorial: SDK Quickstart
Assert, recall, and subscribe in Python / TS / Go.
Recall guide
Hybrid recall, weight tuning, fast-path.
Python SDK Reference
Equivalent Python client.
Go SDK Reference
Experimental Go client.