aboutsummaryrefslogtreecommitdiff
path: root/apps/memory-graph-playground/src/app/api/graph/route.ts
blob: c722c625b90aac253ec24b530e30390326dec2c1 (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
import { NextResponse } from "next/server"

export async function POST(request: Request) {
	try {
		const body = await request.json()
		const {
			apiKey,
			page = 1,
			limit = 500,
			sort = "createdAt",
			order = "desc",
		} = body

		if (!apiKey) {
			return NextResponse.json(
				{ error: "API key is required" },
				{ status: 400 },
			)
		}

		const response = await fetch(
			"https://api.supermemory.ai/v3/documents/documents",
			{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					Authorization: `Bearer ${apiKey}`,
				},
				body: JSON.stringify({
					page,
					limit,
					sort,
					order,
				}),
			},
		)

		if (!response.ok) {
			const errorData = await response.json().catch(() => ({}))
			return NextResponse.json(
				{ error: errorData.message || `API error: ${response.status}` },
				{ status: response.status },
			)
		}

		const data = await response.json()
		return NextResponse.json(data)
	} catch (error) {
		console.error("Graph API error:", error)
		return NextResponse.json(
			{ error: "Failed to fetch documents" },
			{ status: 500 },
		)
	}
}