{
  "openapi": "3.1.0",
  "info": {
    "title": "Debt-Anything API",
    "version": "1.0.0",
    "summary": "Deterministic 5-domain statutory interest, cloud SLA credit, duplicate charge, freight claim, and B2B AR reminder engine with exact Decimal math and SHA-256 receipts.",
    "description": "Polymorphic deterministic calculation and claim-evidence packet engine across UK/EU/US Statutory Late-Payment Interest, Cloud/SaaS SLA Outage Credits, Corporate Card & SaaS Duplicate/Zombie/Price-Hike Detection, Freight Liability Caps (Parcel/Montreal/Carmack), and Courteous B2B AR Reminders. Every request emits a tamper-evident SHA-256 DebtReceipt. HTTP 422 invalid inputs and errors are strictly billed $0 (billed_micros = 0). Zero customer data or billing trajectory resale guarantee. Software disclaimer: deterministic software tooling only; not a law firm, not a licensed collection agency, zero commission, and does not provide legal advice.",
    "contact": {
      "name": "Debt-Anything Operations",
      "email": "ops@fastmesh.dev",
      "url": "https://debt.fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://debt.fastmesh.dev",
      "description": "Public HTTPS Gateway Mount"
    },
    {
      "url": "http://127.0.0.1:9011",
      "description": "Local Runtime Server"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Service liveness and readiness probes" },
    { "name": "Calculation", "description": "Single and batch deterministic 5-domain recovery & AR calculations" },
    { "name": "Receipts", "description": "Tamper-evident SHA-256 DebtReceipt lookup and validation" },
    { "name": "MCP", "description": "Native flat JSON-RPC 2.0 Model Context Protocol endpoint" },
    { "name": "Billing", "description": "Creem HMAC-SHA256 prepaid credit webhook settlement" },
    { "name": "Discovery", "description": "AI agent discovery and technical recovery guides" }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "tags": ["Health"],
        "summary": "Check Debt-Anything service health",
        "responses": {
          "200": {
            "description": "Service is healthy and ready",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" },
                "example": {
                  "status": "ok",
                  "service": "Debt-Anything",
                  "version": "1.0.0",
                  "public_host": "debt.fastmesh.dev",
                  "port": 9011,
                  "domains": [
                    "statutory_interest",
                    "sla_credit",
                    "duplicate_charge",
                    "freight_claim",
                    "ar_reminder"
                  ],
                  "ledger_enabled": true,
                  "mcp_enabled": true,
                  "timestamp_utc": "2026-12-02T12:00:00+00:00"
                }
              }
            }
          }
        }
      }
    },
    "/calculate": {
      "post": {
        "operationId": "calculateSingle",
        "tags": ["Calculation"],
        "summary": "Calculate statutory interest, SLA credits, duplicate charges, freight caps, or AR reminders",
        "description": "Returns a deterministic, exact-Decimal, SHA-256 signed DebtReceipt. Strictly bills $0 (`billed_micros = 0`) on 422 invalid input, negative principal, inverted date windows, or error.",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Optional tenant identifier for prepaid ledger accounting and 10% recheck pricing."
          },
          {
            "name": "X-API-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Optional prepaid API key (omittable for zero-signup Try-Now sandbox)."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/DebtCalculationRequest" },
              "examples": {
                "statutory_interest_uk": {
                  "summary": "Domain 1: UK Late Payment Act 1998 (BoE + 8% + GBP 70 statutory fee)",
                  "value": {
                    "domain": "statutory_interest",
                    "jurisdiction": "uk_late_payment",
                    "principal": "5000.00",
                    "currency": "GBP",
                    "due_date": "2024-01-01",
                    "as_of_date": "2024-04-01",
                    "base_rate_pct": "5.25"
                  }
                },
                "sla_credit_cliff": {
                  "summary": "Domain 2: Cloud 99.9% SLA Outage Credit (Feb 2024 leap year)",
                  "value": {
                    "domain": "sla_credit",
                    "monthly_bill": "10000.00",
                    "sla_tier_pct": "99.9",
                    "billing_month": "2024-02",
                    "outages": [
                      { "start_utc": "2024-02-10T01:00:00Z", "end_utc": "2024-02-10T01:46:00Z", "planned_maintenance": false }
                    ]
                  }
                },
                "duplicate_charge_scan": {
                  "summary": "Domain 3: SaaS Duplicate, Zombie & Stealth Price Hike CSV Scanner",
                  "value": {
                    "domain": "duplicate_charge",
                    "window_days": 7,
                    "price_hike_threshold_pct": "10",
                    "csv_text": "tx_id,date,merchant,amount,currency,status,canceled_at,plan_unit_price\ntx_1,2026-10-01,Figma Inc.,450.00,USD,posted,,15.00\ntx_2,2026-10-02,FIGMA INC,450.00,USD,posted,,15.00\ntx_3,2026-10-15,OldTool Co,99.00,USD,posted,2026-09-30,99.00"
                  }
                },
                "freight_claim_montreal": {
                  "summary": "Domain 4: Montreal Convention Air Freight Damage & Redline Check",
                  "value": {
                    "domain": "freight_claim",
                    "carrier_regime": "montreal_convention",
                    "goods_value": "4500.00",
                    "freight_cost": "320.00",
                    "salvage_value": "0.00",
                    "weight_kg": "120",
                    "sdr_rate_usd": "1.33",
                    "montreal_sdr_per_kg": "26",
                    "waybill_number": "AWB-176-88392011",
                    "claim_type": "damage",
                    "delivery_or_ship_date": "2026-11-20",
                    "claim_date": "2026-11-28",
                    "documents_present": ["commercial_invoice", "bill_of_lading", "delivery_receipt", "damage_photos"]
                  }
                },
                "ar_reminder_30d": {
                  "summary": "Domain 5: Courteous 30-Day B2B AR Reconciliation & Statutory Interest Notice",
                  "value": {
                    "domain": "ar_reminder",
                    "creditor_name": "Northstar Engineering Ltd",
                    "debtor_name": "Meridian Manufacturing PLC",
                    "invoice_number": "INV-2026-441",
                    "principal": "6400.00",
                    "currency": "GBP",
                    "due_date": "2026-10-15",
                    "as_of_date": "2026-11-20",
                    "jurisdiction": "uk_late_payment",
                    "base_rate_pct": "4.50"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Deterministic calculation completed with signed DebtReceipt",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/DebtReceipt" }
              }
            }
          },
          "422": {
            "description": "Invalid input (negative amount, inverted dates, malformed CSV, unsupported regime) — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" },
                "example": {
                  "ok": false,
                  "error": "invalid_input_422",
                  "detail": "Principal must be strictly positive.",
                  "billed_micros": 0,
                  "zero_bill_reason": "invalid_input_422"
                }
              }
            }
          }
        }
      }
    },
    "/calculate/batch": {
      "post": {
        "operationId": "calculateBatch",
        "tags": ["Calculation"],
        "summary": "Calculate a heterogeneous CSV or JSON batch with hard budget cap",
        "description": "Executes concurrent multi-domain calculations bounded by `max_budget_micros`. Any row that fails input validation (422) is strictly billed $0.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BatchCalculateRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Batch execution summary and per-item DebtReceipts",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BatchCalculateResponse" }
              }
            }
          },
          "422": {
            "description": "Invalid batch request — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/receipt/{receipt_id}": {
      "get": {
        "operationId": "getReceipt",
        "tags": ["Receipts"],
        "summary": "Fetch a stored DebtReceipt and verify its SHA-256 attestation",
        "parameters": [
          {
            "name": "receipt_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "Unique DebtReceipt ID"
          }
        ],
        "responses": {
          "200": {
            "description": "Receipt retrieved with cryptographic attestation check",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "signature_valid": { "type": "boolean" },
                    "receipt": { "$ref": "#/components/schemas/DebtReceipt" }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Receipt ID not found",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/verify_receipt": {
      "post": {
        "operationId": "verifyReceipt",
        "tags": ["Receipts"],
        "summary": "Verify an inline or stored DebtReceipt SHA-256 attestation",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "receipt_id": { "type": "string" },
                  "receipt": { "$ref": "#/components/schemas/DebtReceipt" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Verification status and tamper check result",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "signature_valid": { "type": "boolean" },
                    "tampered": { "type": "boolean" },
                    "receipt_id": { "type": "string" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/mcp": {
      "post": {
        "operationId": "mcpJsonRpc",
        "tags": ["MCP"],
        "summary": "Native flat JSON-RPC 2.0 MCP endpoint",
        "description": "Supports initialize, notifications/initialized, ping, tools/list, and tools/call for debt_calculate, debt_calculate_batch, and debt_verify_receipt.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/McpJsonRpcRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC 2.0 response payload",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/McpJsonRpcResponse" }
              }
            }
          },
          "202": {
            "description": "Accepted notification (notifications/initialized)"
          }
        }
      }
    },
    "/webhooks/creem": {
      "post": {
        "operationId": "creemWebhook",
        "tags": ["Billing"],
        "summary": "Creem HMAC-SHA256 payment webhook ingress",
        "description": "Verifies `creem-signature` HMAC-SHA256 using `CREEM_WEBHOOK_SECRET` from environment variables, deduplicates by `event_id`, hashes customer identifiers as `payer_sha256_12`, and atomically credits the tenant wallet inside a SQLite WAL `BEGIN IMMEDIATE` transaction.",
        "parameters": [
          {
            "name": "creem-signature",
            "in": "header",
            "required": true,
            "schema": { "type": "string" },
            "description": "HMAC-SHA256 hex digest (or sha256=<hex>) computed over the raw HTTP request body."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreemWebhookPayload" },
              "example": {
                "id": "evt_creem_debt_01",
                "eventType": "checkout.completed",
                "object": {
                  "product_id": "prod_debt_anything_growth_79",
                  "amount": 7900,
                  "currency": "USD",
                  "customer": { "email": "finance@example.com" },
                  "metadata": { "tenant_id": "tenant_acme", "sku_id": "sku_debt_growth_79" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Event verified and atomically settled (or deduplicated idempotently)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "event_id": { "type": "string" },
                    "tenant_id": { "type": "string" },
                    "payer_sha256_12": { "type": "string" },
                    "credits_granted_micros": { "type": "integer" },
                    "balance_micros": { "type": "integer" },
                    "replayed": { "type": "boolean" },
                    "status": { "type": "string" }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Invalid or missing creem-signature HMAC-SHA256 digest"
          }
        }
      }
    },
    "/llms.txt": {
      "get": {
        "operationId": "getLlmsTxt",
        "tags": ["Discovery"],
        "summary": "Machine-readable AI agent discovery index for debt.fastmesh.dev",
        "responses": {
          "200": {
            "description": "Plain-text llms.txt discovery file",
            "content": { "text/plain": { "schema": { "type": "string" } } }
          }
        }
      }
    },
    "/skill.md": {
      "get": {
        "operationId": "getSkillMd",
        "tags": ["Discovery"],
        "summary": "Agent skill contract for Debt-Anything REST, MCP, and Treg Hub",
        "responses": {
          "200": {
            "description": "Markdown skill specification",
            "content": { "text/markdown": { "schema": { "type": "string" } } }
          }
        }
      }
    },
    "/guides": {
      "get": {
        "operationId": "listGuides",
        "tags": ["Discovery"],
        "summary": "Technical recovery, statutory interest, SLA credit, and freight claim guides index",
        "responses": {
          "200": {
            "description": "HTML guides directory",
            "content": { "text/html": { "schema": { "type": "string" } } }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "HealthResponse": {
        "type": "object",
        "required": ["status", "service"],
        "properties": {
          "status": { "type": "string", "enum": ["ok"] },
          "service": { "type": "string" },
          "version": { "type": "string" },
          "public_host": { "type": "string" },
          "port": { "type": "integer" },
          "domains": { "type": "array", "items": { "type": "string" } },
          "cache": { "type": "object" },
          "ledger": { "type": "object" },
          "timestamp_utc": { "type": "string", "format": "date-time" }
        }
      },
      "DebtCalculationRequest": {
        "type": "object",
        "required": ["domain"],
        "properties": {
          "domain": {
            "type": "string",
            "enum": [
              "statutory_interest",
              "sla_credit",
              "duplicate_charge",
              "freight_claim",
              "ar_reminder"
            ]
          },
          "target": { "type": "string" },
          "recheck": { "type": "boolean", "default": false },
          "no_cache": { "type": "boolean", "default": false },
          "tenant_id": { "type": "string" },
          "payload": { "type": "object" },
          "options": { "type": "object" }
        }
      },
      "DebtReceipt": {
        "type": "object",
        "required": [
          "receipt_id",
          "domain",
          "verdict",
          "payload_sha256",
          "sha256_attestation",
          "billed_micros",
          "base_price_micros",
          "timestamp_utc"
        ],
        "properties": {
          "receipt_id": { "type": "string" },
          "domain": {
            "type": "string",
            "enum": [
              "statutory_interest",
              "sla_credit",
              "duplicate_charge",
              "freight_claim",
              "ar_reminder"
            ]
          },
          "target": { "type": "string" },
          "verdict": { "type": "string" },
          "status": { "type": "string" },
          "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
          "checks": { "type": "array", "items": { "type": "object" } },
          "evidence_sources": { "type": "array", "items": { "type": "object" } },
          "details": { "type": "object" },
          "payload_sha256": { "type": "string" },
          "sha256_attestation": { "type": "string" },
          "billed_micros": { "type": "integer", "minimum": 0 },
          "base_price_micros": { "type": "integer", "minimum": 0 },
          "cache_hit": { "type": "boolean" },
          "cache_tier": { "type": "string", "enum": ["live", "shared_cache", "recheck_10pct"] },
          "timestamp_utc": { "type": "string", "format": "date-time" },
          "zero_bill_reason": { "type": "string" },
          "disclaimer": { "type": "string" }
        }
      },
      "BatchCalculateRequest": {
        "type": "object",
        "properties": {
          "items": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/DebtCalculationRequest" }
          },
          "csv_text": { "type": "string" },
          "max_budget_micros": { "type": "integer", "minimum": 0 },
          "tenant_id": { "type": "string" },
          "recheck": { "type": "boolean", "default": false },
          "max_workers": { "type": "integer", "minimum": 1, "maximum": 16 }
        }
      },
      "BatchCalculateResponse": {
        "type": "object",
        "properties": {
          "batch_id": { "type": "string" },
          "tenant_id": { "type": "string" },
          "total_items": { "type": "integer" },
          "processed_items": { "type": "integer" },
          "stopped_by_budget": { "type": "boolean" },
          "skipped_by_budget_count": { "type": "integer" },
          "cache_hits": { "type": "integer" },
          "zero_billed_rows": { "type": "integer" },
          "max_budget_micros": { "type": ["integer", "null"] },
          "total_billed_micros": { "type": "integer" },
          "elapsed_ms": { "type": "integer" },
          "timestamp_utc": { "type": "string", "format": "date-time" },
          "batch_sha256": { "type": "string" },
          "results": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "row_index": { "type": "integer" },
                "skipped_by_budget": { "type": "boolean" },
                "receipt": { "$ref": "#/components/schemas/DebtReceipt" }
              }
            }
          }
        }
      },
      "ZeroBillErrorResponse": {
        "type": "object",
        "required": ["ok", "error", "billed_micros"],
        "properties": {
          "ok": { "type": "boolean" },
          "error": { "type": "string" },
          "detail": { "type": "string" },
          "billed_micros": { "type": "integer", "enum": [0] },
          "zero_bill_reason": { "type": "string" }
        }
      },
      "McpJsonRpcRequest": {
        "type": "object",
        "required": ["jsonrpc", "method"],
        "properties": {
          "jsonrpc": { "type": "string", "enum": ["2.0"] },
          "id": { "type": ["string", "integer", "null"] },
          "method": { "type": "string" },
          "params": { "type": "object" }
        }
      },
      "McpJsonRpcResponse": {
        "type": "object",
        "required": ["jsonrpc"],
        "properties": {
          "jsonrpc": { "type": "string", "enum": ["2.0"] },
          "id": { "type": ["string", "integer", "null"] },
          "result": { "type": "object" },
          "error": { "type": "object" }
        }
      },
      "CreemWebhookPayload": {
        "type": "object",
        "required": ["id", "eventType", "object"],
        "properties": {
          "id": { "type": "string" },
          "eventType": { "type": "string" },
          "object": { "type": "object" }
        }
      }
    }
  }
}
