{
  "openapi": "3.1.0",
  "info": {
    "title": "Route-Anything API (ANY-A04)",
    "version": "1.0.0",
    "summary": "Polymorphic 5-domain cost-sorted verified waterfall router with BYOK 0% markup, hard max_cost_micros budget cap, and $0 unbilled misses.",
    "description": "Unified routing engine across llm_inference, gpu_compute, multimodal_gen, b2b_enrichment, and fx_logistics. Guarantees zero customer trace resale and deterministic $0 billing on 422 validation errors, fake-200 pages, or misses.",
    "contact": {
      "name": "Route-Anything Operations",
      "email": "routing@fastmesh.dev",
      "url": "https://route.fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://route.fastmesh.dev",
      "description": "Public HTTPS gateway"
    },
    {
      "url": "http://127.0.0.1:8804",
      "description": "Local upstream server"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "summary": "Service health, domain readiness, archive cache stats, and ledger status",
        "responses": {
          "200": {
            "description": "Healthy service payload",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["ok", "service", "subdomain", "domains", "zero_trace_resale"],
                  "properties": {
                    "ok": { "type": "boolean" },
                    "service": { "type": "string" },
                    "subdomain": { "type": "string" },
                    "domains": { "type": "array", "items": { "type": "string" } },
                    "intel_catalog_loaded": { "type": "boolean" },
                    "healthy_nodes": { "type": "integer" },
                    "total_nodes": { "type": "integer" },
                    "zero_trace_resale": { "type": "boolean" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/domains": {
      "get": {
        "operationId": "listDomains",
        "summary": "List all 5 polymorphic routing domains with pricing bounds, volatility policy, input schema, and aggregate node health",
        "responses": {
          "200": {
            "description": "Polymorphic domain directory (zero vendor names exposed)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["ok", "service", "zero_trace_resale", "domains"],
                  "properties": {
                    "ok": { "type": "boolean" },
                    "service": { "type": "string" },
                    "intel_catalog_loaded": { "type": "boolean" },
                    "zero_trace_resale": { "type": "boolean" },
                    "domains": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/DomainSummary" }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/quote": {
      "post": {
        "operationId": "quoteRoute",
        "summary": "Dry-run hidden surcharge normalization and cost-arbitrage quote without executing upstream providers ($0 billed)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/QuoteRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Normalized cost quote and savings estimate",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/QuoteResponse" }
              }
            }
          },
          "422": {
            "description": "Unbilled deterministic validation rejection ($0 charged)",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ValidationErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/api/route": {
      "post": {
        "operationId": "executeRoute",
        "summary": "Execute cost-sorted verified waterfall route with hard max_cost_micros cap, BYOK priority, and redacted trace",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/RouteRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Verified routing outcome with savings receipt and redacted trace",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/RouteResponse" }
              }
            }
          },
          "402": {
            "description": "Budget exceeded or insufficient prepaid balance (no overspend)",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          },
          "422": {
            "description": "Unbilled deterministic validation rejection ($0 charged)",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ValidationErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/api/byok/keys": {
      "get": {
        "operationId": "listByokKeys",
        "summary": "List redacted metadata of registered write-only BYOK keys for a tenant",
        "parameters": [
          {
            "name": "tenant_id",
            "in": "query",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Redacted BYOK key metadata list (plaintext secrets are never returned)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["ok", "tenant_id", "keys"],
                  "properties": {
                    "ok": { "type": "boolean" },
                    "tenant_id": { "type": "string" },
                    "markup_pct": { "type": "number" },
                    "keys": {
                      "type": "array",
                      "items": { "type": "object", "additionalProperties": true }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "registerByokKey",
        "summary": "Register an encrypted write-only BYOK key (0% routing markup, SSRF private-IP blocked)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ByokRegisterRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "BYOK key encrypted and stored",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["ok"],
                  "properties": {
                    "ok": { "type": "boolean" },
                    "tenant_id": { "type": "string" },
                    "provider_id": { "type": "string" },
                    "markup_pct": { "type": "number" },
                    "write_only": { "type": "boolean" }
                  }
                }
              }
            }
          },
          "422": {
            "description": "Invalid input or SSRF private-IP target blocked",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ValidationErrorResponse" }
              }
            }
          }
        }
      },
      "delete": {
        "operationId": "revokeByokKey",
        "summary": "Revoke and delete a registered BYOK key",
        "parameters": [
          {
            "name": "tenant_id",
            "in": "query",
            "required": true,
            "schema": { "type": "string" }
          },
          {
            "name": "provider_id",
            "in": "query",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "BYOK key revoked",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["ok", "deleted"],
                  "properties": {
                    "ok": { "type": "boolean" },
                    "deleted": { "type": "boolean" },
                    "provider_id": { "type": "string" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/mcp": {
      "get": {
        "operationId": "getMcpManifest",
        "summary": "Return native flat MCP server manifest and O(1) tool definitions",
        "responses": {
          "200": {
            "description": "Flat MCP manifest",
            "content": {
              "application/json": {
                "schema": { "type": "object" }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "invokeMcpJsonRpc",
        "summary": "JSON-RPC 2.0 MCP endpoint supporting initialize, tools/list, and tools/call",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/McpJsonRpcRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC 2.0 response envelope",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["jsonrpc", "id"],
                  "properties": {
                    "jsonrpc": { "type": "string", "const": "2.0" },
                    "id": { "type": ["string", "integer", "null"] },
                    "result": { "type": "object" },
                    "error": { "type": "object" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/webhooks/creem": {
      "post": {
        "operationId": "receiveCreemWebhook",
        "summary": "Constant-time HMAC-SHA256 verified, idempotent Creem billing webhook receiver",
        "parameters": [
          {
            "name": "creem-signature",
            "in": "header",
            "required": true,
            "description": "HMAC-SHA256 hex digest (or sha256=<hex>) computed over raw request body using CREEM_WEBHOOK_SECRET",
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreemWebhookPayload" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Webhook signature verified and event processed or idempotently deduplicated",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["ok", "event_id", "event_type", "wallet_id", "customer_ref_sha256_12", "balance_micros"],
                  "properties": {
                    "ok": { "type": "boolean" },
                    "event_id": { "type": "string" },
                    "event_type": { "type": "string" },
                    "wallet_id": { "type": "string" },
                    "customer_ref_sha256_12": { "type": "string" },
                    "duplicate": { "type": "boolean" },
                    "credits_delta_micros": { "type": "integer" },
                    "balance_micros": { "type": "integer", "minimum": 0 },
                    "subscription_status": { "type": "string" },
                    "tier": { "type": "string" }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Missing or forged HMAC-SHA256 signature",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "DomainEnum": {
        "type": "string",
        "enum": [
          "llm_inference",
          "gpu_compute",
          "multimodal_gen",
          "b2b_enrichment",
          "fx_logistics"
        ]
      },
      "ModifierEnum": {
        "type": "string",
        "enum": ["floor", "nitro", "exacto", "balanced"],
        "default": "floor"
      },
      "DomainSummary": {
        "type": "object",
        "required": ["domain", "display_name", "volatile", "ttl_policy", "baseline_cost_micros", "provider_pool_size"],
        "properties": {
          "domain": { "$ref": "#/components/schemas/DomainEnum" },
          "display_name": { "type": "string" },
          "description": { "type": "string" },
          "volatile": { "type": "boolean" },
          "ttl_policy": { "type": "string" },
          "baseline_cost_micros": { "type": "integer" },
          "min_provider_cost_micros": { "type": "integer" },
          "max_provider_cost_micros": { "type": "integer" },
          "min_latency_ms": { "type": "integer" },
          "provider_pool_size": { "type": "integer" },
          "healthy_providers": { "type": "integer" },
          "grade_distribution": { "type": "object", "additionalProperties": { "type": "integer" } },
          "supports_byok": { "type": "boolean" }
        }
      },
      "QuoteRequest": {
        "type": "object",
        "required": ["domain", "params"],
        "properties": {
          "domain": { "$ref": "#/components/schemas/DomainEnum" },
          "params": { "type": "object", "additionalProperties": true },
          "max_cost_micros": { "type": "integer", "minimum": 0 },
          "modifier": { "$ref": "#/components/schemas/ModifierEnum" }
        }
      },
      "RouteRequest": {
        "type": "object",
        "required": ["domain", "params"],
        "properties": {
          "domain": { "$ref": "#/components/schemas/DomainEnum" },
          "params": { "type": "object", "additionalProperties": true },
          "tenant_id": { "type": "string", "default": "default_tenant" },
          "max_cost_micros": { "type": "integer", "minimum": 0 },
          "modifier": { "$ref": "#/components/schemas/ModifierEnum" },
          "use_byok": { "type": "boolean", "default": false },
          "allow_managed_fallback": { "type": "boolean", "default": false },
          "verify": { "type": "boolean", "default": false },
          "confirm": { "type": "boolean", "default": false },
          "require_confirmed": { "type": "boolean", "default": false },
          "no_cache": { "type": "boolean", "default": false },
          "egress_url": { "type": "string", "format": "uri" }
        }
      },
      "RedactedTraceStep": {
        "type": "object",
        "description": "Publicly safe execution trace step with zero vendor names, zero API keys, and zero internal recipe ordering.",
        "properties": {
          "step": { "type": "integer" },
          "source": { "type": "string" },
          "tier": { "type": "string" },
          "outcome": { "type": "string" },
          "status_code": { "type": ["integer", "null"] },
          "latency_ms": { "type": "integer" }
        }
      },
      "QuoteResponse": {
        "type": "object",
        "required": [
          "ok",
          "status_code",
          "domain",
          "charged_micros",
          "estimated_cost_micros",
          "within_budget",
          "baseline_cost_micros",
          "actual_cost_micros",
          "saved_micros",
          "savings_pct",
          "receipt_sha256",
          "zero_trace_resale_attestation"
        ],
        "properties": {
          "ok": { "type": "boolean" },
          "status_code": { "type": "integer" },
          "domain": { "$ref": "#/components/schemas/DomainEnum" },
          "modifier": { "type": "string" },
          "charged_micros": { "type": "integer", "const": 0 },
          "estimated_cost_micros": { "type": "integer" },
          "within_budget": { "type": "boolean" },
          "surcharge_breakdown": { "type": "object", "additionalProperties": true },
          "healthy_tiers_count": { "type": "integer" },
          "volatile": { "type": "boolean" },
          "ttl_policy": { "type": "string" },
          "outcome": { "type": "object", "additionalProperties": true },
          "baseline_cost_micros": { "type": "integer" },
          "actual_cost_micros": { "type": "integer" },
          "saved_micros": { "type": "integer" },
          "savings_pct": { "type": "number" },
          "receipt_sha256": { "type": "string" },
          "zero_trace_resale_attestation": { "type": "object", "additionalProperties": true }
        }
      },
      "RouteResponse": {
        "type": "object",
        "required": [
          "ok",
          "status_code",
          "domain",
          "route_mode",
          "cache_status",
          "outcome",
          "selected_node",
          "charged_micros",
          "trace",
          "baseline_cost_micros",
          "actual_cost_micros",
          "saved_micros",
          "savings_pct",
          "receipt_sha256",
          "zero_trace_resale_attestation"
        ],
        "properties": {
          "ok": { "type": "boolean" },
          "status_code": { "type": "integer" },
          "domain": { "$ref": "#/components/schemas/DomainEnum" },
          "modifier": { "type": "string" },
          "route_mode": { "type": "string" },
          "cache_status": { "type": "string" },
          "outcome": { "type": ["object", "null"], "additionalProperties": true },
          "selected_node": {
            "type": ["object", "null"],
            "properties": {
              "tier": { "type": "string" },
              "grade": { "type": "string" },
              "latency_ms": { "type": "integer" },
              "surcharge_breakdown": { "type": "object", "additionalProperties": true }
            }
          },
          "verification": { "type": ["object", "null"], "additionalProperties": true },
          "confirmation": { "type": ["object", "null"], "additionalProperties": true },
          "charged_micros": { "type": "integer", "minimum": 0 },
          "platform_markup_micros": { "type": "integer", "minimum": 0 },
          "upstream_cost_micros": { "type": "integer", "minimum": 0 },
          "dispatched_count": { "type": "integer", "minimum": 0 },
          "trace": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/RedactedTraceStep" }
          },
          "baseline_cost_micros": { "type": "integer", "minimum": 0 },
          "actual_cost_micros": { "type": "integer", "minimum": 0 },
          "saved_micros": { "type": "integer" },
          "savings_pct": { "type": "number" },
          "receipt_sha256": { "type": "string" },
          "zero_trace_resale_attestation": { "type": "object", "additionalProperties": true }
        }
      },
      "ByokRegisterRequest": {
        "type": "object",
        "required": ["tenant_id", "provider_id", "api_key"],
        "properties": {
          "tenant_id": { "type": "string", "minLength": 1 },
          "provider_id": { "type": "string", "minLength": 1 },
          "api_key": { "type": "string", "minLength": 4 }
        }
      },
      "McpJsonRpcRequest": {
        "type": "object",
        "required": ["jsonrpc", "id", "method"],
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer"] },
          "method": { "type": "string", "enum": ["initialize", "tools/list", "tools/call"] },
          "params": { "type": "object", "additionalProperties": true }
        }
      },
      "CreemWebhookPayload": {
        "type": "object",
        "required": ["id", "eventType"],
        "properties": {
          "id": { "type": "string" },
          "eventType": {
            "type": "string",
            "enum": [
              "checkout.completed",
              "subscription.active",
              "subscription.paid",
              "subscription.canceled",
              "subscription.expired",
              "refund.created"
            ]
          },
          "object": { "type": "object", "additionalProperties": true }
        }
      },
      "ValidationErrorResponse": {
        "type": "object",
        "required": ["ok", "status_code", "charged_micros", "error_code"],
        "properties": {
          "ok": { "type": "boolean", "const": false },
          "status_code": { "type": "integer", "const": 422 },
          "charged_micros": { "type": "integer", "const": 0 },
          "error_code": { "type": "string" },
          "error_detail": { "type": "string" }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": ["ok", "error"],
        "properties": {
          "ok": { "type": "boolean", "const": false },
          "status_code": { "type": "integer" },
          "error": { "type": "string" }
        }
      }
    }
  }
}
