Verified Examples
Follow one server-side path from configuration to a safe retrieval result. Switch languages inside each step without losing your place.
Complete the four-step path
- 1Configure a trusted server client
Load the API key from a server-side environment variable. Choose a project and a stable opaque ID for the end user your server represents.
import osfrom memorysync import MemorySyncClientclient = MemorySyncClient(api_key=os.environ["MEMORYSYNC_API_KEY"],base_url="https://api.memorysync.io",project_id=os.environ["MEMORYSYNC_PROJECT_ID"],end_user_id="user-123",) - 2Add one useful memory
Write a confirmed preference with a source, stable tag, and small non-secret metadata object. Branch on created versus skipped.
import osfrom memorysync import MemorySyncClientclient = MemorySyncClient(api_key=os.environ["MEMORYSYNC_API_KEY"],base_url="https://api.memorysync.io",project_id=os.environ["MEMORYSYNC_PROJECT_ID"],end_user_id="user-123",)result = client.add("The user prefers concise answers.",source="profile",tags=["preference"],importance=0.7,metadata={"setting_id": "response-style"},)if getattr(result, "status", None) == "skipped":print("Not stored:", result.reason)else:print("Created:", result.id) - 3Retrieve it for a later task
Ask for the context needed by the task and use the same project and end-user scope. This example narrows candidates with the verified tag shape.
import osfrom memorysync import MemorySyncClientclient = MemorySyncClient(api_key=os.environ["MEMORYSYNC_API_KEY"],base_url="https://api.memorysync.io",project_id=os.environ["MEMORYSYNC_PROJECT_ID"],end_user_id="user-123",)result = client.query("How should I format the answer?",k=5,filters={"tags": ["preference"]},)for memory in result.memories:print(memory.text) - 4Authorize and use the result
An empty array is valid. For returned candidates, apply application authorization, select useful records, and delimit them as untrusted context before sending anything to a model.
Result handling Memory found
Use only the fields your task needs.
- Check application access.
- Select relevant candidates.
- Keep memory separate from instructions.
Nothing found
Continue safely without durable context.
- Use a product fallback.
- Avoid inventing remembered facts.
- Do not retry an unchanged query in a loop.
Expected checkpoints
No API key or end-user selection is trusted directly from browser input.
Your code does not assume every successful request creates an ID.
Your code accepts both candidate and empty result sets.
Your application approves and delimits selected records.
Before moving beyond the example
- Replace
user-123with a stable opaque ID selected after your own authorization check. - Choose tags and metadata names from a small application-owned vocabulary.
- Add bounded handling for rate limits, server errors, timeouts, and ambiguous write outcomes.
- Connect forget operations to account deletion, consent, and support workflows.
- Evaluate retrieval with representative questions and expected facts from your own product.