{
  "openapi": "3.1.0",
  "info": {
    "title": "Atom-Anything API",
    "version": "1.0.0",
    "summary": "Deterministic 5-domain manufacturing, customs tariff, RFQ TCO, and logistics corridor costing engine with SHA-256 AtomReceipts and $0-on-422 billing.",
    "description": "Polymorphic deterministic physical-world costing engine across PCB/PCBA BOM & DFM, CNC Machining & 3D Printing, 2026 Lawful Public HS Tariffs & Landed Margin, Multi-Factory Incoterms RFQ TCO Normalization, and Geospatial UN/LOCODE Port Corridors. Every calculation returns a tamper-evident SHA-256 AtomReceipt with recomputable formula_audit. Invalid inputs (HTTP 422), misses, errors, and INCONCLUSIVE outcomes are strictly billed $0 (billed_micros = 0). Zero customer BOM, CAD, or supplier quote data resale guarantee.",
    "contact": {
      "name": "Atom-Anything Operations",
      "email": "ops@fastmesh.dev",
      "url": "https://atom.fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://atom.fastmesh.dev",
      "description": "Public HTTPS Gateway Mount"
    },
    {
      "url": "http://127.0.0.1:8810",
      "description": "Local Runtime Server"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Service liveness, pricing schedule, and SQLite ledger health" },
    { "name": "Calculation", "description": "Single and batch polymorphic 5-domain physical manufacturing & trade costing" },
    { "name": "Receipts", "description": "Tamper-evident SHA-256 AtomReceipt lookup and verification" },
    { "name": "MCP", "description": "Native flat JSON-RPC 2.0 Model Context Protocol endpoint" },
    { "name": "Discovery", "description": "Machine-readable OpenAPI, llms.txt, skill.md, and technical guides" },
    { "name": "Billing", "description": "Creem HMAC-SHA256 prepaid credit webhook settlement" }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "tags": ["Health"],
        "summary": "Check Atom-Anything service health, pricing schedule, and ledger status",
        "responses": {
          "200": {
            "description": "Service is healthy and ready",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" },
                "example": {
                  "status": "ok",
                  "service": "atom-anything",
                  "version": "1.0.0",
                  "public_host": "atom.fastmesh.dev",
                  "port": 8810,
                  "domains": [
                    "pcb_bom_dfm",
                    "cnc_3dp_cost",
                    "tariff_landed",
                    "rfq_tco_normalize",
                    "geo_corridor"
                  ],
                  "ledger_enabled": true,
                  "mcp_enabled": true,
                  "timestamp_utc": "2026-12-02T04:00:00+00:00"
                }
              }
            }
          }
        }
      }
    },
    "/calculate": {
      "post": {
        "operationId": "calculateSingle",
        "tags": ["Calculation"],
        "summary": "Run a deterministic physical manufacturing or trade calculation across any of the 5 domains",
        "description": "Returns a deterministic, tamper-evident SHA-256 AtomReceipt with recomputable formula_audit. Strictly bills $0 (`billed_micros = 0`) on HTTP 422 invalid input, miss, error, or INCONCLUSIVE verdict.",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Optional tenant identifier for prepaid credit ledger accounting and 10% recheck tier."
          },
          {
            "name": "X-API-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Optional prepaid API key (omittable for zero-signup Try-Now sandbox)."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CalculationRequest" },
              "examples": {
                "pcb_bom_dfm_4layer": {
                  "summary": "Domain 1: 4-layer FR4 PCB + BOM DFM & EOL Audit",
                  "value": {
                    "domain": "pcb_bom_dfm",
                    "target": "iot_controller_rev2",
                    "layers": 4,
                    "width_mm": 100,
                    "height_mm": 80,
                    "copper_oz": 1,
                    "surface_finish": "ENIG",
                    "trace_mil": 5.0,
                    "space_mil": 5.0,
                    "min_drill_mm": 0.3,
                    "quantities": [10, 100, 1000],
                    "bom": [
                      { "ref": "U101", "mpn": "STM32F405RGT6", "qty": 1, "unit_usd": 4.85, "package": "LQFP64" },
                      { "ref": "MOS1", "mpn": "SI2302CDS", "qty": 4, "unit_usd": 0.14, "package": "SOT23" },
                      { "ref": "R101", "mpn": "RC0603FR-0710KL", "qty": 24, "unit_usd": 0.008, "package": "0603" },
                      { "ref": "C101", "mpn": "CL10B104KB8NNNC", "qty": 18, "unit_usd": 0.012, "package": "0603" }
                    ]
                  }
                },
                "cnc_3dp_cost_6061": {
                  "summary": "Domain 2: CNC 6061-T6 Milled Housing with Tight Tolerance Check",
                  "value": {
                    "domain": "cnc_3dp_cost",
                    "target": "sensor_enclosure_v3",
                    "material": "6061-T6",
                    "process": "cnc_3axis",
                    "x_mm": 120,
                    "y_mm": 85,
                    "z_mm": 32,
                    "part_volume_cm3": 98.5,
                    "tolerance_mm": 0.025,
                    "surface_finish": "anodize_type_ii",
                    "quantities": [1, 10, 100]
                  }
                },
                "tariff_landed_us_cn": {
                  "summary": "Domain 3: 2026 Lawful US-CN HS Tariff, MPF/HMF & Landed Margin",
                  "value": {
                    "domain": "tariff_landed",
                    "target": "ev_charger_pcba_sku",
                    "hs_code": "8504.40",
                    "origin_country": "CN",
                    "destination_country": "US",
                    "unit_value_usd": 42.5,
                    "retail_price_usd": 149.0,
                    "quantity": 500,
                    "weight_kg": 1.4,
                    "dimensions_cm": { "l": 24, "w": 18, "h": 10 },
                    "shipping_mode": "air"
                  }
                },
                "rfq_tco_3factory": {
                  "summary": "Domain 4: Multi-Factory EXW/FOB/DDP RFQ TCO Rank-Reversal Audit",
                  "value": {
                    "domain": "rfq_tco_normalize",
                    "target": "enclosure_rfq_q4",
                    "quantity": 1000,
                    "quotes": [
                      { "supplier": "Shenzhen Alpha EXW", "incoterm": "EXW", "currency": "USD", "unit_price": 11.2, "tooling_usd": 3800, "moq": 500, "payment_terms": "100_prepaid" },
                      { "supplier": "Ningbo Beta FOB", "incoterm": "FOB", "currency": "CNY", "unit_price": 91.5, "tooling_usd": 1800, "moq": 1000, "payment_terms": "30_70" },
                      { "supplier": "LA Gamma DDP", "incoterm": "DDP", "currency": "USD", "unit_price": 16.4, "tooling_usd": 0, "moq": 500, "payment_terms": "net_60" }
                    ]
                  }
                },
                "geo_corridor_cnszx_uslax": {
                  "summary": "Domain 5: Geospatial UN/LOCODE Port Corridor CNSZX -> USLAX",
                  "value": {
                    "domain": "geo_corridor",
                    "target": "CNSZX->USLAX",
                    "origin_port": "CNSZX",
                    "dest_port": "USLAX",
                    "mode": "ocean",
                    "unit_weight_kg": 1.25
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Calculation completed with tamper-evident SHA-256 AtomReceipt",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AtomReceipt" }
              }
            }
          },
          "422": {
            "description": "Invalid, negative, zero, or unsupported physical input — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" },
                "example": {
                  "ok": false,
                  "error": "invalid_input_422",
                  "detail": "PCB width_mm and height_mm must be strictly positive numbers.",
                  "billed_micros": 0,
                  "zero_bill_reason": "invalid_input_422"
                }
              }
            }
          }
        }
      }
    },
    "/calculate/batch": {
      "post": {
        "operationId": "calculateBatch",
        "tags": ["Calculation"],
        "summary": "Run concurrent heterogeneous CSV or JSON batch costing with hard budget lock",
        "description": "Executes up to 100 concurrent heterogeneous BOM/SKU/RFQ/corridor rows bounded by `max_budget_micros`. Invalid (422), skipped, missed, or INCONCLUSIVE rows are strictly billed $0.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BatchCalculationRequest" },
              "example": {
                "tenant_id": "tenant_hw_demo",
                "max_budget_micros": 500000,
                "items": [
                  { "domain": "geo_corridor", "origin_port": "CNSZX", "dest_port": "USLAX", "mode": "ocean" },
                  { "domain": "cnc_3dp_cost", "material": "6061-T6", "process": "cnc_3axis", "x_mm": 100, "y_mm": 60, "z_mm": 25, "part_volume_cm3": 60, "tolerance_mm": 0.05, "quantities": [10, 100] }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Batch execution summary and per-row AtomReceipts",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BatchCalculationResponse" }
              }
            }
          },
          "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 AtomReceipt and verify its SHA-256 attestation",
        "parameters": [
          {
            "name": "receipt_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "Unique AtomReceipt ID (e.g., ar_9a1b2c3d4e5f6a7b)"
          }
        ],
        "responses": {
          "200": {
            "description": "Receipt retrieved with cryptographic attestation verification",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "signature_valid": { "type": "boolean" },
                    "receipt": { "$ref": "#/components/schemas/AtomReceipt" }
                  }
                }
              }
            }
          },
          "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 atom_calculate, atom_batch, and atom_verify_receipt.",
        "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": "procurement-agent", "version": "1.0.0" }
                    }
                  }
                },
                "tools_list": {
                  "summary": "List Flat MCP Tools",
                  "value": {
                    "jsonrpc": "2.0",
                    "id": 2,
                    "method": "tools/list",
                    "params": {}
                  }
                },
                "tools_call_calculate": {
                  "summary": "Invoke atom_calculate Tool",
                  "value": {
                    "jsonrpc": "2.0",
                    "id": 3,
                    "method": "tools/call",
                    "params": {
                      "name": "atom_calculate",
                      "arguments": {
                        "domain": "geo_corridor",
                        "origin_port": "CNSZX",
                        "dest_port": "USLAX",
                        "mode": "ocean"
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "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)"
          }
        }
      }
    },
    "/openapi.json": {
      "get": {
        "operationId": "getOpenApiSpec",
        "tags": ["Discovery"],
        "summary": "Retrieve this OpenAPI 3.1.0 machine-readable specification",
        "responses": {
          "200": {
            "description": "OpenAPI 3.1.0 JSON document",
            "content": {
              "application/json": {
                "schema": { "type": "object" }
              }
            }
          }
        }
      }
    },
    "/llms.txt": {
      "get": {
        "operationId": "getLlmsTxt",
        "tags": ["Discovery"],
        "summary": "AI agent discovery index for atom.fastmesh.dev",
        "responses": {
          "200": {
            "description": "Plaintext Markdown index for AI crawlers and coding agents",
            "content": {
              "text/plain": {
                "schema": { "type": "string" }
              }
            }
          }
        }
      }
    },
    "/skill.md": {
      "get": {
        "operationId": "getSkillMd",
        "tags": ["Discovery"],
        "summary": "Agent skill contract documenting REST, /mcp, and Treg Hub usage",
        "responses": {
          "200": {
            "description": "YAML-frontmatter Markdown skill definition",
            "content": {
              "text/markdown": {
                "schema": { "type": "string" }
              }
            }
          }
        }
      }
    },
    "/guides": {
      "get": {
        "operationId": "listGuides",
        "tags": ["Discovery"],
        "summary": "Browse technical manufacturing, tariff, and RFQ TCO costing guides",
        "responses": {
          "200": {
            "description": "HTML directory of technical costing guides (and /guides/{slug} detail pages)",
            "content": {
              "text/html": {
                "schema": { "type": "string" }
              }
            }
          }
        }
      }
    },
    "/webhooks/creem": {
      "post": {
        "operationId": "creemWebhook",
        "tags": ["Billing"],
        "summary": "Creem HMAC-SHA256 payment webhook ingress",
        "description": "Verifies `creem-signature` HMAC-SHA256 in constant time 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_atom_01",
                "eventType": "checkout.completed",
                "object": {
                  "product_id": "prod_atom_anything_pro_29",
                  "amount": 2900,
                  "currency": "USD",
                  "customer": { "email": "buyer@example.com" },
                  "metadata": { "tenant_id": "tenant_hw_acme", "sku_id": "sku_atom_pro_29" }
                }
              }
            }
          }
        },
        "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" }
        }
      },
      "CalculationRequest": {
        "type": "object",
        "required": ["domain"],
        "additionalProperties": true,
        "properties": {
          "domain": {
            "type": "string",
            "enum": [
              "pcb_bom_dfm",
              "cnc_3dp_cost",
              "tariff_landed",
              "rfq_tco_normalize",
              "geo_corridor"
            ]
          },
          "target": { "type": "string" },
          "confirm": { "type": "boolean", "default": false },
          "recheck": { "type": "boolean", "default": false },
          "tenant_id": { "type": "string" },
          "payload": { "type": "object" },
          "options": { "type": "object" }
        }
      },
      "EvidenceSource": {
        "type": "object",
        "required": ["source", "url", "status", "observed_at_utc", "summary"],
        "properties": {
          "source": { "type": "string" },
          "url": { "type": "string" },
          "status": { "type": "string" },
          "observed_at_utc": { "type": "string", "format": "date-time" },
          "summary": { "type": "string" }
        }
      },
      "CheckItem": {
        "type": "object",
        "required": ["name", "passed", "detail"],
        "properties": {
          "name": { "type": "string" },
          "passed": { "type": "boolean" },
          "detail": { "type": "string" }
        }
      },
      "AtomReceipt": {
        "type": "object",
        "required": [
          "receipt_id",
          "receipt_type",
          "domain",
          "target",
          "verdict",
          "confidence",
          "cost_breakdown",
          "formula_audit",
          "alerts",
          "evidence_sources",
          "checks",
          "payload_sha256",
          "sha256_attestation",
          "billed_micros",
          "base_price_micros",
          "cache_hit",
          "cache_tier",
          "confirm_dual_source",
          "timestamp_utc",
          "zero_bill_reason"
        ],
        "properties": {
          "receipt_id": { "type": "string" },
          "receipt_type": { "type": "string", "enum": ["AtomReceipt"] },
          "domain": {
            "type": "string",
            "enum": [
              "pcb_bom_dfm",
              "cnc_3dp_cost",
              "tariff_landed",
              "rfq_tco_normalize",
              "geo_corridor"
            ]
          },
          "target": { "type": "string" },
          "verdict": { "type": "string", "enum": ["VERIFIED", "FAILED", "INCONCLUSIVE"] },
          "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
          "cost_breakdown": { "type": "object" },
          "formula_audit": { "type": "object" },
          "alerts": { "type": "array", "items": { "type": "object" } },
          "evidence_sources": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/EvidenceSource" }
          },
          "checks": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/CheckItem" }
          },
          "payload_sha256": { "type": "string" },
          "sha256_attestation": { "type": "string" },
          "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"] },
          "confirm_dual_source": { "type": "boolean" },
          "timestamp_utc": { "type": "string", "format": "date-time" },
          "zero_bill_reason": { "type": "string" },
          "details": { "type": "object" }
        }
      },
      "BatchCalculationRequest": {
        "type": "object",
        "properties": {
          "items": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/CalculationRequest" }
          },
          "csv_text": { "type": "string" },
          "max_budget_micros": { "type": "integer", "minimum": 0 },
          "tenant_id": { "type": "string" },
          "confirm": { "type": "boolean", "default": false },
          "recheck": { "type": "boolean", "default": false },
          "max_workers": { "type": "integer", "minimum": 1, "maximum": 16 }
        }
      },
      "BatchCalculationResponse": {
        "type": "object",
        "properties": {
          "batch_id": { "type": "string" },
          "tenant_id": { "type": "string" },
          "total_items": { "type": "integer" },
          "processed_items": { "type": "integer" },
          "verified_count": { "type": "integer" },
          "failed_count": { "type": "integer" },
          "inconclusive_count": { "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/AtomReceipt" }
              }
            }
          }
        }
      },
      "ZeroBillErrorResponse": {
        "type": "object",
        "required": ["ok", "error", "billed_micros"],
        "properties": {
          "ok": { "type": "boolean" },
          "error": { "type": "string" },
          "detail": { "type": "string" },
          "billed_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" }
        }
      }
    }
  }
}
