{
  "openapi": "3.1.0",
  "info": {
    "title": "Science-Anything API",
    "version": "1.0.0",
    "summary": "Polymorphic 5-source scientific evidence compiler with provenance verification, reproducible protocol checklists, and $0-on-miss billing.",
    "description": "Compile verifiable structured comparison matrices and reproducible protocol checklists across Literature (EuropePMC/PubMed/arXiv/Crossref), Compounds (PubChem PUG REST), Clinical Trials (ClinicalTrials.gov v2), Regulatory & Safety (OpenFDA), and Patent Claims & White-Space (USPTO/PatentsView). Invalid inputs (HTTP 422), empty results, and misses strictly bill $0 (billed_micros = 0, charged_micros = 0). Hard $2.00 (2,000,000 micro-USD) external micro-ammo spend cap. Zero customer query or research data resale guarantee. Research compilation only, not medical advice.",
    "contact": {
      "name": "Science-Anything Operations",
      "email": "ops@fastmesh.dev",
      "url": "https://science.fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://science.fastmesh.dev",
      "description": "Public HTTPS Gateway Mount"
    },
    {
      "url": "http://127.0.0.1:8809",
      "description": "Local Runtime Server"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Service liveness and readiness probes" },
    { "name": "Compile", "description": "Single and batch 5-source scientific compilation" },
    { "name": "Export", "description": "JSON, CSV, and Markdown comparison matrix and protocol checklist export" },
    { "name": "MCP", "description": "Native flat JSON-RPC 2.0 Model Context Protocol endpoint" },
    { "name": "Billing", "description": "Creem HMAC-SHA256 prepaid credit webhook settlement, balance queries, and SKU packs" }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "tags": ["Health"],
        "summary": "Check Science-Anything service health",
        "responses": {
          "200": {
            "description": "Service is healthy and ready",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" }
              }
            }
          }
        }
      }
    },
    "/compile": {
      "post": {
        "operationId": "compileScientificEvidence",
        "tags": ["Compile"],
        "summary": "Compile verifiable scientific evidence across 5 public sources (or auto mode)",
        "description": "Accepts a compound, target, NCT ID, patent ID, PMID/DOI, or research query. Returns structured comparison matrix, reproducible protocol checklist, verified provenance IDs, and SHA-256 attestation. Empty hits and HTTP 422 invalid queries strictly bill $0 (billed_micros = 0).",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CompileRequest" },
              "examples": {
                "compound_aspirin": {
                  "summary": "Compound Lipinski + Bioassay lookup via PubChem",
                  "value": { "query": "aspirin", "source_mode": "compound" }
                },
                "trials_nct": {
                  "summary": "ClinicalTrials.gov v2 study protocol & endpoints",
                  "value": { "query": "NCT04280705", "source_mode": "trials" }
                },
                "literature_pmid": {
                  "summary": "PubMed / EuropePMC reproducible protocol compilation",
                  "value": { "query": "PMID:32123347", "source_mode": "literature" }
                },
                "fda_faers": {
                  "summary": "OpenFDA label, recall, and FAERS adverse event matrix",
                  "value": { "query": "semaglutide", "source_mode": "fda" }
                },
                "patent_claims": {
                  "summary": "USPTO / PatentsView independent claim white-space audit",
                  "value": { "query": "US10702512B2", "source_mode": "patent" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Compiled scientific comparison matrix and protocol checklist",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CompileResponse" }
              }
            }
          },
          "422": {
            "description": "Invalid or empty/junk query ($0 billed, billed_micros = 0)"
          }
        }
      }
    },
    "/api/compile": {
      "post": {
        "operationId": "compileScientificEvidenceApi",
        "tags": ["Compile"],
        "summary": "Compile verifiable scientific evidence across 5 public sources (canonical /api alias)",
        "description": "Alias for POST /compile. Returns structured comparison matrix, reproducible protocol checklist, verified provenance IDs, and SHA-256 attestation. Empty hits and HTTP 422 invalid queries strictly bill $0 (billed_micros = 0, charged_micros = 0).",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CompileRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Compiled scientific comparison matrix and protocol checklist",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CompileResponse" }
              }
            }
          },
          "422": {
            "description": "Invalid or empty/junk query ($0 billed, billed_micros = 0, charged_micros = 0)"
          }
        }
      }
    },
    "/batch": {
      "post": {
        "operationId": "compileBatch",
        "tags": ["Compile"],
        "summary": "Concurrent heterogeneous scientific batch compilation with hard max_budget_micros cap",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BatchRequest" }
            }
          }
        },
        "responses": {
          "200": { "description": "Batch compilation summary and per-item receipts" },
          "422": { "description": "Invalid batch input ($0 billed)" }
        }
      }
    },
    "/api/batch": {
      "post": {
        "operationId": "compileBatchApi",
        "tags": ["Compile"],
        "summary": "Concurrent heterogeneous scientific batch compilation (canonical /api alias)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BatchRequest" }
            }
          }
        },
        "responses": {
          "200": { "description": "Batch compilation summary and per-item receipts" },
          "422": { "description": "Invalid batch input ($0 billed)" }
        }
      }
    },
    "/export": {
      "post": {
        "operationId": "exportCompiledEvidence",
        "tags": ["Export"],
        "summary": "Export compiled comparison table and protocol checklist as JSON, CSV, or Markdown",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ExportRequest" }
            }
          }
        },
        "responses": {
          "200": { "description": "Exported payload in JSON, CSV, or Markdown" },
          "422": { "description": "Invalid export request ($0 billed)" }
        }
      },
      "get": {
        "operationId": "exportCompiledEvidenceGet",
        "tags": ["Export"],
        "summary": "Export a stored receipt or query in JSON, CSV, or Markdown",
        "parameters": [
          { "name": "format", "in": "query", "schema": { "type": "string", "enum": ["json", "csv", "markdown"] } },
          { "name": "receipt_id", "in": "query", "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Exported payload" },
          "404": { "description": "Receipt not found" }
        }
      }
    },
    "/api/export": {
      "post": {
        "operationId": "exportCompiledEvidenceApi",
        "tags": ["Export"],
        "summary": "Export compiled comparison table and protocol checklist (canonical /api alias)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ExportRequest" }
            }
          }
        },
        "responses": {
          "200": { "description": "Exported payload in JSON, CSV, or Markdown" },
          "422": { "description": "Invalid export request ($0 billed)" }
        }
      },
      "get": {
        "operationId": "exportCompiledEvidenceApiGet",
        "tags": ["Export"],
        "summary": "Export a stored receipt or query in JSON, CSV, or Markdown (/api alias)",
        "parameters": [
          { "name": "format", "in": "query", "schema": { "type": "string", "enum": ["json", "csv", "markdown"] } },
          { "name": "receipt_id", "in": "query", "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Exported payload" },
          "404": { "description": "Receipt not found" }
        }
      }
    },
    "/api/billing/balance": {
      "get": {
        "operationId": "getBillingBalance",
        "tags": ["Billing"],
        "summary": "Query tenant prepaid credit balance, hit/miss counters, and $2.00 external ammo cap status",
        "parameters": [
          { "name": "tenant_id", "in": "query", "required": false, "schema": { "type": "string" } },
          { "name": "X-Tenant-Id", "in": "header", "required": false, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "Tenant balance and external micro-ammo cap status",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BillingBalanceResponse" }
              }
            }
          },
          "422": { "description": "Missing or invalid tenant_id ($0 billed)" }
        }
      }
    },
    "/api/billing/packs": {
      "get": {
        "operationId": "getBillingPacks",
        "tags": ["Billing"],
        "summary": "List Creem prepaid credit packs, per-source unit prices, and $0-on-miss guarantees",
        "responses": {
          "200": { "description": "Creem credit packs and per-source pricing schedule" }
        }
      }
    },
    "/mcp": {
      "post": {
        "operationId": "invokeMcp",
        "tags": ["MCP"],
        "summary": "Native flat JSON-RPC 2.0 MCP endpoint",
        "responses": {
          "200": { "description": "JSON-RPC 2.0 response" }
        }
      }
    },
    "/webhooks/creem": {
      "post": {
        "operationId": "handleCreemWebhook",
        "tags": ["Billing"],
        "summary": "Idempotent Creem HMAC-SHA256 prepaid credit webhook handler",
        "description": "Verifies creem-signature using CREEM_WEBHOOK_SECRET from environment variables and credits tenant balance atomically inside SQLite WAL BEGIN IMMEDIATE.",
        "responses": {
          "200": { "description": "Webhook processed or deduplicated" },
          "401": { "description": "Invalid or missing HMAC-SHA256 signature" },
          "503": { "description": "CREEM_WEBHOOK_SECRET not configured" }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "HealthResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string" },
          "service": { "type": "string" },
          "version": { "type": "string" },
          "public_host": { "type": "string" },
          "port": { "type": "integer" },
          "sources": { "type": "array", "items": { "type": "string" } },
          "ledger_enabled": { "type": "boolean" },
          "mcp_enabled": { "type": "boolean" }
        }
      },
      "CompileRequest": {
        "type": "object",
        "required": ["query"],
        "properties": {
          "query": { "type": "string", "description": "Compound name/CID/SMILES, target, NCT ID, patent ID, PMID/PMCID/DOI/arXiv ID, or scientific question." },
          "source_mode": {
            "type": "string",
            "enum": ["auto", "literature", "compound", "trials", "fda", "patent"],
            "default": "auto"
          },
          "tenant_id": { "type": "string" },
          "verify": { "type": "boolean", "default": true },
          "confirm": { "type": "boolean", "default": false },
          "recheck": { "type": "boolean", "default": false },
          "export_format": { "type": "string", "enum": ["json", "csv", "markdown"] }
        }
      },
      "CompileResponse": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "status_code": { "type": "integer" },
          "receipt_id": { "type": "string" },
          "query": { "type": "string" },
          "source_mode": { "type": "string" },
          "verdict": { "type": "string" },
          "confidence": { "type": "number" },
          "records": { "type": "array", "items": { "type": "object" } },
          "comparison_table": {
            "oneOf": [
              { "type": "object" },
              { "type": "array", "items": { "type": "object" } }
            ]
          },
          "protocol_checklist": { "type": "array", "items": { "type": "object" } },
          "provenance_ids": {
            "oneOf": [
              { "type": "array", "items": { "type": "string" } },
              { "type": "object" }
            ]
          },
          "evidence_sources": { "type": "array", "items": { "type": "object" } },
          "checks": { "type": "array", "items": { "type": "object" } },
          "billed_micros": { "type": "integer" },
          "charged_micros": { "type": "integer" },
          "base_price_micros": { "type": "integer" },
          "zero_bill_reason": { "type": "string" },
          "payload_sha256": { "type": "string" },
          "sha256_attestation": { "type": "string" },
          "cache_hit": { "type": "boolean" },
          "cache_tier": { "type": "string" },
          "timestamp_utc": { "type": "string" }
        }
      },
      "BillingBalanceResponse": {
        "type": "object",
        "properties": {
          "tenant_id": { "type": "string" },
          "exists": { "type": "boolean" },
          "balance_micros": { "type": "integer" },
          "balance_usd": { "type": "number" },
          "total_credited_micros": { "type": "integer" },
          "total_debited_micros": { "type": "integer" },
          "total_hits": { "type": "integer" },
          "total_zero_bill_misses": { "type": "integer" },
          "external_ammo": { "type": "object" }
        }
      },
      "BatchRequest": {
        "type": "object",
        "properties": {
          "items": { "type": "array", "items": { "$ref": "#/components/schemas/CompileRequest" } },
          "csv_text": { "type": "string" },
          "source_mode": { "type": "string" },
          "max_budget_micros": { "type": "integer", "default": 2000000 },
          "tenant_id": { "type": "string" }
        }
      },
      "ExportRequest": {
        "type": "object",
        "required": ["format"],
        "properties": {
          "format": { "type": "string", "enum": ["json", "csv", "markdown"] },
          "receipt_id": { "type": "string" },
          "result": { "type": "object" },
          "query": { "type": "string" },
          "source_mode": { "type": "string" }
        }
      }
    }
  }
}
