SDKs · Node.js
Node.js Quickstart
Install the Node.js SDK, configure trusted scope, add one memory, and retrieve it. The example uses the published TypeScript surface.
Before you start
- Node.js 18 or newer.
- A MemorySync API key and project ID.
- A server-side TypeScript or JavaScript project.
- A stable opaque ID for the application user.
Install the package
terminal
npm install --save-exact memorysync-sdk@1.9.1
Set environment variables
PowerShell
$env:MEMORYSYNC_API_KEY="your-api-key"$env:MEMORYSYNC_PROJECT_ID="your-project-id"
Add and query memory
quickstart.ts
import { MemorySyncClient } from "memorysync-sdk";const client = new MemorySyncClient({apiKey: process.env.MEMORYSYNC_API_KEY!,baseUrl: "https://api.memorysync.io",projectId: process.env.MEMORYSYNC_PROJECT_ID!,endUserId: "usr_7f3a9c2e",});const result = await client.add({text: "The user prefers concise answers.",source: "profile",tags: ["preference"],});if ("status" in result && result.status === "skipped") {console.log("Skipped:", result.reason);} else {console.log("Memory ID:", result.id);}const response = await client.query({query: "How should I format the answer?",k: 5,});for (const memory of response.memories) {console.log(memory.id, memory.text);}
Understand the result
CREATED
A MemoryRecord is returned
Use its numeric ID for later targeted operations.
SKIPPED
No new record was created
Read the reason from the documented skipped response.
EMPTY
Query returned no candidates
An empty memories array is a successful result.
ERROR
A typed error was thrown
Correct permanent failures or apply the production recovery policy.
Keep the integration safe
- Resolve project and end-user scope from authenticated server state.
- Use the same scope for writes and reads.
- Store durable context, not secrets or one-request instructions.
- Delimit returned memory as untrusted context before sending it to a model.
Continue
Was this page helpful?