{
  "openapi": "3.1.0",
  "info": {
    "title": "Scrape-Anything API (W4-C05)",
    "version": "1.0.0",
    "summary": "Multi-Tier Adaptive Structured Extraction & Anti-Fake-200 Engine ($0 on Miss, Fake-200, or Schema Fail)",
    "description": "Deterministic structured extraction across 5 formats: (1) JS-embedded hydration payloads (__NEXT_DATA__, __NUXT__, JSON-LD, OpenGraph), (2) complex HTML tables with rowspan/colspan/multi-level headers & price cards, (3) pure-stream binary PDF (FlateDecode text, tables, /AcroForm fields), (4) recursive robots.txt, sitemapindex, urlset, RSS 2.0, Atom & XML feeds, and (5) multi-sheet binary XLSX & CSV/TSV mapped to strict JSON Schema. Enforces strict SSRF blocking (HTTP 422, billed_micros=0), anti-fake-200 CAPTCHA/login-wall gating (billed_micros=0), cross-tenant adaptive 7d-30d verified-hit archive caching (100% new tenant / 10% same-tenant repeat), SHA-256 verification receipts, and zero customer data resale.",
    "contact": {
      "name": "Scrape-Anything Operations",
      "email": "ops@fastmesh.dev",
      "url": "https://scrape.fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://scrape.fastmesh.dev",
      "description": "Production HTTPS Gateway"
    },
    {
      "url": "http://127.0.0.1:9005",
      "description": "Local Upstream Instance"
    }
  ],
  "tags": [
    {
      "name": "System",
      "description": "Health, OpenAPI discovery, and transparent pricing"
    },
    {
      "name": "Extraction",
      "description": "Adaptive 5-format structured extraction, sitemap crawl, and concurrent batch endpoints"
    },
    {
      "name": "MCP",
      "description": "Native JSON-RPC 2.0 Model Context Protocol endpoint"
    },
    {
      "name": "Billing",
      "description": "Creem.io HMAC-SHA256 webhook receiver for idempotent prepaid credit fulfillment"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "tags": ["System"],
        "operationId": "getHealth",
        "summary": "Service liveness, archive cache telemetry, and zero-on-miss runtime metadata",
        "responses": {
          "200": {
            "description": "Service is healthy",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HealthResponse"
                }
              }
            }
          }
        }
      }
    },
    "/openapi.json": {
      "get": {
        "tags": ["System"],
        "operationId": "getOpenApiSpec",
        "summary": "Return this OpenAPI 3.1.0 machine-readable schema",
        "responses": {
          "200": {
            "description": "OpenAPI 3.1.0 JSON document",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/v1/pricing": {
      "get": {
        "tags": ["System"],
        "operationId": "getPricing",
        "summary": "Return prepaid Creem credit packs ($15/$59/$199), micro-USD unit prices, and $0-on-miss SLA",
        "responses": {
          "200": {
            "description": "Prepaid SKU catalog and micro-USD pricing schedule",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PricingResponse"
                }
              }
            }
          }
        }
      }
    },
    "/v1/extract": {
      "post": {
        "tags": ["Extraction"],
        "operationId": "extractStructured",
        "summary": "Extract structured data from a public URL or inline text/base64 document with strict JSON Schema validation ($0.015 verified; $0 on fake-200, CAPTCHA, schema fail, or SSRF 422)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ExtractRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Structured extraction envelope (billed_micros=0 if blocked as fake-200/CAPTCHA, empty, encrypted PDF, or schema_valid=false)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExtractResponse"
                }
              }
            }
          },
          "400": {
            "description": "Malformed JSON request body (billed_micros=0)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "422": {
            "description": "SSRF blocked (private/loopback/IPv6-local/cloud-metadata/unsafe redirect) or invalid input (billed_micros=0)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExtractResponse"
                }
              }
            }
          }
        }
      }
    },
    "/v1/scrape": {
      "post": {
        "tags": ["Extraction"],
        "operationId": "scrapeStructuredAlias",
        "summary": "Alias of POST /v1/extract with identical request/response contract and $0-on-miss SLA",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ExtractRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Structured extraction envelope (billed_micros=0 on fake-200, CAPTCHA, schema fail, or empty result)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExtractResponse"
                }
              }
            }
          },
          "422": {
            "description": "SSRF blocked or invalid input (billed_micros=0)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExtractResponse"
                }
              }
            }
          }
        }
      }
    },
    "/v1/sitemap": {
      "post": {
        "tags": ["Extraction"],
        "operationId": "discoverSitemap",
        "summary": "Recursively crawl public robots.txt, sitemapindex, urlset, RSS 2.0, Atom, or XML feeds with regex include/exclude filters and per-hop SSRF validation ($0.010 verified; $0 on empty/blocked/SSRF)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SitemapRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Discovered URLs, sitemap traversal metadata, and SHA-256 receipt",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SitemapResponse"
                }
              }
            }
          },
          "422": {
            "description": "SSRF blocked or invalid URL (billed_micros=0)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/v1/batch": {
      "post": {
        "tags": ["Extraction"],
        "operationId": "batchExtract",
        "summary": "Concurrent multi-URL/document structured extraction with per-row $0-on-miss protection and hard max_cost_micros budget cap",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BatchRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Ordered batch results with per-item $0 protection and cumulative micro-USD accounting",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BatchResponse"
                }
              }
            }
          },
          "422": {
            "description": "Invalid or empty items list (billed_micros=0)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/mcp": {
      "post": {
        "tags": ["MCP"],
        "operationId": "mcpJsonRpc",
        "summary": "Native JSON-RPC 2.0 Model Context Protocol endpoint (`initialize`, `notifications/initialized`, `tools/list`, `tools/call` for `scrape_extract`, `scrape_table`, `scrape_pdf`, `scrape_sitemap`, `scrape_spreadsheet`, `scrape_batch`)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/McpJsonRpcRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC 2.0 MCP response envelope",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/McpJsonRpcResponse"
                }
              }
            }
          },
          "202": {
            "description": "JSON-RPC 2.0 notification accepted (no response body)"
          }
        }
      }
    },
    "/webhooks/creem": {
      "post": {
        "tags": ["Billing"],
        "operationId": "creemWebhook",
        "summary": "HMAC-SHA256 verified idempotent Creem.io prepaid credit fulfillment webhook (accepts X-Creem-Signature or creem-signature)",
        "parameters": [
          {
            "name": "X-Creem-Signature",
            "in": "header",
            "required": false,
            "description": "HMAC-SHA256 hex digest (optionally prefixed with sha256=) computed with CREEM_WEBHOOK_SECRET",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "creem-signature",
            "in": "header",
            "required": false,
            "description": "Standard Creem webhook HMAC-SHA256 signature header",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreemWebhookEvent"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Webhook processed or idempotently replayed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreemWebhookResult"
                }
              }
            }
          },
          "400": {
            "description": "Malformed JSON payload"
          },
          "401": {
            "description": "Invalid X-Creem-Signature / creem-signature HMAC-SHA256 digest"
          },
          "503": {
            "description": "CREEM_WEBHOOK_SECRET is not configured on this instance"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "HealthResponse": {
        "type": "object",
        "required": ["status", "engine_id", "service", "zero_on_miss"],
        "properties": {
          "status": {
            "type": "string",
            "example": "ok"
          },
          "engine_id": {
            "type": "string",
            "example": "W4-C05"
          },
          "service": {
            "type": "string",
            "example": "scrape-anything"
          },
          "port": {
            "type": "integer",
            "example": 9005
          },
          "zero_on_miss": {
            "type": "boolean",
            "example": true
          },
          "cache_stats": {
            "type": "object"
          }
        }
      },
      "PricingResponse": {
        "type": "object",
        "properties": {
          "schema_version": { "type": "string" },
          "engine_id": { "type": "string", "example": "W4-C05" },
          "product_name": { "type": "string", "example": "Scrape-Anything" },
          "currency": { "type": "string", "example": "USD" },
          "zero_on_miss_sla": { "type": "object" },
          "unit_prices_micros": { "type": "object" },
          "skus": {
            "type": "array",
            "items": { "type": "object" }
          }
        }
      },
      "ExtractRequest": {
        "type": "object",
        "description": "Provide at least one of `url`, `content_text`, or `content_base64`.",
        "properties": {
          "url": {
            "type": "string",
            "format": "uri",
            "example": "https://example.com"
          },
          "content_text": {
            "type": "string",
            "description": "Inline UTF-8 HTML, XML, CSV, TSV, or JSON payload"
          },
          "content_base64": {
            "type": "string",
            "description": "Inline base64-encoded binary PDF or XLSX payload"
          },
          "content_type": {
            "type": "string",
            "example": "text/html"
          },
          "format_hint": {
            "type": "string",
            "enum": ["auto", "js_embedded", "html_table", "pdf", "sitemap_xml", "spreadsheet"],
            "default": "auto"
          },
          "json_schema": {
            "type": "object",
            "description": "Strict JSON Schema applied to extracted data; validation failure guarantees billed_micros=0"
          },
          "tenant_id": {
            "type": "string",
            "default": "public"
          },
          "max_cost_micros": {
            "type": "integer",
            "minimum": 0,
            "example": 50000
          },
          "allow_cache": {
            "type": "boolean",
            "default": true
          }
        }
      },
      "ExtractResponse": {
        "type": "object",
        "required": [
          "ok",
          "status",
          "format_detected",
          "schema_valid",
          "validation_errors",
          "blocked_reason",
          "billed_micros",
          "upstream_cost_micros",
          "cache_hit",
          "billing_tier",
          "receipt_sha256",
          "data",
          "markdown"
        ],
        "properties": {
          "ok": { "type": "boolean" },
          "status": {
            "type": "string",
            "example": "verified_hit"
          },
          "format_detected": {
            "type": "string",
            "example": "html_table"
          },
          "schema_valid": { "type": "boolean" },
          "validation_errors": {
            "type": "array",
            "items": { "type": "string" }
          },
          "blocked_reason": {
            "type": ["string", "null"]
          },
          "billed_micros": {
            "type": "integer",
            "minimum": 0,
            "description": "Exact micro-USD billed (0 on fake-200, CAPTCHA, schema fail, empty result, encrypted PDF, or SSRF 422)"
          },
          "upstream_cost_micros": {
            "type": "integer",
            "minimum": 0
          },
          "cache_hit": { "type": "boolean" },
          "billing_tier": {
            "type": "string",
            "example": "verified_live"
          },
          "receipt_sha256": {
            "type": "string",
            "description": "Deterministic SHA-256 verification fingerprint"
          },
          "data": {
            "description": "Structured extracted payload (object or array or null on failure)"
          },
          "markdown": {
            "type": "string",
            "description": "Clean structured Markdown representation of the extracted content"
          }
        }
      },
      "SitemapRequest": {
        "type": "object",
        "required": ["url"],
        "properties": {
          "url": {
            "type": "string",
            "format": "uri",
            "example": "https://example.com/sitemap.xml"
          },
          "include_pattern": { "type": "string" },
          "exclude_pattern": { "type": "string" },
          "max_urls": {
            "type": "integer",
            "minimum": 1,
            "maximum": 5000,
            "default": 200
          },
          "max_depth": {
            "type": "integer",
            "minimum": 0,
            "maximum": 10,
            "default": 3
          },
          "tenant_id": {
            "type": "string",
            "default": "public"
          }
        }
      },
      "SitemapResponse": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "status": { "type": "string" },
          "format_detected": { "type": "string", "example": "sitemap_xml" },
          "urls": {
            "type": "array",
            "items": { "type": "string" }
          },
          "url_count": { "type": "integer" },
          "sitemaps_visited": {
            "type": "array",
            "items": { "type": "string" }
          },
          "disallow_rules": {
            "type": "array",
            "items": { "type": "string" }
          },
          "blocked_reason": { "type": ["string", "null"] },
          "billed_micros": { "type": "integer", "minimum": 0 },
          "upstream_cost_micros": { "type": "integer", "minimum": 0 },
          "cache_hit": { "type": "boolean" },
          "billing_tier": { "type": "string" },
          "receipt_sha256": { "type": "string" }
        }
      },
      "BatchRequest": {
        "type": "object",
        "required": ["items"],
        "properties": {
          "items": {
            "type": "array",
            "minItems": 1,
            "items": {
              "oneOf": [
                { "type": "string", "format": "uri" },
                { "$ref": "#/components/schemas/ExtractRequest" }
              ]
            }
          },
          "json_schema": { "type": "object" },
          "tenant_id": { "type": "string", "default": "public" },
          "max_cost_micros": {
            "type": "integer",
            "minimum": 0
          }
        }
      },
      "BatchResponse": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "status": { "type": "string" },
          "total_items": { "type": "integer" },
          "verified_items": { "type": "integer" },
          "zero_billed_items": { "type": "integer" },
          "billed_micros": { "type": "integer", "minimum": 0 },
          "upstream_cost_micros": { "type": "integer", "minimum": 0 },
          "budget_cap_reached": { "type": "boolean" },
          "receipt_sha256": { "type": "string" },
          "results": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ExtractResponse" }
          }
        }
      },
      "McpJsonRpcRequest": {
        "type": "object",
        "required": ["jsonrpc", "method"],
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer", "null"] },
          "method": {
            "type": "string",
            "enum": ["initialize", "notifications/initialized", "ping", "tools/list", "tools/call"]
          },
          "params": { "type": "object" }
        }
      },
      "McpJsonRpcResponse": {
        "type": "object",
        "required": ["jsonrpc"],
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer", "null"] },
          "result": { "type": "object" },
          "error": { "type": "object" }
        }
      },
      "CreemWebhookEvent": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "example": "evt_01JXYZ..." },
          "eventType": {
            "type": "string",
            "enum": ["checkout.completed", "refund.created", "dispute.created"]
          },
          "object": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "product": { "type": "object" },
              "order": { "type": "object" },
              "customer": { "type": "object" },
              "metadata": {
                "type": "object",
                "properties": {
                  "tenant_id": { "type": "string" },
                  "sku_id": {
                    "type": "string",
                    "enum": ["sku_scrape_starter_15", "sku_scrape_growth_59", "sku_scrape_scale_199"]
                  }
                }
              }
            }
          }
        }
      },
      "CreemWebhookResult": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "status_code": { "type": "integer", "example": 200 },
          "event_id": { "type": "string" },
          "event_type": { "type": "string" },
          "tenant_id": { "type": "string" },
          "sku_id": { "type": "string" },
          "credit_micros_delta": { "type": "integer" },
          "balance_micros": { "type": "integer" },
          "duplicate": { "type": "boolean" },
          "idempotent": { "type": "boolean" },
          "payer_sha256_12": { "type": "string" },
          "payload_sha256": { "type": "string" }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean", "const": false },
          "status": { "type": "string" },
          "blocked_reason": { "type": ["string", "null"] },
          "error": { "type": "string" },
          "billed_micros": { "type": "integer", "const": 0 }
        }
      }
    }
  }
}
