Skip to content

Your first event with a client

No server of your own needed: the hosted service runs memento for anyone with a key. The programs under clients/ talk to it the way any integration would, with the language’s own HTTP client and a WebSocket, no SDK.

  1. Register on the service site and open the link in the mail it sends: until the address is verified, the site creates no keys. The account comes with a tenant of its own; nobody else’s events are visible to it, and yours to nobody.

  2. Under Keys, New API key: a name, the scopes read (list and stream) and write (push) — the program needs both — and optionally an expiry date and a daily limit.

    • The key is shown once, in the dialog that creates it, with the two lines the programs read (MEMENTO_URL, MEMENTO_API_KEY) and a button that copies them. The server keeps only its hash.
    • A key may store 100 000 events per UTC day unless you set a lower limit. At 80 % you get a mail; past the limit POST /events answers 429 quota_exceeded until midnight UTC. The Keys page shows each key’s count for today.
  3. Get the JavaScript program and run it with the key (Node 22 or newer, nothing to install):

    Terminal
    curl -O https://service.memento-service.org/clients/javascript.zip
    unzip javascript.zip && cd javascript
    export MEMENTO_API_KEY=mk_… # read + write; MEMENTO_URL defaults to the same service
    node memento.mjs

    MEMENTO_URL defaults to the service’s API, https://service.memento-service.org/memento; point it at your own server (Docker: http://localhost:3000/memento) and the program does not notice.

  4. Read what it printed:

    What it prints
    Pushed fb0e7a36-e137-413e-afea-535471a3effc
    User digital logged in from 127.0.0.1.
    Latest events of user/digital:
    2026-11-02T00:31:42.133Z USER_LOGIN User digital logged in from 127.0.0.1.
    2026-11-02T00:31:34.681Z USER_LOGIN User digital logged in from 127.0.0.2.
    2026-11-02T00:31:13.174Z USER_LOGIN User digital logged in from 127.0.0.1.
    2026-11-02T00:21:48.199Z USER_LOGIN User digital logged in from 127.0.0.1.
    Tailing user/digital for 10 s …
    User digital logged in from 127.0.0.2.
    Done.
    • Push: one USER_LOGIN for you (your OS user is the instance_id and the actor), with payload: {ip, client}. memento answers with the stored event; the program prints its id and the sentence memento rendered from it.
    • Query: the last ten events of user/<you> through the Filter DSL (POST /events/query), newest first: every run adds one.
    • Tail: ten seconds of GET /ws?instance_type=user&instance_id=<you>. Anything stored for you in that time arrives as it is stored; the line above is a push from another terminal.

The whole program, as the zip has it:

javascript/memento.mjs
// memento.mjs — a complete JSMemento client in plain JavaScript (Node ≥ 22, no dependencies).
//
// It does three things, in this order:
// 1. push one USER_LOGIN event for you (the OS user) and print its id and rendered line;
// 2. query the last 10 events of user/<you>;
// 3. tail the live stream of user/<you> for 10 seconds.
//
// Environment:
// MEMENTO_API_KEY your API key with the read and write scopes (required)
// MEMENTO_URL the API base, default https://service.memento-service.org/memento
//
// Run:
// MEMENTO_API_KEY=mk_… node memento.mjs
import { userInfo } from "node:os";
const DEFAULT_URL = "https://service.memento-service.org/memento";
const BASE_URL = (process.env.MEMENTO_URL || DEFAULT_URL).replace(/\/+$/, "");
const API_KEY = process.env.MEMENTO_API_KEY;
const TAIL_SECONDS = 10;
class ApiError extends Error {}
/** POST a JSON body to the API and return the envelope's `data`; throws ApiError. */
async function post(path, body) {
let response;
try {
response = await fetch(`${BASE_URL}${path}`, {
method: "POST",
headers: { authorization: `Bearer ${API_KEY}`, "content-type": "application/json" },
body: JSON.stringify(body),
});
} catch (error) {
throw new ApiError(`Cannot reach ${BASE_URL}: ${error.cause?.message ?? error.message}`);
}
const envelope = await response.json().catch(() => ({}));
if (!response.ok) {
const message = envelope.error?.message ?? response.statusText;
throw new ApiError(`HTTP ${response.status}: ${message}`);
}
return envelope.data;
}
/** 1. Push one event; returns the stored event. */
async function push(user) {
const event = await post("/events", {
event_type: "USER_LOGIN",
instance_type: "user",
instance_id: user,
actor_id: user,
payload: { ip: "127.0.0.1", client: "javascript" },
});
console.log(`Pushed ${event.id}`);
console.log(` ${event.rendered}`);
return event;
}
/** 2. The last 10 events of user/<user>, newest first. */
async function query(user) {
const events = await post("/events/query", {
filter: {
and: [
{ field: "instance_type", op: "eq", value: "user" },
{ field: "instance_id", op: "eq", value: user },
],
},
limit: 10,
});
console.log(`\nLatest events of user/${user}:`);
for (const event of events) {
console.log(` ${event.occurred_at} ${event.event_type} ${event.rendered ?? ""}`);
}
}
/** 3. Print every new event of user/<user> for TAIL_SECONDS seconds. */
function tail(user) {
const params = new URLSearchParams({ instance_type: "user", instance_id: user, locale: "en" });
const url = `${BASE_URL.replace(/^http/, "ws")}/ws?${params}`;
return new Promise((resolve, reject) => {
// The key travels as a subprotocol; the server answers with `memento.v1`.
const socket = new WebSocket(url, ["memento.v1", `bearer.${API_KEY}`]);
let finished = false;
const timer = setTimeout(() => {
finished = true;
socket.close(1000);
console.log("Done.");
resolve();
}, TAIL_SECONDS * 1000);
socket.addEventListener("message", (message) => {
const frame = JSON.parse(message.data);
if (frame.kind === "memento.welcome") {
console.log(`\nTailing user/${user} for ${TAIL_SECONDS} s …`);
} else if (frame.kind === "memento.event.new") {
console.log(` ${frame.payload.rendered ?? frame.payload.event_type}`);
}
// anything else (memento.pong, …) is ignored
});
socket.addEventListener("error", () => {
if (finished) return;
clearTimeout(timer);
reject(new ApiError(`Tail: cannot open the stream at ${url.split("?")[0]}`));
});
socket.addEventListener("close", (event) => {
if (finished) return;
clearTimeout(timer);
reject(new ApiError(`Tail: the server closed the stream (${event.code} ${event.reason})`));
});
});
}
/** The OS user name: the instance and actor of the event. */
function osUser() {
try {
return userInfo().username;
} catch {
return process.env.USER || process.env.USERNAME || "anonymous";
}
}
async function main() {
if (!API_KEY) {
console.error("Set MEMENTO_API_KEY (create one at https://service.memento-service.org/keys)");
process.exit(2);
}
const user = osUser();
try {
await push(user);
await query(user);
await tail(user);
} catch (error) {
if (!(error instanceof ApiError)) throw error;
console.error(error.message);
process.exit(1);
}
}
await main();

The same three steps with the same event, so the programs read side by side. Each is a zip on the service site’s Clients page, with its README.

language files run tail
TypeScript memento.ts, package.json npx tsx memento.ts yes
Python memento.py python3 memento.py with the websockets package
PHP memento.php php memento.php no
Java Memento.java java Memento.java (Java 21) yes
Rust Cargo.toml, src/main.rs cargo run yes
Go main.go, go.mod go run . yes
C++ memento.cpp, CMakeLists.txt cmake -B build && cmake --build build && ./build/memento no

Without MEMENTO_API_KEY every program says where to create one and exits with code 2; an HTTP error prints the status and the server’s message (HTTP 401: invalid or expired API key) and exits with 1. All eight run in CI against a real server on every change of clients/.

v1.1.0released built 663143e