aboutsummaryrefslogtreecommitdiff
path: root/apps/docs/memory-graph/quickstart.mdx
blob: d05a09250cca79b225be2e1cee57e26b70c8445c (plain) (blame)
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
---
title: 'Quick Start'
description: 'Get Memory Graph running in 2 minutes'
---

## Basic Setup

Here's a minimal example to get the graph running:

```tsx
'use client'; // For Next.js App Router

import { MemoryGraph } from '@supermemory/memory-graph';
import type { DocumentWithMemories } from '@supermemory/memory-graph';
import { useEffect, useState } from 'react';

export default function GraphPage() {
  const [documents, setDocuments] = useState<DocumentWithMemories[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<Error | null>(null);

  useEffect(() => {
    fetch('/api/graph')
      .then(res => res.json())
      .then(data => {
        setDocuments(data.documents);
        setIsLoading(false);
      })
      .catch(err => {
        setError(err);
        setIsLoading(false);
      });
  }, []);

  return (
    <div style={{ height: '100vh' }}>
      <MemoryGraph
        documents={documents}
        isLoading={isLoading}
        error={error}
        variant="console"
      />
    </div>
  );
}
```

## Backend API Route

Create an API route to fetch documents from Supermemory:

<CodeGroup>

```typescript Next.js App Router
// app/api/graph/route.ts
import { NextResponse } from 'next/server';

export async function GET() {
  const response = await fetch('https://api.supermemory.ai/v3/documents/documents', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${process.env.SUPERMEMORY_API_KEY}`,
    },
    body: JSON.stringify({
      page: 1,
      limit: 500,
      sort: 'createdAt',
      order: 'desc',
    }),
  });

  const data = await response.json();
  return NextResponse.json(data);
}
```

```typescript Next.js Pages Router
// pages/api/graph.ts
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const response = await fetch('https://api.supermemory.ai/v3/documents/documents', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${process.env.SUPERMEMORY_API_KEY}`,
    },
    body: JSON.stringify({
      page: 1,
      limit: 500,
      sort: 'createdAt',
      order: 'desc',
    }),
  });

  const data = await response.json();
  res.json(data);
}
```

```javascript Express
// routes/graph.js
app.get('/api/graph', async (req, res) => {
  const response = await fetch('https://api.supermemory.ai/v3/documents/documents', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${process.env.SUPERMEMORY_API_KEY}`,
    },
    body: JSON.stringify({
      page: 1,
      limit: 500,
      sort: 'createdAt',
      order: 'desc',
    }),
  });

  const data = await response.json();
  res.json(data);
});
```

</CodeGroup>

<Warning>
  Never expose your Supermemory API key to the client. Always fetch data through your backend.
</Warning>

## Environment Variables

Add your API key to `.env.local`:

```bash
SUPERMEMORY_API_KEY=your_api_key_here
```

Get your API key from the [Supermemory dashboard](https://console.supermemory.ai).

## Common Customizations

### Embedded Mode

For a widget-style view, use the consumer variant:

```tsx
<MemoryGraph
  documents={documents}
  isLoading={isLoading}
  variant="consumer"
/>
```

### CSS Import

The component includes bundled styles. You don't need to import CSS separately. Styles are automatically injected when the component mounts.

If you want explicit control, you can import the stylesheet:

```typescript
import '@supermemory/memory-graph/styles.css';
```

<Note>
  The automatic CSS injection works for most setups. Only use the explicit import if you need custom control over style loading order.
</Note>


### Custom Empty State

Show custom content when no documents exist:

```tsx
<MemoryGraph
  documents={documents}
  isLoading={isLoading}
>
  <div style={{ textAlign: 'center', padding: '2rem' }}>
    <h2>No memories yet</h2>
    <p>Add content to see your knowledge graph</p>
  </div>
</MemoryGraph>
```

### Hide Space Selector

```tsx
<MemoryGraph
  documents={documents}
  isLoading={isLoading}
  showSpacesSelector={false}
/>
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Examples" icon="code" href="/memory-graph/examples">
    See more usage examples
  </Card>
  <Card title="API Reference" icon="book" href="/memory-graph/api-reference">
    Full API documentation
  </Card>
</CardGroup>