Embedding shmem in your app

If your product has its own agent, shmem can be its memory: each of your users gets a private shmem account that remembers them across sessions and can share what's relevant with their teammates. Your users never see shmem or sign in to it. Your backend creates their accounts and hands your agent a short-lived key for one user at a time.

This guide is for the backend engineers wiring shmem into a custom agent harness.

What you get

When we set you up, you receive two things:

  • A server URL, such as https://acme.shmem.secondmoment.ai. It is your own shmem server: only your users have accounts on it.
  • An app key. It is a long-lived secret for your backend only. It can create users and mint their session keys, and nothing else: it can't read or write anyone's memory.

Every credential you handle is an opaque bearer string. You don't sign tokens, run OAuth flows, or call an identity provider.

The flow

your backend ──app key──► PUT  /v1/embedded/users/{id}        (first use)
             ──app key──► POST /v1/embedded/users/{id}/keys   (each session)
                              └─► session key ──► your agent ──► shmem MCP

1. Provision a user, on first use

curl -X PUT https://acme.shmem.secondmoment.ai/v1/embedded/users/u_8271 \
  -H "Authorization: Bearer $SHMEM_APP_KEY" \
  -d '{"name": "Dana Ruiz"}'
{"app_user_id": "u_8271", "account": "u_8271@acme.embedded.secondmoment.ai", "name": "Dana Ruiz", "created": true}

{id} is your own ID for the user: 1–64 characters of letters, digits, ., _ and -. The call is idempotent. It answers 201 when it creates the user and 200 when the user already exists, so you can make it at the start of every session if that's simpler than tracking who you've provisioned.

The account is <id>@<your account domain>, on a domain we assign when we set you up. It is the user's identity: invitations address it, and it never changes. A user ID that isn't lowercase letters, digits, ., _ and - gets a hashed local part instead, so keep IDs lowercase if you want readable accounts. Nobody can sign in with an account, and shmem never sends it mail.

The name is optional and is how people are shown: agents see "Dana Ruiz" when they recall a teammate's memories or answer questions about them, rather than the account. It can change: send a new one on any later call. Without a name, people are shown by account.

2. Start a session: mint a session key

curl -X POST https://acme.shmem.secondmoment.ai/v1/embedded/users/u_8271/keys \
  -H "Authorization: Bearer $SHMEM_APP_KEY"
{"id": "api_key_01J…", "key": "sk_…", "expires_at": "2026-11-12T22:04:00Z"}

The key reads and writes this one user's memory, and expires after 12 hours. For a shorter lifetime, send {"ttl_seconds": 3600} (60 seconds to 12 hours). The key value is returned only once. Keep id if you want to end the session early.

Mint a fresh key for each agent session rather than storing keys: there are no refresh tokens, and minting is cheap.

3. Give the key to your agent

You can run shmem in either of two ways. The same session key works in both.

Remote MCP. This is the simplest option, with no shmem process to run. Point your agent's MCP client at <server URL>/mcp (streamable HTTP), with the session key as its bearer token:

{
  "mcpServers": {
    "shmem": {
      "type": "http",
      "url": "https://acme.shmem.secondmoment.ai/mcp",
      "headers": {"Authorization": "Bearer sk_…"}
    }
  }
}

Bundled local. Run shmem-mcp over stdio on your agent's host, against a store directory per user that syncs to that user's account. This lowers recall latency and tolerates a flaky network. Bundle the shmem and shmem-mcp binaries, then for each session:

export SHMEM_ROOT=/var/lib/myapp/shmem/u_8271
# The first session on this host:
echo "$SESSION_KEY" | shmem remote add origin https://acme.shmem.secondmoment.ai/mcp --token - --no-push
# Every later session: install the fresh key in place.
echo "$SESSION_KEY" | shmem remote set-token origin
# Then run the MCP server for the agent:
shmem-mcp

A store directory is a cache of the user's account, not the account itself. You can delete it, or rebuild it on another host, and nothing is lost.

Your backend can use a session key the same way when it reads or writes memory outside an agent session, for a bulk import, say.

Saving conversations

Embedded shmem has no automatic capture. Coding agents get it from hooks that shmem installs, but your harness has none, so nothing is remembered unless something writes it. Save each turn from your harness, not by asking the model to: after a turn, call the add_memory_batch tool with the user's message as a user fact and the reply as an llm fact. Then the agent only needs to recall, and memory doesn't depend on the model choosing to store something. Tell the agent its user's name, so anything it writes itself names people instead of saying "the user", which reads wrong once a teammate has it shared.

4. End access

To end Call Effect
One session DELETE /v1/embedded/users/{id}/keys/{key_id} That key stops working at once. Unrevoked keys expire on their own.
A user's access DELETE /v1/embedded/users/{id}/access Every key the user holds stops working, and no new ones can be minted. Their account and memory stay. Provisioning them again restores the same memory.

Both answer 204 and take the app key. Deleting a user and their memory isn't supported yet; DELETE /v1/embedded/users/{id} is reserved for it.

Errors

Every endpoint answers errors as JSON, {"error": "…"}:

Status Meaning
400 A malformed user ID or body
401 A missing or invalid app key. On /mcp, a missing, expired or revoked session key: mint a new one.
404 No such user (minting before provisioning), or no such key for that user
409 A key was minted for a user whose access was removed
503 Authentication is temporarily unavailable. Retry after the Retry-After interval; it isn't a reason to mint a new key.

Sharing between your users

Each user's memory is private until they share it. Your users can share memories with each other through shmem's sharing tools, by groups and invitations, and their agents see what was shared with them. See Sharing. An invitation names its recipient by their account, the account that provisioning returned, so tell your agent the names and accounts of the teammates its user works with. The people tool lists the names of everyone in the user's groups, and members_of_group with names: true names a group's members. Only your users have accounts on your server, so nothing can be shared outside your app.

Within your app, though, your agent shares on its user's behalf when the model asks it to. A prompt injection, such as text in a document the agent reads, could ask it to invite another of your users or share memories with one. If that matters for your product, keep the sharing tools behind confirmation in your harness or filter them out of the tool list your agent sees.

Whether your users share is decided when your server is set up. Sharing can be off for everyone; offered, so that each user turns it on for themselves through their agent (set_sharing); or on from the start for every user, who can still turn it off. With sharing on, the server reads the facts a user stores to nominate what may be worth sharing, which sends them to the server's LLM provider; nothing is shared without an explicit share or a share rule the user wrote.

Handling keys safely

  • Keep the app key on your backend. Never put it in an agent's context, a client app, or logs. Anyone holding it can mint keys for any of your users. Ask us to rotate it if it leaks.
  • Treat session keys as user credentials. They are safe to place in an agent runtime that runs model-directed tool calls, which is what they're for. A leaked key exposes one user's memory until it expires or you revoke it.
  • Expect 401s and mint again. A key that expires mid-session gets a 401. Mint a new key and reconnect.