{
  "openapi": "3.1.0",
  "info": {
    "title": "Harvest-Anything API (ANY-A03)",
    "version": "1.0.0",
    "summary": "Polymorphic 5-Family Public Web, Filing, Tender, Registry, Feed, and PDF Schema Extraction Engine.",
    "description": "Deterministic pure-Python-stdlib extraction across public gov tenders/notices, SEC EDGAR filings, ClinicalTrials/patents, customs/RSS/Atom feeds, and embedded JSON-LD/Microdata/PDF text blocks with buyer JSON Schema validation, per-field confidence, source line references, SSRF 422 guard, anti-fake-200 $0 billing, and 7d-30d verified caching.",
    "contact": {
      "name": "Harvest-Anything Operations",
      "url": "https://harvest.fastmesh.dev",
      "email": "ops@fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://harvest.fastmesh.dev",
      "description": "Production HTTPS gateway"
    },
    {
      "url": "http://127.0.0.1:8803",
      "description": "Local development upstream"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Service liveness, extractor modes, cache stats, and spend cap metadata" },
    { "name": "Harvest", "description": "Single-target, batch, and incremental diff extraction endpoints" },
    { "name": "Billing", "description": "Creem prepaid credit packs, hash-chained ledger balances, and HMAC-SHA256 webhooks" },
    { "name": "MCP", "description": "Native JSON-RPC 2.0 Remote Model Context Protocol endpoint" }
  ],
  "paths": {
    "/": {
      "get": {
        "tags": ["Harvest"],
        "operationId": "getConsole",
        "summary": "5-second Try-Now extraction web console",
        "responses": {
          "200": { "description": "HTML console" }
        }
      }
    },
    "/try": {
      "get": {
        "tags": ["Harvest"],
        "operationId": "getTryConsole",
        "summary": "Alias for 5-second Try-Now extraction web console",
        "responses": {
          "200": { "description": "HTML console" }
        }
      }
    },
    "/health": {
      "get": {
        "tags": ["Health"],
        "operationId": "getHealth",
        "summary": "Check service liveness, supported modes, cache stats, and ammo cap",
        "responses": {
          "200": {
            "description": "Service health snapshot",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" }
              }
            }
          }
        }
      }
    },
    "/harvest": {
      "post": {
        "tags": ["Harvest"],
        "operationId": "runHarvestExtract",
        "summary": "Extract clean schema-validated JSON from a public URL or inline payload",
        "description": "Runs polymorphic extraction across gov_notices, sec_filings, trials_patents, customs_feeds, or embedded_pdf_schema (or auto). Enforces SSRF HTTP 422 guard and strict $0 billing on fake-200 soft-blocks, parse failures, or empty results.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/HarvestRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "HarvestReceipt (billed_micros == 0 if soft-blocked, parse failed, or empty)",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HarvestReceipt" }
              }
            }
          },
          "422": {
            "description": "SSRF blocked or malformed input rejected with $0 billing",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HarvestReceipt" }
              }
            }
          }
        }
      }
    },
    "/batch": {
      "post": {
        "tags": ["Harvest"],
        "operationId": "runHarvestBatch",
        "summary": "Run concurrent heterogeneous batch extraction with hard max_budget_micros cap",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BatchHarvestRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Aggregated batch HarvestReceipt",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BatchHarvestResponse" }
              }
            }
          },
          "422": {
            "description": "Malformed batch request rejected with $0 billing"
          }
        }
      }
    },
    "/webhooks/diff": {
      "get": {
        "tags": ["Harvest"],
        "operationId": "listDiffWatches",
        "summary": "List registered incremental diff watches",
        "responses": {
          "200": { "description": "Active diff watches" }
        }
      },
      "post": {
        "tags": ["Harvest"],
        "operationId": "registerOrDispatchDiffWatch",
        "summary": "Register an incremental diff watch or run an immediate diff webhook check",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/DiffWatchRequest" }
            }
          }
        },
        "responses": {
          "200": { "description": "Diff watch status and delta summary" },
          "422": { "description": "SSRF or invalid watch target ($0 billed)" }
        }
      }
    },
    "/billing/pricing": {
      "get": {
        "tags": ["Billing"],
        "operationId": "getBillingPricing",
        "summary": "Get Creem prepaid packs, per-mode micro-pricing, and $0 guarantee",
        "responses": {
          "200": { "description": "Pricing catalog JSON" }
        }
      }
    },
    "/billing/packs": {
      "get": {
        "tags": ["Billing"],
        "operationId": "getBillingPacks",
        "summary": "Get Creem prepaid credit pack SKUs",
        "responses": {
          "200": { "description": "Array of Creem credit packs" }
        }
      }
    },
    "/billing/balance": {
      "get": {
        "tags": ["Billing"],
        "operationId": "getBillingBalance",
        "summary": "Query prepaid credit balance and usage counters for an account",
        "parameters": [
          {
            "name": "account_id",
            "in": "query",
            "required": false,
            "schema": { "type": "string", "default": "anonymous" }
          }
        ],
        "responses": {
          "200": { "description": "Account credit balance JSON" }
        }
      }
    },
    "/billing/ledger": {
      "get": {
        "tags": ["Billing"],
        "operationId": "getBillingLedger",
        "summary": "Query SHA-256 hash-chained billing ledger entries and chain verification status",
        "parameters": [
          {
            "name": "account_id",
            "in": "query",
            "required": false,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": { "description": "Ledger entries and chain integrity check" }
        }
      }
    },
    "/webhooks/creem": {
      "post": {
        "tags": ["Billing"],
        "operationId": "handleCreemWebhook",
        "summary": "Idempotent Creem payment webhook verified with HMAC-SHA256 (CREEM_WEBHOOK_SECRET)",
        "responses": {
          "200": { "description": "Webhook processed idempotently" },
          "400": { "description": "Malformed JSON or missing event_id" },
          "401": { "description": "Invalid HMAC-SHA256 signature" },
          "503": { "description": "CREEM_WEBHOOK_SECRET not configured" }
        }
      }
    },
    "/mcp": {
      "post": {
        "tags": ["MCP"],
        "operationId": "handleMcpJsonRpc",
        "summary": "Native JSON-RPC 2.0 MCP endpoint (harvest_extract, harvest_batch, harvest_diff_watch)",
        "responses": {
          "200": { "description": "JSON-RPC 2.0 response" },
          "400": { "description": "JSON-RPC parse or invalid request error" }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "HealthResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string" },
          "service": { "type": "string" },
          "port": { "type": "integer" },
          "modes": { "type": "array", "items": { "type": "string" } },
          "ammo_cap_micros": { "type": "integer" }
        }
      },
      "HarvestRequest": {
        "type": "object",
        "properties": {
          "url": { "type": "string" },
          "content": { "type": "string" },
          "mode": {
            "type": "string",
            "enum": ["auto", "gov_notices", "sec_filings", "trials_patents", "customs_feeds", "embedded_pdf_schema"],
            "default": "auto"
          },
          "schema": { "type": "object" },
          "tenant_id": { "type": "string" },
          "recheck": { "type": "boolean", "default": false },
          "no_cache": { "type": "boolean", "default": false },
          "pagination": { "type": "boolean", "default": false },
          "max_pages": { "type": "integer", "default": 3 }
        }
      },
      "HarvestReceipt": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "status_code": { "type": "integer" },
          "mode": { "type": "string" },
          "url": { "type": "string" },
          "extracted": { "type": "object" },
          "records": { "type": "array", "items": { "type": "object" } },
          "field_confidence": { "type": "object" },
          "source_line_refs": { "type": "object" },
          "schema_valid": { "type": "boolean" },
          "cache_hit": { "type": "boolean" },
          "cache_tier": { "type": "string" },
          "billed_micros": { "type": "integer" },
          "zero_bill_reason": { "type": ["string", "null"] },
          "sha256_receipt": { "type": "string" }
        }
      },
      "BatchHarvestRequest": {
        "type": "object",
        "required": ["items"],
        "properties": {
          "items": { "type": "array", "items": { "$ref": "#/components/schemas/HarvestRequest" } },
          "max_budget_micros": { "type": "integer", "default": 2000000 },
          "max_workers": { "type": "integer", "default": 4 },
          "tenant_id": { "type": "string" }
        }
      },
      "BatchHarvestResponse": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "total_items": { "type": "integer" },
          "succeeded": { "type": "integer" },
          "billed_micros": { "type": "integer" },
          "results": { "type": "array", "items": { "$ref": "#/components/schemas/HarvestReceipt" } }
        }
      },
      "DiffWatchRequest": {
        "type": "object",
        "required": ["url"],
        "properties": {
          "watch_id": { "type": "string" },
          "url": { "type": "string" },
          "mode": { "type": "string", "default": "auto" },
          "callback_url": { "type": "string" },
          "schema": { "type": "object" },
          "content": { "type": "string" }
        }
      }
    }
  }
}
