{
  "openapi": "3.1.0",
  "info": {
    "title": "Price-Anything Deterministic Microeconomics & Pricing Optimization API",
    "version": "1.0.0",
    "summary": "Polymorphic 5-domain pricing optimization engine with decoy tier synthesis, AI credit-pack bundling, OLS log-log elasticity, Van Westendorp PSM, and global PPP localization.",
    "description": "Deterministic pricing engine hosted at https://price.fastmesh.dev. Returns recomputable gross-margin lift projections, optimal price grids, decoy tier structures, and copyable JSON + HTML pricing cards. Enforces strict $0 billing on HTTP 422 invalid inputs and errors.",
    "contact": {
      "name": "Price-Anything Engineering",
      "url": "https://price.fastmesh.dev",
      "email": "ops@fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://price.fastmesh.dev",
      "description": "Production public endpoint"
    },
    {
      "url": "http://127.0.0.1:9004",
      "description": "Local upstream server"
    }
  ],
  "tags": [
    {
      "name": "Optimization",
      "description": "Polymorphic 5-domain deterministic pricing optimization endpoints"
    },
    {
      "name": "Batch",
      "description": "Concurrent multi-SKU batch pricing solver with hard budget lock"
    },
    {
      "name": "MCP",
      "description": "Native JSON-RPC 2.0 Model Context Protocol endpoint"
    },
    {
      "name": "Billing",
      "description": "Prepaid credit packs ($19/$79/$199), credit balance, and Creem HMAC-SHA256 webhooks"
    },
    {
      "name": "System",
      "description": "Health, discovery, and OpenAPI schema endpoints"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "tags": ["System"],
        "operationId": "getHealth",
        "summary": "Service liveness and capability snapshot",
        "responses": {
          "200": {
            "description": "Service is healthy",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["status", "service"],
                  "properties": {
                    "status": { "type": "string", "enum": ["ok"] },
                    "service": { "type": "string", "const": "c04_price_anything" },
                    "version": { "type": "string" },
                    "domains": {
                      "type": "array",
                      "items": { "type": "string" }
                    },
                    "ledger": { "type": "object" },
                    "cache": { "type": "object" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/optimize": {
      "post": {
        "tags": ["Optimization"],
        "operationId": "optimizePricing",
        "summary": "Run deterministic pricing optimization across any of the 5 supported domains",
        "description": "Dispatch a pricing optimization request by specifying `domain` (`saas_tiers`, `ai_credits`, `ecom_elasticity`, `van_westendorp`, or `ppp_global`). Returns gross-margin lift, optimal price grid, decoy tier structures, and embeddable JSON/HTML pricing cards. Strictly returns HTTP 422 with `billable: false` and `charged_micros: 0` on invalid input.",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Optional tenant identifier for prepaid credit ledger and cache recheck pricing."
          },
          {
            "name": "X-API-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Optional prepaid API key."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/OptimizeRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Deterministic optimization result",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/OptimizeResponse" }
              }
            }
          },
          "422": {
            "description": "Validation error — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/api/v1/optimize/{domain}": {
      "post": {
        "tags": ["Optimization"],
        "operationId": "optimizePricingByDomain",
        "summary": "Run pricing optimization for a specific path domain",
        "parameters": [
          {
            "name": "domain",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "saas_tiers",
                "ai_credits",
                "ecom_elasticity",
                "van_westendorp",
                "ppp_global"
              ]
            },
            "description": "Pricing optimization domain"
          },
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": true
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Deterministic domain optimization result",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/OptimizeResponse" }
              }
            }
          },
          "422": {
            "description": "Validation error or unsupported domain — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/api/v1/batch": {
      "post": {
        "tags": ["Batch"],
        "operationId": "optimizeBatch",
        "summary": "Concurrent multi-SKU batch pricing optimization with hard budget lock",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BatchRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Batch execution summary with per-item results and exact billed micro-USD",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BatchResponse" }
              }
            }
          },
          "422": {
            "description": "Invalid batch envelope — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/mcp": {
      "post": {
        "tags": ["MCP"],
        "operationId": "invokeMcpJsonRpc",
        "summary": "Native JSON-RPC 2.0 MCP server endpoint",
        "description": "Supports `initialize`, `notifications/initialized`, `ping`, `tools/list`, and `tools/call` across all 5 pricing domains and batch optimization.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/JsonRpcRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC 2.0 response payload",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JsonRpcResponse" }
              }
            }
          },
          "202": {
            "description": "Notification acknowledged"
          }
        }
      }
    },
    "/webhooks/creem": {
      "post": {
        "tags": ["Billing"],
        "operationId": "handleCreemWebhook",
        "summary": "Idempotent Creem HMAC-SHA256 payment webhook",
        "description": "Verifies `creem-signature` HMAC-SHA256 against `CREEM_WEBHOOK_SECRET` and idempotently credits prepaid optimization credits in SQLite WAL storage.",
        "parameters": [
          {
            "name": "creem-signature",
            "in": "header",
            "required": true,
            "schema": { "type": "string" },
            "description": "HMAC-SHA256 hex digest of raw request body"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": true
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Webhook verified and idempotently processed",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "duplicate": { "type": "boolean" },
                    "event_id": { "type": "string" },
                    "tenant_id": { "type": "string" },
                    "sku_id": { "type": "string" },
                    "credited_micros": { "type": "integer" },
                    "balance_micros": { "type": "integer" }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Invalid or missing creem-signature HMAC digest"
          },
          "400": {
            "description": "Malformed JSON payload"
          }
        }
      }
    },
    "/api/v1/credits/balance": {
      "get": {
        "tags": ["Billing"],
        "operationId": "getCreditsBalance",
        "summary": "Retrieve tenant prepaid credit balance and recent ledger activity",
        "parameters": [
          {
            "name": "tenant_id",
            "in": "query",
            "required": false,
            "schema": { "type": "string" },
            "description": "Tenant identifier (or supply via X-Tenant-Id header)"
          },
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Tenant credit balance snapshot",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "tenant_id": { "type": "string" },
                    "balance_micros": { "type": "integer" },
                    "balance_usd": { "type": "number" },
                    "credits_remaining": { "type": "number" },
                    "total_credited_micros": { "type": "integer" },
                    "total_debited_micros": { "type": "integer" },
                    "recent_entries": {
                      "type": "array",
                      "items": { "type": "object" }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/checkout": {
      "post": {
        "tags": ["Billing"],
        "operationId": "createCheckout",
        "summary": "Create Creem prepaid credit-pack checkout session ($19 Starter, $79 Growth, $199 Scale)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "sku_id": {
                    "type": "string",
                    "enum": [
                      "price_anything_starter_19",
                      "price_anything_growth_79",
                      "price_anything_scale_199",
                      "starter",
                      "growth",
                      "scale"
                    ]
                  },
                  "tier": { "type": "string" },
                  "tenant_id": { "type": "string" },
                  "customer_email": { "type": "string" },
                  "success_url": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Checkout session created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "checkout_session_id": { "type": "string" },
                    "sku_id": { "type": "string" },
                    "product_id": { "type": "string" },
                    "price_usd": { "type": "number" },
                    "credits_micros": { "type": "integer" },
                    "checkout_url": { "type": "string" }
                  }
                }
              }
            }
          },
          "422": {
            "description": "Unknown SKU — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "OptimizeRequest": {
        "type": "object",
        "required": ["domain"],
        "additionalProperties": true,
        "properties": {
          "domain": {
            "type": "string",
            "enum": [
              "saas_tiers",
              "ai_credits",
              "ecom_elasticity",
              "van_westendorp",
              "ppp_global"
            ]
          },
          "unit_cogs": { "type": "number", "minimum": 0 },
          "competitor_price": { "type": "number", "exclusiveMinimum": 0 },
          "tenant_id": { "type": "string" },
          "no_cache": { "type": "boolean", "default": false }
        }
      },
      "OptimizeResponse": {
        "type": "object",
        "required": ["ok", "service", "domain", "billable", "charged_micros", "cache_hit", "receipt_sha256", "result"],
        "additionalProperties": true,
        "properties": {
          "ok": { "type": "boolean" },
          "service": { "type": "string", "const": "c04_price_anything" },
          "version": { "type": "string" },
          "domain": { "type": "string" },
          "billable": { "type": "boolean" },
          "charged_micros": { "type": "integer", "minimum": 0 },
          "cache_hit": { "type": "boolean" },
          "cache_tier": { "type": "string" },
          "cache_backend": { "type": "string" },
          "cache_key_hash": { "type": "string" },
          "receipt_sha256": { "type": "string" },
          "timestamp_utc": { "type": "string" },
          "result": {
            "type": "object",
            "properties": {
              "projected_metrics": { "type": "object" },
              "pricing_table_json": { "type": "object" },
              "pricing_table_html": { "type": "string" },
              "warnings": { "type": "array", "items": {} }
            }
          }
        }
      },
      "ZeroBillErrorResponse": {
        "type": "object",
        "required": ["ok", "billable", "charged_micros", "error"],
        "properties": {
          "ok": { "type": "boolean", "const": false },
          "billable": { "type": "boolean", "const": false },
          "charged_micros": { "type": "integer", "const": 0 },
          "cache_hit": { "type": "boolean", "const": false },
          "error": {
            "oneOf": [
              { "type": "string" },
              {
                "type": "object",
                "properties": {
                  "code": { "type": "string" },
                  "field": { "type": "string" },
                  "message": { "type": "string" }
                }
              }
            ]
          }
        }
      },
      "BatchRequest": {
        "type": "object",
        "required": ["items"],
        "properties": {
          "items": {
            "type": "array",
            "minItems": 1,
            "items": { "$ref": "#/components/schemas/OptimizeRequest" }
          },
          "max_budget_micros": { "type": "integer", "minimum": 0 },
          "max_workers": { "type": "integer", "minimum": 1 },
          "tenant_id": { "type": "string" }
        }
      },
      "BatchResponse": {
        "type": "object",
        "required": ["ok", "total_items", "succeeded", "total_charged_micros", "results"],
        "properties": {
          "ok": { "type": "boolean" },
          "total_items": { "type": "integer" },
          "succeeded": { "type": "integer" },
          "failed_422": { "type": "integer" },
          "skipped_budget": { "type": "integer" },
          "total_charged_micros": { "type": "integer" },
          "budget_remaining_micros": { "type": "integer" },
          "results": {
            "type": "array",
            "items": { "type": "object" }
          }
        }
      },
      "JsonRpcRequest": {
        "type": "object",
        "required": ["jsonrpc", "method"],
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer", "null"] },
          "method": { "type": "string" },
          "params": { "type": "object" }
        }
      },
      "JsonRpcResponse": {
        "type": "object",
        "required": ["jsonrpc"],
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer", "null"] },
          "result": { "type": "object" },
          "error": {
            "type": "object",
            "properties": {
              "code": { "type": "integer" },
              "message": { "type": "string" },
              "data": {}
            }
          }
        }
      }
    }
  }
}
