API quickstart
Use Python, TypeScript or curl to add a document, wait until the Memory has learned it, and search the result.
This walkthrough adds one note to a Memory, waits for learning to finish, and searches for what it said. Choose Python, TypeScript or curl; each follows the same sequence.
You need a Membase account, a Memory you can write to, and a working model with available turns. Create a Memory in the app quickstart and check AI Setup if a run cannot use a model.
1. Create a key
Section titled “1. Create a key”In the app, open Connect › Skills › Manage keys › Create key. Name the key, choose Read & write, and select exactly one Memory under Memory access for this walkthrough. Choose an expiry and create the key. Copy the token while it is shown; it is shown only once. The profile permission is optional and is not needed for this example.
One Memory matters: no request names a Memory, so a write lands in the key’s write target, the
one text Memory it reaches. A key that reaches several is refused with
422 write_target_required and nothing is written.
export MEMBASE_API_KEY="mbk_…"The key guide explains rotation and
revocation. The curl example and the SDKs call https://api.app.membase.com. Point either SDK
elsewhere with base_url / baseUrl or MEMBASE_BASE_URL.
2. Add, wait, then search
Section titled “2. Add, wait, then search”add returns 202 with a receipt ({id, kind, status, learning}) when material is accepted;
it does not promise that learning has finished. Read documents.get(id) until every entry of
its learning is succeeded before searching for the new content. The status is pending
while it waits for a run, processing while one reads it and failed when the run failed. The
examples stop on a failed learning run and have a polling limit, so they cannot wait
indefinitely.
Install with pip install unibaseio-membase (0.4.0 or newer), save this as quickstart.py,
and run python quickstart.py in the shell where you exported the key.
import timefrom membase import Membase
client = Membase()custom_id = "quickstart-ledger-v1"
added = client.add( "We chose Postgres for the Lumen ledger.", title="Lumen decision", custom_id=custom_id,)document_id = added["id"]if not document_id: raise RuntimeError("Accepted, but the document id is pending. See API troubleshooting.")
for _ in range(60): learning = client.documents.get(document_id).get("learning") or [] statuses = {entry["status"] for entry in learning} if learning and statuses == {"succeeded"}: break if "failed" in statuses: raise RuntimeError("Learning failed. Open the Memory's Report in the app.") time.sleep(5)else: raise TimeoutError("Still unread. Check the Memory's Report and model settings before retrying.")
found = client.search("Which database did we choose for the Lumen ledger?")errors = [p for p in found["partial"] if p["reason"] == "error"]if errors: raise RuntimeError(f"Some Memories could not answer: {errors}")for hit in found["items"]: print(hit["memory_name"], "·", hit["content"])if not found["items"]: print("No matching passages. Inspect the Memory and its latest Report in the app.")Install with npm install @unibaseio/membase (0.4.0 or newer) and run this in a server-side
TypeScript project on Node 18 or newer. The package is ESM-only and the example uses top-level await, so the
project must be an ES module: "type": "module" in package.json, or save the file as
quickstart.mts. Keep the key on the server, outside browser bundles.
import { Membase } from "@unibaseio/membase";
const client = new Membase();const customId = "quickstart-ledger-v1";const added = await client.add({ content: "We chose Postgres for the Lumen ledger.", title: "Lumen decision", customId,});const documentId = added.id;if (!documentId) throw new Error("Accepted; document id pending. See API troubleshooting.");let learned = false;
for (let attempt = 0; attempt < 60; attempt++) { const { learning = [] } = await client.documents.get(documentId); if (learning.length && learning.every(entry => entry.status === "succeeded")) { learned = true; break; } if (learning.some(entry => entry.status === "failed")) { throw new Error("Learning failed. Open the Memory's Report in the app."); } await new Promise(resolve => setTimeout(resolve, 5000));}if (!learned) throw new Error("Still unread. Check the Memory's Report and model settings.");
const found = await client.search({ q: "Which database did we choose for the Lumen ledger?" });const errors = found.partial.filter(p => p.reason === "error");if (errors.length) throw new Error(JSON.stringify(errors));for (const hit of found.items) console.log(hit.memory_name, "·", hit.content);if (!found.items.length) console.log("No matching passages. Inspect the Memory and its Report.");Requires Bash, curl and jq. Save this as quickstart.sh and run bash quickstart.sh in the
shell where you exported the key. A failed HTTP request stops the script.
set -euo pipefailBASE=https://api.app.membase.comAUTH="Authorization: Bearer $MEMBASE_API_KEY"custom_id=quickstart-ledger-v1payload=$(jq -n --arg id "$custom_id" \ '{content: "We chose Postgres for the Lumen ledger.", title: "Lumen decision", custom_id: $id}')added=$(curl --fail-with-body -sS "$BASE/v1/documents/add" -H "$AUTH" \ -H 'Content-Type: application/json' -d "$payload")document_id=$(jq -r '.id // empty' <<< "$added")if [ -z "$document_id" ]; then echo "Accepted, but document id pending. See API troubleshooting." >&2 exit 1filearned=false
for ((attempt=0; attempt<60; attempt++)); do document=$(curl --fail-with-body -sS "$BASE/v1/documents/$document_id" -H "$AUTH") if jq -e '(.learning | length > 0) and all(.learning[]; .status == "succeeded")' <<< "$document" >/dev/null; then learned=true break fi if jq -e 'any(.learning[]; .status == "failed")' <<< "$document" >/dev/null; then echo "Learning failed. Open the Memory's Report in the app." >&2 exit 1 fi sleep 5doneif [ "$learned" != true ]; then echo "Still unread. Check the Memory's Report and model settings." >&2 exit 1fipayload=$(jq -n '{q: "Which database did we choose for the Lumen ledger?"}')found=$(curl --fail-with-body -sS "$BASE/v1/search" -H "$AUTH" \ -H 'Content-Type: application/json' -d "$payload")jq -e 'if any(.partial[]; .reason == "error") then error("A Memory could not answer; inspect partial") else . end' <<< "$found"3. Check the result
Section titled “3. Check the result”Expect a passage naming Postgres and the Memory it came from. The exact wording can vary.
An empty items list is not enough to diagnose a problem: inspect partial first (a Memory
named there with reason: "error" could not answer), then the Memory’s learned content and
latest Report.
Re-running this example with the same custom_id does not add a second copy: the receipt
names the document already there. If learning was
deferred or failed, fix the cause and run Update now in the app; resending the same document
is not a way to force another learning run. See API troubleshooting.
Next steps
Section titled “Next steps”- Memory operations: facts, profile facts, documents, search and deletion.
- SDKs: configuration, errors and method signatures.
- Authentication: permissions, reach and expiry.
- Integrations: put these calls behind your model or framework.