Memory compare-and-set
POST /api/memory/casAtomically write (or release) a key only if its current value equals `expected` - the coordination primitive for distributed locks and optimistic concurrency across agents. Acquire a lock: expected=null + a value + ttlSeconds. Release it: expected=<your token> with no value (deletes on match). Update safely: expected=<old>, value=<new>. Returns whether it swapped and the current value.
Input
| Field | Type | Description |
|---|---|---|
key * | string | Key to conditionally write |
expected | any | Required current value to match (null or omitted = key absent/expired) |
value | any | New value to set on match; omit to DELETE on match (lock release) |
ttlSeconds | number | Optional TTL for the written value (lease for locks) |
owner | string | Optional 0x namespace (requires a readwrite grant) |
Example output
{
"key": "locks/import",
"swapped": true,
"value": "agent-7",
"owner": "0x…",
"expiresAt": 1760086430
}
Try it - see the 402 challenge (free)
curl -i -X POST https://agent402.tools/api/memory/cas \
-H "Content-Type: application/json" \
-d '{"key":"locks/import","expected":null,"value":"agent-7","ttlSeconds":30}'
The response is HTTP 402 Payment Required with exact payment requirements. Any x402 v2 client pays automatically and retries:
Paid call (JavaScript agent)
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
const client = new x402Client();
registerExactEvmScheme(client, { signer: privateKeyToAccount(KEY) });
const payFetch = wrapFetchWithPayment(fetch, client);
const res = await payFetch("https://agent402.tools/api/memory/cas", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
"key": "locks/import",
"expected": null,
"value": "agent-7",
"ttlSeconds": 30
}),
});
Related tools
Memory write
POST /api/memoryPersistent key-value memory for agents, scoped to the paying wallet. Your x402 payment IS your authentication: the walle…
Memory read
GET /api/memoryRead from a wallet-scoped namespace. ?key=… returns the stored value; omit key to list keys. The read half of memory-wri…
Memory counter
POST /api/memory/incrAtomically increment (or decrement) a numeric key and return the new value - a coordination primitive for counters, lock…