{
  "openapi": "3.1.0",
  "info": {
    "title": "Memory01",
    "version": "1.0.0",
    "description": "Agentic memory for AI agents: free to store and search. $0.01 USDC on Base over x402. First 30 searches/day free per agent. Write token authentication (one token per IP + agent_id, with a recovery code issued at first store for IP changes). Starter $2 for 500 credits (never expire, 200/day rate limit), Pro $9/month unlimited (500/day rate limit), Enterprise $20/month unlimited+priority (2000/day rate limit).",
    "contact": {
      "email": "info@node01.org"
    },
    "x-guidance": "You are an AI agent. Store memories (free) and search them semantically (30 free/day). Write token is bound to (IP, agent_id). No signup, no API key. Read https://memory.node01.org/agents.md first."
  },
  "servers": [
    {
      "url": "https://memory.node01.org"
    }
  ],
  "paths": {
    "/api/memory/store": {
      "post": {
        "operationId": "store_memory",
        "summary": "Store a memory (free)",
        "tags": [
          "Memory"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "agent_id",
                  "key",
                  "value"
                ],
                "properties": {
                  "agent_id": {
                    "type": "string",
                    "description": "Agent identifier (treated as secret)"
                  },
                  "key": {
                    "type": "string",
                    "description": "Memory key/label"
                  },
                  "value": {
                    "type": "string",
                    "description": "Memory value/content"
                  },
                  "metadata": {
                    "type": "object",
                    "description": "Optional metadata"
                  },
                  "write_token": {
                    "type": "string",
                    "description": "Write token from first store call (required for subsequent calls)"
                  },
                  "session_id": {
                    "type": "string",
                    "description": "Optional session ID to scope memories (e.g., 'opencode-js-project1'). Omit for global."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Memory stored successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "stored": {
                      "type": "boolean"
                    },
                    "agent_id": {
                      "type": "string"
                    },
                    "key": {
                      "type": "string"
                    },
                    "stored_at": {
                      "type": "string"
                    },
                    "write_token": {
                      "type": "string",
                      "description": "Save this! Required for all future operations."
                    },
                    "tier": {
                      "type": "string",
                      "enum": [
                        "free",
                        "starter",
                        "pro",
                        "enterprise"
                      ]
                    },
                    "credits": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          }
        },
        "security": [],
        "description": "Store a key/value memory. Free. First call for a new (IP, agent_id) pair returns a write_token. Subsequent calls must include it."
      }
    },
    "/api/memory/search": {
      "post": {
        "operationId": "search_memory",
        "summary": "Search memories (free tier: 30/day, then $0.01 USDC)",
        "tags": [
          "Memory"
        ],
        "x-payment-info": {
          "price": {
            "mode": "fixed",
            "currency": "USD",
            "amount": "0.010000"
          },
          "protocols": [
            {
              "x402": {
                "network": "eip155:8453",
                "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
              }
            }
          ]
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "agent_id",
                  "write_token",
                  "query"
                ],
                "properties": {
                  "agent_id": {
                    "type": "string",
                    "description": "Agent identifier"
                  },
                  "write_token": {
                    "type": "string",
                    "description": "Write token from first store call"
                  },
                  "query": {
                    "type": "string",
                    "description": "Search query (keyword matching)"
                  },
                  "top_k": {
                    "type": "integer",
                    "description": "Number of results (1-20, default 5)"
                  },
                  "session_id": {
                    "type": "string",
                    "description": "Optional session ID filter. If provided, only searches within that session. Omit for global search."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Search results",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "results": {
                      "type": "array"
                    },
                    "free_tier": {
                      "type": "boolean"
                    },
                    "tier": {
                      "type": "string"
                    },
                    "credits_remaining": {
                      "type": "integer"
                    },
                    "free_searches_remaining": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "402": {
            "description": "Payment required"
          }
        },
        "description": "Semantic search over your stored memories. 50+ languages, cross-lingual. Free tier 30/day. After that, x402 payment ($0.01 USDC on Base)."
      }
    },
    "/api/memory/subscribe": {
      "post": {
        "operationId": "subscribe",
        "summary": "Subscribe to a plan (Starter/Pro/Enterprise)",
        "tags": [
          "Memory"
        ],
        "x-payment-info": {
          "price": {
            "mode": "fixed",
            "currency": "USD",
            "amount": "2.000000"
          },
          "protocols": [
            {
              "x402": {
                "network": "eip155:8453",
                "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
              }
            }
          ]
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "agent_id",
                  "write_token",
                  "plan"
                ],
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "plan": {
                    "type": "string",
                    "enum": [
                      "starter",
                      "pro",
                      "enterprise"
                    ],
                    "description": "starter=$2/500 credits, pro=$9/mo unlimited, enterprise=$20/mo unlimited+priority"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Subscription activated",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "plan": {
                      "type": "string",
                      "enum": [
                        "starter",
                        "pro",
                        "enterprise"
                      ]
                    },
                    "tier": {
                      "type": "string"
                    },
                    "credits_added": {
                      "type": "integer"
                    },
                    "total_credits": {
                      "type": "integer"
                    },
                    "expires_at": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "rate_limit": {
                      "type": "integer"
                    },
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "402": {
            "description": "Payment required"
          }
        },
        "description": "Subscribe to a paid plan (Starter/Pro/Enterprise). x402 payment."
      }
    },
    "/api/health": {
      "get": {
        "operationId": "health",
        "summary": "Service health check",
        "tags": [
          "Health"
        ],
        "responses": {
          "200": {
            "description": "Service is healthy"
          }
        },
        "security": []
      }
    },
    "/api/memory/stats": {
      "get": {
        "operationId": "stats",
        "summary": "Global memory stats",
        "tags": [
          "Memory"
        ],
        "responses": {
          "200": {
            "description": "Global statistics"
          }
        },
        "security": []
      }
    },
    "/api/memory/recover": {
      "post": {
        "operationId": "recover_token",
        "description": "Recover a write_token on a new IP using the recovery code issued at first store.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "agent_id",
                  "recovery_code"
                ],
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "recovery_code": {
                    "type": "string",
                    "description": "Recovery code (rc_...) issued at first store."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "New write_token issued for this IP."
          },
          "403": {
            "description": "Invalid recovery code."
          },
          "429": {
            "description": "Rate limited."
          }
        }
      }
    },
    "/api/memory/recovery/regenerate": {
      "post": {
        "operationId": "regenerate_recovery_code",
        "description": "Issue a new recovery code (invalidates the old one). Requires valid write_token from the original IP.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "write_token"
                ],
                "properties": {
                  "write_token": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "New recovery code issued."
          },
          "403": {
            "description": "Invalid token."
          }
        }
      }
    },
    "/api/memory/bootstrap": {
      "post": {
        "operationId": "bootstrap_base_memories",
        "description": "Create conventional base memories if missing (idempotent). Default keys: credentials-location (sensitive), user-preferences, user-details, code-style.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "agent_id",
                  "write_token"
                ],
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "keys": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    },
                    "description": "Optional list of base keys to create (defaults to all conventional)."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Base memories initialized."
          },
          "403": {
            "description": "Invalid token."
          }
        }
      }
    },
    "/api/memory/get": {
      "post": {
        "operationId": "get_memory",
        "description": "Retrieve a single memory by key (useful for sensitive base memories like credentials-location).",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "agent_id",
                  "write_token",
                  "key"
                ],
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "key": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Memory found."
          },
          "404": {
            "description": "Not found."
          },
          "403": {
            "description": "Invalid token."
          }
        }
      }
    },
    "/api/memory/init": {
      "post": {
        "operationId": "init_memory",
        "description": "Initialize Memory01 in one call: creates token, bootstraps base memories, stores credentials-location pointer, returns recovery code and best practices.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "agent_id"
                ],
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string",
                    "description": "Existing write token (omit on first run)"
                  },
                  "key": {
                    "type": "string",
                    "description": "Optional first memory key (default: init)"
                  },
                  "value": {
                    "type": "string",
                    "description": "Optional first memory value"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Initialized. Returns write_token, recovery_code (if new), bootstrap result, guide with best practices."
          },
          "403": {
            "description": "Token already exists or invalid."
          }
        }
      }
    },
    "/api/memory/register-key": {
      "post": {
        "operationId": "register_key",
        "description": "Bind an Ed25519 public key to an existing write_token. Must be called from the IP that owns the token. After registration, signed requests work from any IP.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "agent_id",
                  "write_token",
                  "public_key"
                ],
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "public_key": {
                    "type": "string",
                    "description": "Base64-encoded raw Ed25519 public key (32 bytes)."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Public key registered; token is now key-bound."
          },
          "400": {
            "description": "Missing field or invalid public key."
          },
          "403": {
            "description": "write_token invalid for this IP or wrong agent_id."
          },
          "429": {
            "description": "Rate limited."
          }
        }
      }
    },
    "/api/memory/key": {
      "get": {
        "operationId": "key_status",
        "description": "Check whether an agent has a registered Ed25519 public key (no secrets returned).",
        "parameters": [
          {
            "name": "agent_id",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "key_bound true/false plus registered_at when bound."
          }
        }
      }
    },
    "/api/memory/export": {
      "get": {
        "summary": "Export memories (mem01-memory@1)",
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      },
      "post": {
        "summary": "Export memories",
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      }
    },
    "/api/memory/import": {
      "post": {
        "summary": "Import memories upsert",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "memories": {
                    "type": "array"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      }
    },
    "/api/memory/forget": {
      "post": {
        "summary": "Forget a memory",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "key": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      }
    },
    "/api/memory/dream": {
      "post": {
        "summary": "Dreaming ideate (optional; NOT consolidate)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "dream": {
                    "type": "string",
                    "enum": [
                      "ideate"
                    ],
                    "default": "ideate"
                  },
                  "since": {
                    "type": "string"
                  },
                  "context": {
                    "type": "string"
                  },
                  "n": {
                    "type": "integer"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      }
    },
    "/api/memory/invalidate": {
      "post": {
        "summary": "Temporal invalidation (not delete); set valid_to, keep history, bump version",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "key": {
                    "type": "string"
                  },
                  "valid_to": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      }
    },
    "/api/memory/relate": {
      "post": {
        "summary": "Add typed KG relation",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "key": {
                    "type": "string"
                  },
                  "type": {
                    "type": "string"
                  },
                  "target": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      }
    },
    "/api/memory/relations": {
      "post": {
        "summary": "List typed relations",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "agent_id": {
                    "type": "string"
                  },
                  "write_token": {
                    "type": "string"
                  },
                  "key": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "ok"
          }
        }
      }
    }
  },
  "x-payment-headers": {
    "request_current": "X-Payment",
    "request_legacy_alias": "payment-signature",
    "response_402": "PAYMENT-REQUIRED",
    "response_200": "PAYMENT-RESPONSE",
    "notes": "Both request header names are accepted and carry the same signed payment proof. Prefer X-Payment."
  },
  "x-free-tier": {
    "store": "always free, max 50000 chars per value",
    "search": "30 searches/day free per agent, then $0.01 USDC per search",
    "base_memories": "max 2000 chars per value",
    "rate_limits": {
      "free": "30/day (soft slowdown after 10, hard block after 30)",
      "starter": "200/day (soft slowdown after 50, hard block after 200)",
      "pro": "500/day (soft slowdown after 100, hard block after 500)",
      "enterprise": "2000/day (soft slowdown after 500, hard block after 2000)"
    }
  },
  "x-recovery": {
    "token_binding": "One write_token per (IP, agent_id).",
    "first_store": "Returns write_token AND recovery_code (shown once). Save both offline (chmod 600).",
    "recover": "POST /api/memory/recover {agent_id, recovery_code} -> new write_token for the current IP; plan and credits migrate to the new token.",
    "regenerate": "POST /api/memory/recovery/regenerate {write_token} (original IP) -> new recovery code, old one invalidated.",
    "lost_result": "A settled search is cached for 10 seconds (cursor TTL). Retry with the same payment header within 10s. After that the cursor expires (410) and a new payment is required; no partial refund."
  }
}