Tenants and API keys
One memento holds the events of many tenants — companies, applications, the accounts of the service site — and none of them sees another’s. What decides the tenant of a request is its API key, and nothing else.
Tenants
Section titled “Tenants”A tenant is a row of memento_tenant: a slug (acme, lowercase words joined by dashes,
2–63 characters), a display name and is_active. The keys of an inactive tenant fail
authentication. A tenant comes from the CLI (memento create-tenant acme --name "Acme Inc") or
is made for each person who registers on the service site (slug t- and eight random
characters, see Accounts).
Isolation is enforced in the service layer, not in each route:
- the key is resolved before the handler runs, and the request carries its tenant in an
AsyncLocalStoragecontext; - every service reads the tenant from that context (
requireTenant()), never from the body or the query string, and every query hasWHERE tenant_id = <the context's tenant>; - a service called outside a tenant context throws
unauthorized, so a new route cannot run unscoped by accident.
The same holds for the stream: each tenant’s events are published for that tenant alone (with Redis, on their own channel), and a WebSocket client only receives its own tenant’s (Streaming).
API keys
Section titled “API keys”A key is sent as Authorization: Bearer mk_… or X-Api-Key: mk_… (on the WebSocket, as the
subprotocol bearer.mk_…). Its format is mk_<8 × [a-z0-9]>_<32 × [A-Za-z0-9]>: the eight
characters after mk_ are the prefix, stored in clear to find the row; the rest is about
190 bits of secret.
- Shown once. memento stores only the SHA-256 of the full key and compares it in constant time. The plaintext exists in the one response (or CLI output) that created it.
- One message for every failure. A missing, unknown, revoked, expired, paused or deleted
key, or one of an inactive tenant, is
401 unauthorizedwith the same message; a key without the route’s scope is403 forbidden. last_used_atis updated on every authenticated request.
| scope | allows |
|---|---|
read |
list and query events, entity history, /describe, read chats, the stream, UDC reads |
write |
push events, create chats and send messages |
admin |
everything; reads chats without naming a participant |
admin implies the other two. A person on the service site can give a key read and write
only; admin keys come from the CLI.
Where keys come from
Section titled “Where keys come from”- The CLI, against the database:
memento create-key --tenant acme --scopes read,write --name backend(optionally--expires-in-days N) prints the key once;memento revoke-key <prefix>revokes it for good. The CLI writes no events about its keys. - The service site, for a signed-in user: Keys creates
one with a name, scopes, an optional expiry and an optional daily limit, and shows it once
with the two lines a program reads (
MEMENTO_URL,MEMENTO_API_KEY). The same page pauses a key (it fails authentication like a revoked one), resumes it, changes its name and limit, and deletes it — a soft delete: the row stays because events point at it, but the key is hidden from every list and never authenticates again.
A tenant may hold max_keys live keys (not deleted, not revoked), else
MEMENTO_DEFAULT_MAX_KEYS (10); one more is 409 limit_reached. Every change made on the
site is itself an event in the key’s tenant — APIKEY_CREATED, APIKEY_DISABLED,
APIKEY_ENABLED, APIKEY_DELETED, APIKEY_LIMIT_CHANGED on the instance api_key/<prefix>,
with the user’s email as the actor.
Every event stored with a key records it in api_key_id. The UDC events collection shows it
as api_key, the prefix, which is what a person recognizes and can filter on.
Daily limits
Section titled “Daily limits”A key of a self-service tenant (one that came from a registration on the service site) may
store a number of events per UTC day: its own daily_limit, else MEMENTO_DEFAULT_DAILY_LIMIT
(100 000). That holds on every server on the database, with accounts on or off, so the key counts
the same whichever one receives its events. A server with accounts on limits every other key
too, CLI keys included. Every route that stores an event with a limited key checks it first.
- At 80 % the event is stored and, the first time that day, the key’s creator (or the
tenant’s owner) gets a
quota_warningmail. - Past 100 % nothing is stored: the push answers
429 quota_exceededwith{limit, used, resets_at}until midnight UTC, and the first refusal of the day sends one more mail.
A producer pushing one event a second writes 86 400 a day, so heavier ones need a higher limit
(MEMENTO_MAX_DAILY_LIMIT caps what a user may set). Today’s count lives in Redis
(memento:quota:<key>:<day>), shared by every process; without Redis it is summed from the
usage table — every process on one database uses the same store. A deployment that never had
accounts has no self-service tenants, so nothing there is limited or metered.
An error looks the same for every route, a 422 here:
{ "ok": false, "error": { "code": "validation_error", "message": "invalid event", "details": [ { "origin": "string", "code": "invalid_format", "format": "regex", "pattern": "/^[A-Z][A-Z0-9_]*$/", "path": ["event_type"], "message": "event_type must be SCREAMING_SNAKE_CASE (e.g. USER_LOGIN)" } ] }, "request_id": "048b0b5e…"}The usage meter
Section titled “The usage meter”Per limited key (above) and per UTC hour the server counts requests and bytes_in (every request),
events_written, rows_read (rows returned by the event lists, the Filter DSL query and UDC
list) and stream_seconds (open /ws connections). It keeps them in memory and writes them
to memento_apikey_usage every 5 s and on shutdown. Requests made with the session cookie are
not metered.
The service site draws these counters per key, next to the key’s activity per minute and its events (the service site).
- The routes, bodies and answers: HTTP API › Authentication and Daily limits and usage.
- Registration, sessions and the service site: Accounts.
v1.1.0released built 663143e