File size: 7,930 Bytes
0ae3f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
---
title: Node SDK Quickstart
description: "Store and search Mem0 memories from a TypeScript or JavaScript app in minutes."
icon: "js"
---

Spin up Mem0 with the Node SDK in just a few steps. You’ll install the package, initialize the client, add a memory, and confirm retrieval with a single search.

## Prerequisites

- Node.js 18 or higher
- (Optional) OpenAI API key stored in your environment when you want to customize providers

## Install and run your first memory

<Steps>
<Step title="Install the SDK">
```bash
npm install mem0ai
```
</Step>

<Step title="Initialize the client">
```ts
import { Memory } from "mem0ai/oss";

const memory = new Memory();
```
</Step>

<Step title="Add a memory">
```ts
const messages = [
  { role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" },
  { role: "assistant", content: "How about thriller movies? They can be quite engaging." },
  { role: "user", content: "I'm not a big fan of thriller movies but I love sci-fi movies." },
  { role: "assistant", content: "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future." }
];

await memory.add(messages, { userId: "alice", metadata: { category: "movie_recommendations" } });
```
</Step>

<Step title="Search memories">
```ts
const results = await memory.search("What do you know about me?", { userId: "alice" });
console.log(results);
```

**Output**
```json
{
  "results": [
    {
      "id": "892db2ae-06d9-49e5-8b3e-585ef9b85b8e",
      "memory": "User is planning to watch a movie tonight.",
      "score": 0.38920719231944799,
      "metadata": {
        "category": "movie_recommendations"
      },
      "userId": "alice"
    }
  ]
}
```
</Step>
</Steps>

<Note>
By default the Node SDK uses local-friendly settings (OpenAI `gpt-4.1-nano-2025-04-14`, `text-embedding-3-small`, in-memory vector store, and SQLite history). Swap components by passing a config as shown below.
</Note>

## Configure for production

```ts
import { Memory } from "mem0ai/oss";

const memory = new Memory({
  version: "v1.1",
  embedder: {
    provider: "openai",
    config: {
      apiKey: process.env.OPENAI_API_KEY || "",
      model: "text-embedding-3-small"
    }
  },
  vectorStore: {
    provider: "memory",
    config: {
      collectionName: "memories",
      dimension: 1536
    }
  },
  llm: {
    provider: "openai",
    config: {
      apiKey: process.env.OPENAI_API_KEY || "",
      model: "gpt-4-turbo-preview"
    }
  },
  historyDbPath: "memory.db"
});
```

## Manage memories (optional)

<CodeGroup>
```ts Get all memories
const allMemories = await memory.getAll({ userId: "alice" });
console.log(allMemories);
```

```ts Get one memory
const singleMemory = await memory.get("892db2ae-06d9-49e5-8b3e-585ef9b85b8e");
console.log(singleMemory);
```

```ts Search memories
const result = await memory.search("What do you know about me?", { userId: "alice" });
console.log(result);
```

```ts Update a memory
const updateResult = await memory.update(
  "892db2ae-06d9-49e5-8b3e-585ef9b85b8e",
  "I love India, it is my favorite country."
);
console.log(updateResult);
```
</CodeGroup>

```ts
// Audit history
const history = await memory.history("892db2ae-06d9-49e5-8b3e-585ef9b85b8e");
console.log(history);

// Delete specific or scoped memories
await memory.delete("892db2ae-06d9-49e5-8b3e-585ef9b85b8e");
await memory.deleteAll({ userId: "alice" });

// Reset everything
await memory.reset();
```

## Use a custom history store

The Node SDK supports Supabase (or other providers) when you need serverless-friendly history storage.

<CodeGroup>
```ts Supabase provider
import { Memory } from "mem0ai/oss";

const memory = new Memory({
  historyStore: {
    provider: "supabase",
    config: {
      supabaseUrl: process.env.SUPABASE_URL || "",
      supabaseKey: process.env.SUPABASE_KEY || "",
      tableName: "memory_history"
    }
  }
});
```

```ts Disable history
import { Memory } from "mem0ai/oss";

const memory = new Memory({
  disableHistory: true
});
```
</CodeGroup>

Create the Supabase table with:

```sql
create table memory_history (
  id text primary key,
  memory_id text not null,
  previous_value text,
  new_value text,
  action text not null,
  created_at timestamp with time zone default timezone('utc', now()),
  updated_at timestamp with time zone,
  is_deleted integer default 0
);
```

## Configuration parameters

Mem0 offers granular configuration across vector stores, LLMs, embedders, and history stores.

<AccordionGroup>
  <Accordion title="Vector store">
| Parameter | Description | Default |
| --- | --- | --- |
| `provider` | Vector store provider (e.g., `"memory"`) | `"memory"` |
| `host` | Host address | `"localhost"` |
| `port` | Port number | `undefined` |
  </Accordion>
  <Accordion title="LLM">
| Parameter | Description | Provider |
| --- | --- | --- |
| `provider` | LLM provider (e.g., `"openai"`, `"anthropic"`) | All |
| `model` | Model to use | All |
| `temperature` | Temperature value | All |
| `apiKey` | API key | All |
| `maxTokens` | Max tokens to generate | All |
| `topP` | Probability threshold | All |
| `topK` | Token count to keep | All |
| `openaiBaseUrl` | Base URL override | OpenAI |
  </Accordion>
  <Accordion title="Graph store">
| Parameter | Description | Default |
| --- | --- | --- |
| `provider` | Graph store provider (e.g., `"neo4j"`) | `"neo4j"` |
| `url` | Connection URL | `process.env.NEO4J_URL` |
| `username` | Username | `process.env.NEO4J_USERNAME` |
| `password` | Password | `process.env.NEO4J_PASSWORD` |
  </Accordion>
  <Accordion title="Embedder">
| Parameter | Description | Default |
| --- | --- | --- |
| `provider` | Embedding provider | `"openai"` |
| `model` | Embedding model | `"text-embedding-3-small"` |
| `apiKey` | API key | `undefined` |
  </Accordion>
  <Accordion title="General">
| Parameter | Description | Default |
| --- | --- | --- |
| `historyDbPath` | Path to history database | `"{mem0_dir}/history.db"` |
| `version` | API version | `"v1.0"` |
| `customPrompt` | Custom processing prompt | `undefined` |
  </Accordion>
  <Accordion title="History store">
| Parameter | Description | Default |
| --- | --- | --- |
| `provider` | History provider | `"sqlite"` |
| `config` | Provider configuration | `undefined` |
| `disableHistory` | Disable history store | `false` |
  </Accordion>
  <Accordion title="Complete config example">
```ts
const config = {
  version: "v1.1",
  embedder: {
    provider: "openai",
    config: {
      apiKey: process.env.OPENAI_API_KEY || "",
      model: "text-embedding-3-small"
    }
  },
  vectorStore: {
    provider: "memory",
    config: {
      collectionName: "memories",
      dimension: 1536
    }
  },
  llm: {
    provider: "openai",
    config: {
      apiKey: process.env.OPENAI_API_KEY || "",
      model: "gpt-4-turbo-preview"
    }
  },
  historyStore: {
    provider: "supabase",
    config: {
      supabaseUrl: process.env.SUPABASE_URL || "",
      supabaseKey: process.env.SUPABASE_KEY || "",
      tableName: "memories"
    }
  },
  disableHistory: false,
  customPrompt: "I'm a virtual assistant. I'm here to help you with your queries."
};
```
  </Accordion>
</AccordionGroup>

## What's next?

<CardGroup cols={3}>
  <Card title="Explore Memory Operations" icon="database" href="/core-concepts/memory-operations/add">
    Review CRUD patterns, filters, and advanced retrieval across the OSS stack.
  </Card>
  <Card title="Customize Configuration" icon="sliders" href="/open-source/configuration">
    Swap in your preferred LLM, vector store, and history provider for production use.
  </Card>
  <Card title="Automate Node Workflows" icon="plug" href="/cookbooks/integrations/openai-tool-calls">
    See a full Node-based workflow that layers Mem0 memories onto tool-calling agents.
  </Card>
</CardGroup>

If you have any questions, please feel free to reach out:

<Snippet file="get-help.mdx" />