javascript
// memory-layer.js — Drop-in client for any Node.js or browser project
const BASE_URL = 'https://memory-layer-api.onrender.com';
export class MemoryLayerClient {
constructor(apiKey, userId) {
this.headers = {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
};
this.userId = userId;
}
async remember(content, metadata = {}) {
const res = await fetch(`${BASE_URL}/v1/memory/`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify({
content,
external_user_id: this.userId,
metadata,
}),
});
return res.json();
}
async recall(query, limit = 5, threshold = 0.3) {
const res = await fetch(`${BASE_URL}/v1/memory/search`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify({
query,
external_user_id: this.userId,
limit,
similarity_threshold: threshold,
}),
});
return res.json();
}
async chat(message, topK = 5) {
const res = await fetch(`${BASE_URL}/v1/chat/`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify({
message,
external_user_id: this.userId,
top_k: topK,
}),
});
return res.json();
}
}
// Usage
const client = new MemoryLayerClient('YOUR_API_KEY', 'user-123');
await client.remember('I prefer React over Vue for frontend work');
const { reply } = await client.chat('What frontend framework do I use?');
console.log(reply);
// "You prefer React for frontend work."