{
  "openapi": "3.1.0",
  "info": {
    "title": "SplitFill gateway",
    "version": "0.1.0",
    "description": "Pay-per-read access to a shared KV cache. The first reader of a public document pays the prefill, the n-th reader pays C/n, and earlier readers earn rebates. Payments use x402 v2 with an EIP-3009 USDC authorization on Base. Testnet only. Planned and not yet implemented: POST /v1/pools (publisher registration) and GET /metrics."
  },
  "servers": [
    {
      "url": "https://split-fill.vercel.app",
      "description": "Public market API. It forwards /v1/pools, /v1/fees and /v1/chat/completions to the Fly gateway, which is not deployed yet. Run the stack locally with `just market-local` until it is."
    }
  ],
  "paths": {
    "/healthz": {
      "get": {
        "summary": "Process health",
        "operationId": "health",
        "responses": { "204": { "description": "The process is up." } }
      }
    },
    "/v1/chat/completions": {
      "post": {
        "summary": "Ask one question about a pooled document",
        "description": "One non-streaming question. Without PAYMENT-SIGNATURE the gateway answers 402 with an exact x402 v2 quote in the PAYMENT-REQUIRED header (base64 JSON). With a signature the gateway settles, answers, and returns a PAYMENT-RESPONSE receipt header. The gateway overwrites cache_salt and priority, and strips kv_transfer_params. Do not send them.",
        "operationId": "ask",
        "parameters": [
          { "name": "X-SplitFill-Pool", "in": "header", "required": false, "schema": { "type": "string", "pattern": "^0x[0-9a-fA-F]{64}$" }, "description": "Pool ID. Required unless splitfill.pool_id is in the body. If both are sent they must match." },
          { "name": "PAYMENT-SIGNATURE", "in": "header", "required": false, "schema": { "type": "string" }, "description": "Base64 JSON x402 v2 payload with an EIP-3009 authorization. Omit it to get a quote." },
          { "name": "X-SplitFill-Name", "in": "header", "required": false, "schema": { "type": "string" }, "description": "An ENS name the payer claims. The receipt carries splitfill.verified_name only if the name lists the payer account and the account's ENSIP-28 consent is valid. A bad claim never blocks the read." },
          { "name": "Prefer", "in": "header", "required": false, "schema": { "type": "string", "enum": ["respond-async"] }, "description": "Facilitated reads only. Return 202 and poll the operation." }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["model", "messages"],
                "properties": {
                  "model": { "type": "string", "description": "The pool's model alias." },
                  "messages": { "type": "array", "minItems": 1, "maxItems": 1, "items": { "type": "object", "required": ["role", "content"], "properties": { "role": { "const": "user" }, "content": { "type": "string" } } } },
                  "max_tokens": { "type": "integer", "minimum": 1, "maximum": 1024 },
                  "stream": { "type": "boolean", "const": false },
                  "splitfill": { "type": "object", "properties": { "pool_id": { "type": "string" } } }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The answer. An OpenAI-style chat completion. splitfill.verified_name is present when the claimed ENS name verified.",
            "headers": { "PAYMENT-RESPONSE": { "schema": { "type": "string" }, "description": "Base64 JSON receipt: success, network, transaction, payer, amount, and vaultTransaction when recorded." } },
            "content": { "application/json": { "schema": { "type": "object", "additionalProperties": true, "properties": { "choices": { "type": "array", "items": { "type": "object" } }, "splitfill": { "type": "object", "properties": { "verified_name": { "type": "string" } } } } } } }
          },
          "202": { "description": "Settlement is confirming. Poll status_url with the original PAYMENT-SIGNATURE.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/PendingOperation" } } } },
          "400": { "description": "Malformed request or payment payload." },
          "402": {
            "description": "A quote, or a payment that did not verify.",
            "headers": { "PAYMENT-REQUIRED": { "schema": { "type": "string" }, "description": "Base64 JSON x402 v2 challenge: x402Version, resource, and accepts with scheme exact, network, amount, asset, payTo, maxTimeoutSeconds." } },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/QuoteBody" } } }
          },
          "404": { "description": "Unknown pool." },
          "409": { "description": "The authorization expired without a settled transfer. Nothing was paid. Request a new quote." },
          "410": { "description": "The pool is exhausted and takes no new reads." },
          "503": { "description": "The settlement outcome is not known yet. Keep the original signed read and retry it. Do not pay again." }
        },
        "security": [{ "x402": [] }, {}]
      }
    },
    "/v1/pools": {
      "get": {
        "summary": "List pools",
        "description": "Reader counts are bucketed (0, 1, 2-4, 5-16, 17+) and delayed. In market mode the model and content_hash filters find a pool for a public document.",
        "operationId": "listPools",
        "parameters": [
          { "name": "model", "in": "query", "required": false, "schema": { "type": "string" } },
          { "name": "content_hash", "in": "query", "required": false, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Pools.", "content": { "application/json": { "schema": { "type": "object", "required": ["pools"], "properties": { "pools": { "type": "array", "items": { "$ref": "#/components/schemas/Pool" } } } } } } }
        }
      }
    },
    "/v1/pools/{id}": {
      "get": {
        "summary": "One pool",
        "operationId": "getPool",
        "parameters": [{ "$ref": "#/components/parameters/PoolId" }],
        "responses": {
          "200": { "description": "The pool.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Pool" } } } },
          "404": { "description": "Unknown pool." },
          "410": { "description": "The pool is exhausted and takes no new reads." }
        }
      }
    },
    "/v1/pools/{id}/rebate": {
      "get": {
        "summary": "Cumulative rebate for one paid read",
        "description": "Send the original PAYMENT-SIGNATURE of the read. The entitlement is floor(C*(N-i)/(i*N)) for reader i of N.",
        "operationId": "getRebate",
        "parameters": [
          { "$ref": "#/components/parameters/PoolId" },
          { "name": "PAYMENT-SIGNATURE", "in": "header", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "The entitlement so far.", "content": { "application/json": { "schema": { "type": "object", "properties": { "pool_id": { "type": "string" }, "epoch": { "type": "integer" }, "reader_index": { "type": "integer" }, "prefix_paid": { "type": "string", "description": "Base units." }, "cumulative_rebate": { "type": "string", "description": "Base units." } } } } } },
          "404": { "description": "No receipt for this signature." }
        },
        "security": [{ "x402": [] }]
      }
    },
    "/v1/pools/{id}/operations/{epoch}/{operation}": {
      "get": {
        "summary": "Poll an asynchronous read",
        "description": "Facilitated reads can opt into Prefer: respond-async. Poll with the original PAYMENT-SIGNATURE. 202 while confirming, 200 with the answer and the original receipt once settled.",
        "operationId": "getOperation",
        "parameters": [
          { "$ref": "#/components/parameters/PoolId" },
          { "name": "epoch", "in": "path", "required": true, "schema": { "type": "integer" } },
          { "name": "operation", "in": "path", "required": true, "schema": { "type": "string", "pattern": "^[0-9a-f]{64}$" } },
          { "name": "PAYMENT-SIGNATURE", "in": "header", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Settled. Same body and PAYMENT-RESPONSE header as the original read." },
          "202": { "description": "Still confirming.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/PendingOperation" } } } },
          "409": { "description": "The authorization expired without a settled transfer." }
        },
        "security": [{ "x402": [] }]
      }
    },
    "/v1/fees": {
      "get": {
        "summary": "Recent cache utilization and base fees",
        "description": "Base fees followed by the next base fee. The sampler targets 70% cache use and caps each step at 12.5%.",
        "operationId": "getFees",
        "parameters": [{ "name": "blocks", "in": "query", "required": false, "schema": { "type": "integer", "minimum": 1, "maximum": 1024, "default": 20 } }],
        "responses": {
          "200": { "description": "History.", "content": { "application/json": { "schema": { "type": "object", "properties": { "base_fee": { "type": "array", "items": { "type": "string" }, "description": "Base units. The last entry is the next base fee." }, "utilization": { "type": "array", "items": { "type": "number" } } } } } } },
          "400": { "description": "blocks is outside 1 to 1024." }
        }
      }
    },
    "/v1/operator/pools": {
      "post": {
        "summary": "Publish a registered public document (operator only)",
        "description": "Market mode. Reachable on loopback only, never through the public host.",
        "operationId": "publishPool",
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "required": ["model", "document", "epoch"], "additionalProperties": false, "properties": { "model": { "type": "string" }, "document": { "type": "string" }, "epoch": { "type": "integer" } } } } } },
        "responses": { "200": { "description": "The published pool." }, "401": { "description": "Operator authorization required." } },
        "security": [{ "operator": [] }]
      }
    },
    "/v1/operator/status": {
      "get": {
        "summary": "Pending settlements (operator only)",
        "description": "Market mode. Loopback only.",
        "operationId": "operatorStatus",
        "responses": { "200": { "description": "Pending count and age.", "content": { "application/json": { "schema": { "type": "object", "properties": { "pending": { "type": "integer" }, "oldest_pending_seconds": { "type": ["integer", "null"] } } } } } }, "401": { "description": "Operator authorization required." } },
        "security": [{ "operator": [] }]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "x402": { "type": "apiKey", "in": "header", "name": "PAYMENT-SIGNATURE", "description": "x402 v2 payment payload (EIP-3009 transferWithAuthorization)." },
      "operator": { "type": "http", "scheme": "bearer", "description": "Operator admin token. Loopback only." }
    },
    "parameters": {
      "PoolId": { "name": "id", "in": "path", "required": true, "schema": { "type": "string", "pattern": "^0x[0-9a-fA-F]{64}$" }, "description": "Pool ID: a hash of the model, the template version and the content hash of the public document." }
    },
    "schemas": {
      "Pool": {
        "type": "object",
        "required": ["pool_id", "model", "template_version", "epoch", "readers_bucket"],
        "properties": {
          "pool_id": { "type": "string" },
          "content_hash": { "type": "string" },
          "model": { "type": "string" },
          "template_version": { "type": "string" },
          "epoch": { "type": "integer" },
          "readers_bucket": { "type": "string", "enum": ["0", "1", "2-4", "5-16", "17+"] },
          "cache_status": { "type": "string", "description": "Present when a registry is configured." },
          "rent_balance": { "type": ["string", "integer"], "description": "Present when rent is enabled. Delayed." }
        }
      },
      "QuoteBody": {
        "type": "object",
        "additionalProperties": true,
        "description": "Human-readable quote beside the PAYMENT-REQUIRED header. The header is the authority.",
        "properties": {
          "max_amount": { "type": "string", "description": "Base units." },
          "standalone_prefix_price": { "type": "string", "description": "Market mode. What a standalone prefix would cost." },
          "prefix_price": { "type": "string", "description": "Market mode. The next pooled prefix charge." },
          "epoch": { "type": "integer" },
          "cache_status": { "type": "string" }
        }
      },
      "PendingOperation": {
        "type": "object",
        "required": ["status", "operation_id", "status_url"],
        "properties": {
          "status": { "const": "confirming" },
          "operation_id": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
          "status_url": { "type": "string" }
        }
      }
    }
  }
}
