{
  "openapi": "3.1.0",
  "info": {
    "title": "Forecast-Anything API",
    "version": "1.0.0",
    "summary": "Deterministic 5-domain statistical time-series forecasting, anomaly detection, and SVG chart engine with SHA-256 receipts.",
    "description": "Polymorphic pure-stdlib time-series forecasting across E-commerce ROP/Safety Stock/EOQ, SaaS MRR/NRR/Runway, Cloud/GPU/LLM Budget Breach Alerts, 100-SKU Concurrent Batch Anomaly Detection (Z-score/IQR/CUSUM), and Rolling Backtest + XML-valid SVG Charts. Every forecast emits a tamper-evident SHA-256 ForecastReceipt. HTTP 422 invalid inputs (empty series, NaN/Inf, non-numeric cells, horizon <= 0, negative seasonal period) are strictly billed $0 (billed_micros = 0, charged_micros = 0). Zero customer data resale guarantee. Software disclaimer: deterministic statistical time-series forecasting software; not securities/investment advice or return guarantee.",
    "contact": {
      "name": "Forecast-Anything Operations",
      "email": "ops@fastmesh.dev",
      "url": "https://forecast.fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://forecast.fastmesh.dev",
      "description": "Public HTTPS Gateway Mount"
    },
    {
      "url": "http://127.0.0.1:9014",
      "description": "Local Runtime Server"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Service liveness, pricing schedule, and readiness probes" },
    { "name": "Forecasting", "description": "Single and batch deterministic 5-domain time-series forecasting & anomaly detection" },
    { "name": "Receipts", "description": "Tamper-evident SHA-256 ForecastReceipt lookup and verification" },
    { "name": "MCP", "description": "Native flat JSON-RPC 2.0 Model Context Protocol endpoint" },
    { "name": "Billing", "description": "Creem HMAC-SHA256 prepaid credit webhook settlement" }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "tags": ["Health"],
        "summary": "Check Forecast-Anything service health",
        "responses": {
          "200": {
            "description": "Service is healthy and ready",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" },
                "example": {
                  "status": "ok",
                  "service": "forecast-anything",
                  "version": "1.0.0",
                  "public_host": "forecast.fastmesh.dev",
                  "port": 9014,
                  "domains": [
                    "ecommerce_rop",
                    "saas_mrr",
                    "cloud_budget",
                    "batch_anomaly",
                    "backtest_svg"
                  ],
                  "ledger_enabled": true,
                  "mcp_enabled": true,
                  "timestamp_utc": "2026-12-02T12:00:00Z"
                }
              }
            }
          }
        }
      }
    },
    "/forecast": {
      "post": {
        "operationId": "forecastSingle",
        "tags": ["Forecasting"],
        "summary": "Run deterministic forecast across any of the 5 supported domains",
        "description": "Returns a deterministic ForecastReceipt with P50/P80/P90/P95/P99 intervals, domain alerts, optional valid XML <svg> chart, and SHA-256 attestation. Strictly bills $0 (`billed_micros = 0`, `charged_micros = 0`) on HTTP 422 invalid input 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 console)."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ForecastRequest" },
              "examples": {
                "ecommerce_rop_order_now": {
                  "summary": "Domain 1: E-commerce SKU Safety Stock, EOQ & ROP Alert",
                  "value": {
                    "domain": "ecommerce_rop",
                    "target": "SKU-WINTER-BOOT-42",
                    "series": [42, 45, 48, 52, 60, 58, 44, 46, 49, 53, 62, 61, 47, 50, 54, 65, 63, 48, 52, 57, 68],
                    "horizon": 30,
                    "seasonal_period": 7,
                    "lead_time_days": 10,
                    "service_level": 0.95,
                    "on_hand_units": 380,
                    "order_cost": 85.0,
                    "holding_cost_per_unit_year": 4.5
                  }
                },
                "saas_mrr_runway": {
                  "summary": "Domain 2: SaaS MRR, ARR, NRR & Cash Runway Forecast",
                  "value": {
                    "domain": "saas_mrr",
                    "target": "B2B-COHORT-CORE",
                    "series": [82000, 86500, 91200, 95800, 101000, 106400, 112000, 118500, 124000, 130200, 136800, 143500],
                    "horizon": 12,
                    "cash_balance": 950000,
                    "monthly_burn": 165000
                  }
                },
                "cloud_budget_breach": {
                  "summary": "Domain 3: Cloud / GPU / LLM Spend P50/P90/P99 & Breach Date",
                  "value": {
                    "domain": "cloud_budget",
                    "target": "gpu-inference-cluster-us",
                    "series": [1820, 1910, 1980, 2050, 2110, 1340, 1290, 1950, 2040, 2120, 2210, 2290, 1410, 1360, 2150, 2260, 2340, 2410, 2490, 1520, 1460],
                    "horizon": 10,
                    "seasonal_period": 7,
                    "monthly_budget_cap": 55000
                  }
                },
                "backtest_svg_chart": {
                  "summary": "Domain 5: Rolling Holdout Backtest + Valid XML SVG Chart",
                  "value": {
                    "domain": "backtest_svg",
                    "target": "daily-orders-benchmark",
                    "series": [105, 118, 125, 132, 140, 128, 112, 110, 124, 131, 139, 148, 134, 117, 115, 129, 137, 145, 154, 141, 122],
                    "horizon": 14,
                    "seasonal_period": 7,
                    "holdout_size": 7
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Deterministic forecast completed with signed ForecastReceipt",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ForecastReceipt" }
              }
            }
          },
          "422": {
            "description": "Invalid input (empty series, NaN/Inf, non-numeric, horizon <= 0, negative seasonal period) — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" },
                "example": {
                  "ok": false,
                  "error": "invalid_input_422",
                  "detail": "Series must be non-empty numeric values without NaN or Inf.",
                  "billed_micros": 0,
                  "charged_micros": 0,
                  "zero_bill_reason": "invalid_input_422"
                }
              }
            }
          }
        }
      }
    },
    "/forecast/batch": {
      "post": {
        "operationId": "forecastBatch",
        "tags": ["Forecasting"],
        "summary": "Run concurrent multi-series CSV/JSON batch forecasting and anomaly detection with hard budget cap",
        "description": "Executes concurrent forecasts and residual Z-score/IQR/CUSUM change-point detection bounded by `max_budget_micros`. Invalid 422 rows and budget-skipped rows are strictly billed $0.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BatchForecastRequest" },
              "example": {
                "tenant_id": "tenant_demo",
                "max_budget_micros": 300000,
                "items": [
                  { "domain": "ecommerce_rop", "target": "SKU-101", "series": [30, 32, 34, 31, 35, 38, 33, 36, 39, 41, 37, 40, 43, 45], "horizon": 14, "on_hand_units": 200, "lead_time_days": 7 },
                  { "domain": "cloud_budget", "target": "llm-tokens", "series": [400, 410, 420, 415, 430, 290, 280, 440, 455, 465, 470, 485, 310, 300], "horizon": 10, "monthly_budget_cap": 11000 }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Batch execution summary and per-item ForecastReceipts",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BatchForecastResponse" }
              }
            }
          },
          "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 ForecastReceipt and verify its SHA-256 attestation",
        "parameters": [
          {
            "name": "receipt_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "Unique ForecastReceipt 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/ForecastReceipt" }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Receipt ID not found",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/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 forecast_series, forecast_batch_csv, forecast_verify_receipt, and forecast_catalog.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/McpJsonRpcRequest" },
              "examples": {
                "initialize": {
                  "summary": "MCP Initialize Handshake",
                  "value": {
                    "jsonrpc": "2.0",
                    "id": 1,
                    "method": "initialize",
                    "params": {
                      "protocolVersion": "2025-03-26",
                      "clientInfo": { "name": "agent-client", "version": "1.0.0" }
                    }
                  }
                },
                "tools_list": {
                  "summary": "List Flat MCP Tools",
                  "value": {
                    "jsonrpc": "2.0",
                    "id": 2,
                    "method": "tools/list",
                    "params": {}
                  }
                },
                "tools_call_forecast_series": {
                  "summary": "Invoke forecast_series Tool",
                  "value": {
                    "jsonrpc": "2.0",
                    "id": 3,
                    "method": "tools/call",
                    "params": {
                      "name": "forecast_series",
                      "arguments": {
                        "domain": "ecommerce_rop",
                        "target": "SKU-101",
                        "series": [40, 42, 44, 46, 50, 48, 39, 41, 43, 45, 47, 52, 49, 40],
                        "horizon": 30,
                        "lead_time_days": 7,
                        "on_hand_units": 260
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC 2.0 response payload",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/McpJsonRpcResponse" }
              }
            }
          },
          "202": {
            "description": "Accepted notification (notifications/initialized without id)"
          }
        }
      }
    },
    "/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`, 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_forecast_01",
                "eventType": "checkout.completed",
                "object": {
                  "product_id": "prod_forecast_anything_growth_79",
                  "amount": 7900,
                  "currency": "USD",
                  "customer": { "email": "buyer@example.com" },
                  "metadata": { "tenant_id": "tenant_acme", "sku_id": "sku_forecast_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" },
                    "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"
          }
        }
      }
    }
  },
  "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" } },
          "pricing": { "type": "object" },
          "cache_stats": { "type": "object" },
          "ledger_enabled": { "type": "boolean" },
          "mcp_enabled": { "type": "boolean" },
          "timestamp_utc": { "type": "string", "format": "date-time" }
        }
      },
      "ForecastRequest": {
        "type": "object",
        "required": ["domain"],
        "properties": {
          "domain": {
            "type": "string",
            "enum": [
              "ecommerce_rop",
              "saas_mrr",
              "cloud_budget",
              "batch_anomaly",
              "backtest_svg"
            ]
          },
          "target": { "type": "string" },
          "series": { "type": "array", "items": { "type": ["number", "null"] } },
          "csv_text": { "type": "string" },
          "horizon": { "type": "integer", "minimum": 1 },
          "seasonal_period": { "type": "integer", "minimum": 1 },
          "recheck": { "type": "boolean", "default": false },
          "no_cache": { "type": "boolean", "default": false },
          "tenant_id": { "type": "string" },
          "payload": { "type": "object" },
          "options": { "type": "object" }
        }
      },
      "ForecastReceipt": {
        "type": "object",
        "required": [
          "receipt_id",
          "domain",
          "verdict",
          "payload_sha256",
          "sha256_attestation",
          "charged_micros",
          "billed_micros",
          "base_price_micros",
          "timestamp_utc"
        ],
        "properties": {
          "ok": { "type": "boolean" },
          "http_status": { "type": "integer" },
          "receipt_id": { "type": "string" },
          "domain": {
            "type": "string",
            "enum": [
              "ecommerce_rop",
              "saas_mrr",
              "cloud_budget",
              "batch_anomaly",
              "backtest_svg"
            ]
          },
          "target": { "type": "string" },
          "verdict": { "type": "string", "enum": ["VERIFIED", "ALERT", "INCONCLUSIVE"] },
          "confidence": { "type": "string" },
          "checks": { "type": "array", "items": { "type": "object" } },
          "evidence_sources": { "type": "array", "items": { "type": "object" } },
          "details": { "type": "object" },
          "payload_sha256": { "type": "string" },
          "sha256_attestation": { "type": "string" },
          "charged_micros": { "type": "integer", "minimum": 0 },
          "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" }
        }
      },
      "BatchForecastRequest": {
        "type": "object",
        "properties": {
          "items": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ForecastRequest" }
          },
          "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 }
        }
      },
      "BatchForecastResponse": {
        "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/ForecastReceipt" }
              }
            }
          }
        }
      },
      "ZeroBillErrorResponse": {
        "type": "object",
        "required": ["ok", "billed_micros"],
        "properties": {
          "ok": { "type": "boolean" },
          "error": { "type": "string" },
          "detail": { "type": "string" },
          "billed_micros": { "type": "integer", "enum": [0] },
          "charged_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" }
        }
      }
    }
  }
}
