{
  "openapi": "3.1.0",
  "info": {
    "title": "Escrow-Anything API",
    "version": "1.0.0",
    "summary": "Zero-custody SQLite WAL double-entry micro-USD metering, 4D budget lock, Proof-of-Delivery escrow, multi-party split ledger, and Creem/Escrow.com reconciliation.",
    "description": "Polymorphic 5-mode settlement engine for autonomous AI agents and B2B workflows. Strictly zero third-party fiat custody (platform_retained_third_party_funds_micros = 0); all fiat checkout and tax compliance runs via Creem MoR and licensed Escrow.com. Enforces SUM(debits) == SUM(credits) in integer micro-USD (1 USD = 1,000,000 micros), non-negative spendable balance, and $0 billing on misses, errors, 422 inputs, and fake-200 responses.",
    "contact": {
      "name": "Escrow-Anything Operations",
      "email": "ops@fastmesh.dev",
      "url": "https://escrow.fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://escrow.fastmesh.dev",
      "description": "Public HTTPS Gateway Mount"
    },
    {
      "url": "http://127.0.0.1:8805",
      "description": "Local Runtime Server"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Service liveness and double-entry invariant probes" },
    { "name": "Wallets", "description": "Prepaid micro-USD entitlement balances" },
    { "name": "Mode1", "description": "API/MCP atomic pre-reserve and exact settle ($0 on fake-200/miss)" },
    { "name": "Mode2", "description": "4D budget governance (per_call, per_hour, per_day, per_task) & allow/ask/deny" },
    { "name": "Mode3", "description": "Proof-of-Delivery conditional escrow release or 100% auto-refund" },
    { "name": "Mode4", "description": "Integer-conserving multi-party splits and atomic clawback reversal" },
    { "name": "Mode5", "description": "Creem HMAC-SHA256 webhooks, Escrow.com tracking, and two-way reconciliation" },
    { "name": "MCP", "description": "Native JSON-RPC 2.0 Model Context Protocol endpoint" },
    { "name": "Sandbox", "description": "Public 5-second Try-Now interactive simulation" }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "tags": ["Health"],
        "summary": "Check Escrow-Anything service health and double-entry invariant status",
        "responses": {
          "200": {
            "description": "Service healthy and ledger balanced",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" }
              }
            }
          }
        }
      }
    },
    "/openapi.json": {
      "get": {
        "operationId": "getOpenApiSpec",
        "tags": ["Health"],
        "summary": "Return OpenAPI 3.1.0 machine-readable specification",
        "responses": { "200": { "description": "OpenAPI JSON" } }
      }
    },
    "/mcp": {
      "post": {
        "operationId": "invokeMcp",
        "tags": ["MCP"],
        "summary": "Native JSON-RPC 2.0 MCP endpoint exposing 9 escrow & metering tools",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/JsonRpcRequest" }
            }
          }
        },
        "responses": { "200": { "description": "JSON-RPC 2.0 response" } }
      }
    },
    "/webhooks/creem": {
      "post": {
        "operationId": "ingestCreemWebhook",
        "tags": ["Mode5"],
        "summary": "Idempotent Creem HMAC-SHA256 webhook handler (401 on invalid/missing signature)",
        "parameters": [
          {
            "name": "creem-signature",
            "in": "header",
            "required": true,
            "schema": { "type": "string" },
            "description": "HMAC-SHA256 signature computed with CREEM_WEBHOOK_SECRET"
          }
        ],
        "responses": {
          "200": { "description": "Webhook event processed or idempotently replayed" },
          "401": { "description": "Missing, tampered, or invalid signature" }
        }
      }
    },
    "/v1/wallets/topup": {
      "post": {
        "operationId": "topupWallet",
        "tags": ["Wallets"],
        "summary": "Credit tenant spendable micro-USD entitlement balance",
        "responses": { "200": { "description": "Wallet credited with double-entry journal proof" } }
      }
    },
    "/v1/wallets/{tenant_id}": {
      "get": {
        "operationId": "getWallet",
        "tags": ["Wallets"],
        "summary": "Inspect tenant available_micros, locked_micros, 4D cap usage, and recent journal entries",
        "parameters": [
          { "name": "tenant_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Wallet and ledger snapshot" } }
      }
    },
    "/v1/reserve": {
      "post": {
        "operationId": "reserveFunds",
        "tags": ["Mode1"],
        "summary": "Mode 1: Atomically lock max_cost_micros prior to API/MCP execution",
        "responses": {
          "200": { "description": "Reservation created or replayed idempotently" },
          "402": { "description": "Insufficient spendable balance" },
          "409": { "description": "Idempotency conflict on mutated payload" }
        }
      }
    },
    "/v1/settle": {
      "post": {
        "operationId": "settleReservation",
        "tags": ["Mode1"],
        "summary": "Mode 1: Settle exact actual_cost_micros and unlock unused delta ($0 on miss/fake-200)",
        "responses": {
          "200": { "description": "Settled or released at $0" },
          "400": { "description": "actual_cost_micros exceeds reserved_micros" }
        }
      }
    },
    "/v1/release": {
      "post": {
        "operationId": "releaseReservation",
        "tags": ["Mode1"],
        "summary": "Mode 1: Explicit 100% unbilled release of reserved funds",
        "responses": { "200": { "description": "Released" } }
      }
    },
    "/v1/budget/policy": {
      "post": {
        "operationId": "configureBudgetPolicy",
        "tags": ["Mode2"],
        "summary": "Mode 2: Configure 4D spend caps (per_call, per_hour, per_day, per_task) and allow/ask/deny rules",
        "responses": { "200": { "description": "Policy saved" } }
      }
    },
    "/v1/budget/check": {
      "post": {
        "operationId": "checkBudgetLock",
        "tags": ["Mode2"],
        "summary": "Mode 2: Evaluate 4D caps & policy; freeze funds into approval ticket on ask",
        "responses": {
          "200": { "description": "Allowed or frozen pending approval" },
          "402": { "description": "Insufficient funds" },
          "403": { "description": "Denied by policy rule or 4D hard cap" }
        }
      }
    },
    "/v1/approvals/{approval_id}/decide": {
      "post": {
        "operationId": "decideApproval",
        "tags": ["Mode2"],
        "summary": "Mode 2: Approve or reject pending ask ticket (rejection auto-unfreezes 100% of locked funds)",
        "parameters": [
          { "name": "approval_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Decision recorded" } }
      }
    },
    "/v1/escrow/create": {
      "post": {
        "operationId": "createPodEscrow",
        "tags": ["Mode3"],
        "summary": "Mode 3: Lock funds under Proof-of-Delivery (json_schema, sha256, http_verifier, composite)",
        "responses": {
          "200": { "description": "Escrow locked" },
          "402": { "description": "Insufficient funds" }
        }
      }
    },
    "/v1/escrow/deliver": {
      "post": {
        "operationId": "deliverPodEscrow",
        "tags": ["Mode3"],
        "summary": "Mode 3: Verify deliverable against PoD constraints (release on pass, 100% auto-refund on failure)",
        "responses": { "200": { "description": "PoD resolved" } }
      }
    },
    "/v1/escrow/{escrow_id}": {
      "get": {
        "operationId": "getPodEscrow",
        "tags": ["Mode3"],
        "summary": "Mode 3: Inspect conditional escrow status and verification receipt",
        "parameters": [
          { "name": "escrow_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Escrow record" },
          "404": { "description": "Not found" }
        }
      }
    },
    "/v1/splits/execute": {
      "post": {
        "operationId": "executeSplit",
        "tags": ["Mode4"],
        "summary": "Mode 4: Execute integer-conserving multi-party split (platform/creator/affiliate)",
        "responses": { "200": { "description": "Split settled" } }
      }
    },
    "/v1/splits/reverse": {
      "post": {
        "operationId": "reverseSplit",
        "tags": ["Mode4"],
        "summary": "Mode 4: Atomic clawback reversal of a settled multi-party split",
        "responses": { "200": { "description": "Split reversed" } }
      }
    },
    "/v1/escrow-com/record": {
      "post": {
        "operationId": "recordEscrowComMilestone",
        "tags": ["Mode5"],
        "summary": "Mode 5: Record non-custodial Escrow.com 2017-09-01 milestone state",
        "responses": { "200": { "description": "Milestone recorded" } }
      }
    },
    "/v1/reconcile/report": {
      "get": {
        "operationId": "getReconcileReport",
        "tags": ["Mode5"],
        "summary": "Mode 5: Two-way reconciliation report comparing double-entry ledger vs Creem & Escrow.com",
        "responses": { "200": { "description": "Reconciliation report" } }
      },
      "post": {
        "operationId": "runReconcileReport",
        "tags": ["Mode5"],
        "summary": "Mode 5: Run two-way reconciliation against provided external Creem/Escrow.com records",
        "responses": { "200": { "description": "Reconciliation report" } }
      }
    },
    "/v1/ledger/audit": {
      "get": {
        "operationId": "auditLedger",
        "tags": ["Health"],
        "summary": "Verify SUM(debits) == SUM(credits), zero negative balances, and zero-custody invariant",
        "responses": { "200": { "description": "Double-entry audit proof" } }
      }
    },
    "/v1/try-now/simulate": {
      "post": {
        "operationId": "simulateTryNow",
        "tags": ["Sandbox"],
        "summary": "Public 5-second Try-Now sandbox simulation covering all 5 settlement modes",
        "responses": { "200": { "description": "Simulation output with wallet and double-entry audit" } }
      }
    }
  },
  "components": {
    "schemas": {
      "HealthResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string" },
          "service": { "type": "string" },
          "version": { "type": "string" },
          "public_host": { "type": "string" },
          "port": { "type": "integer" },
          "zero_custody_invariant": { "type": "boolean" },
          "platform_retained_third_party_funds_micros": { "type": "integer" },
          "ledger_ok": { "type": "boolean" },
          "ledger_drift_micros": { "type": "integer" },
          "mcp_enabled": { "type": "boolean" },
          "timestamp_utc": { "type": "string" }
        }
      },
      "JsonRpcRequest": {
        "type": "object",
        "required": ["jsonrpc", "method"],
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer", "null"] },
          "method": { "type": "string" },
          "params": { "type": "object" }
        }
      }
    }
  },
  "x-mcp-tools": [
    "escrow_reserve",
    "escrow_settle",
    "escrow_budget_check",
    "escrow_approval_decide",
    "escrow_pod_create",
    "escrow_pod_verify",
    "escrow_split",
    "escrow_reconcile",
    "escrow_balance"
  ]
}
