Identity walkthrough
How an API key carries identity — it binds a (namespace, space) scope at creation, the server derives everything from it, and the connected client reports it back.
- Access to a Brain deployment. There is no public hosted endpoint yet, so that means one you run yourself.
- An API key bound to a
(namespace, space), minted via the admin API. - Familiarity with Identity by key.
A client that authenticates with a key, prints the (namespace, space, permissions) the server derived from it, and — for backends serving many tenants — runs an operation as a different identity with act_as.
The model in one paragraph
An API key is durably bound at creation to a namespace (the company / tenant — the top-level isolation boundary), a space (the app / persona within the tenant), and a set of permission flags. The server resolves (namespace, space, permissions) from the key at the handshake and scopes all reads and writes to it. Clients never send a scope on the wire — there is no namespace or space field to set, and a key can only ever touch data in its own namespace. This is identity-by-key: it removes the whole class of bugs where client and server disagree about who a request is for.
Step-by-step
The key takes effect the same way on both surfaces — it is the whole credential and clients never send a scope alongside it. Over the wire protocol it is sent after the handshake; BrainClient.connect takes it once and authenticates the connection.
import { BrainClient } from "@brain-db/sdk";
const client = await BrainClient.connect("127.0.0.1", 9090, {
auth: { kind: "token", token: new TextEncoder().encode(process.env.BRAIN_API_KEY!) },
});import os
from brain_db_sdk import Auth, BrainClient
client = BrainClient.connect(
"127.0.0.1", 9090, Auth.token(os.environ["BRAIN_API_KEY"].encode())
)use brain_db_sdk::{Auth, BrainClient};
let client = BrainClient::connect(addr, Auth::Token(api_key.into_bytes())).await?;Over the HTTP edge the same key rides one of two interchangeable headers — the BrainHttpClient sets it for you, or send it yourself with curl:
import { BrainHttpClient } from "@brain-db/sdk";
const brain = new BrainHttpClient({
apiKey: process.env.BRAIN_API_KEY!, // a `brain_…` key
baseUrl: process.env.BRAIN_BASE_URL, // unset → http://127.0.0.1:8080
});# Authorization: Bearer … and X-API-Key: … are equivalent — pick one.
curl "$BRAIN_BASE_URL/v1/whoami" \
-H "Authorization: Bearer $BRAIN_API_KEY"Confirm exactly what the server derived from the key, and fail fast if it resolves to the wrong namespace. On the wire client it comes straight off the handshake, so reading it is free; over HTTP, GET /v1/whoami returns the same namespace, space_id, and permissions.
// Wire client — off the handshake.
console.log(client.namespace, client.spaceId);
console.log(client.connection.permissions, client.connection.boundShardId);
// HTTP client — a whoami round-trip.
const me = await brain.whoami();
console.log(me.namespace, me.spaceId, me.permissions);# Wire client — off the handshake.
print(client.namespace, client.space_id)
print(client.connection.permissions, client.connection.bound_shard_id)
# HTTP client — a whoami round-trip.
me = brain.whoami()
print(me.namespace, me.space_id, me.permissions)// Wire client — off the handshake.
println!("{:?} {:?}", client.namespace(), client.space_id());
println!("{:?}", client.connection().permissions);
// HTTP client — a whoami round-trip.
let me = brain.whoami().await?;
println!("{} {} {:?}", me.namespace, me.space_id, me.permissions);curl "$BRAIN_BASE_URL/v1/whoami" \
-H "Authorization: Bearer $BRAIN_API_KEY"
# → { "namespace": "acme", "space_id": "0a1b2c3d-…", "permissions": { … } }A key without the flag a verb requires is rejected with an authorization error (403 forbidden over HTTP) before the handler runs — e.g. a read-only key calling encode.
Key lifecycle is an operator concern handled on the brain-server admin plane (loopback-only, gated on the operator admin token), not the data plane. A memory verb never mints, rotates, or revokes a key. See API keys for the full contract.
- Create —
POST /v1/api-keysbound to a(namespace, space). Thebrain_…secret is returned once (with itskey_hash_hex); store it immediately. - List —
GET /v1/api-keys?space=<hex>. - Revoke —
DELETE /v1/api-keys/{key_hash_hex}; subsequent connections with the key fail authentication.
Rotate by minting a replacement bound to the same (namespace, space) and revoking the old key by its hash once traffic has cut over, so there is never a window with no valid key.
A backend that serves many tenants from one process can hold a single service key and run each request under a different effective (namespace, space) with act_as — a per-request option on the request builders. It requires the key to hold the can_act_as grant; otherwise the server rejects with ActAsDenied.
import { EncodeBuilder } from "@brain-db/sdk";
await client.encode(
new EncodeBuilder("Tenant-scoped memory.").actAs(tenantNamespace, tenantSpaceId).build(),
);from brain_db_sdk import EncodeBuilder
client.encode(
EncodeBuilder("Tenant-scoped memory.").act_as(tenant_namespace, tenant_space_id).build()
)use brain_db_sdk::EncodeBuilder;
// act_as rebinds the effective (namespace, space) for this one write; the
// connection principal must hold the can_act_as grant. tenant_namespace is a
// &str/String; tenant_space_id is the target space's WireUuid ([u8; 16]).
client
.encode(
&EncodeBuilder::new("Tenant-scoped memory.")
.act_as(tenant_namespace, tenant_space_id)
.build(),
)
.await?;Permissions
The resolved identity carries per-verb permission flags fixed at key creation — which verbs the key may call (encode, recall, forget), whether it may perform admin operations, and whether it holds the can_act_as grant. To change what a key may do, mint a new key with the flags you want and rotate. See Identity by key → Permissions.
Give each app (space) its own key, and rotate on a schedule and on every personnel change. Because the key is the isolation boundary, sharing one across apps means you cannot rotate or revoke one without disrupting the others.
Was this page helpful?
Faithfulness checking
Score whether an LLM response is grounded in the memories you recalled, and gate downstream actions on the score — a client-side pattern built on recall.
Namespace lifecycle
Plan, create, populate, and decommission a namespace — the tenant boundary. Tenancy is managed in the dashboard; per-namespace schema is declared over the wire.