{
  "openapi": "3.1.0",
  "info": {
    "title": "SPL-Token Data API",
    "version": "1.1.0",
    "description": "x402 v2 pay-per-call SPL data APIs on Solana: **check-balance** (wallet holds mint) and **mint-metadata** (on-chain mint + Metaplex metadata + deterministic risk flags). Unpaid GETs return HTTP 402 with PaymentRequired JSON (and `PAYMENT-REQUIRED` header). Retry with `PAYMENT-SIGNATURE` after facilitator settlement (e.g. pr402). If pricing/`accepts[]` cannot be built, the service returns **503** (`PAYMENT_REQUIREMENTS_UNAVAILABLE`).\n\nAll successful and error responses include `X-API-Version: 1`.\n\nCanonical OpenAPI: `/openapi.json`. Agent discovery: `/.well-known/x402-resources.json`.",
    "license": { "name": "Proprietary" }
  },
  "externalDocs": {
    "description": "x402 Agent Integration Guide and HTTP workflow",
    "url": "https://spl-token.signer-payer.me/agent-integration.md"
  },
  "servers": [
    { "url": "https://spl-token.signer-payer.me", "description": "Production" }
  ],
  "paths": {
    "/api/v1/check-balance": {
      "get": {
        "summary": "Check SPL token balance (x402 v2)",
        "description": "Requires valid `wallet` and `spl-token` mint query params. When payment is enabled, the first request returns **402** with `accepts[]` — settle with your facilitator, then retry with `PAYMENT-SIGNATURE`. Responses include `X-API-Version: 1`.",
        "operationId": "checkBalance",
        "parameters": [
          {
            "name": "wallet",
            "in": "query",
            "required": true,
            "schema": { "type": "string" },
            "description": "Solana wallet public key (base58)"
          },
          {
            "name": "spl-token",
            "in": "query",
            "required": true,
            "schema": { "type": "string" },
            "description": "SPL token mint public key (base58)"
          },
          {
            "name": "min-balance",
            "in": "query",
            "required": false,
            "schema": { "type": "number", "default": 1.0 },
            "description": "Minimum required balance in UI token units (raw compare on-chain uses mint decimals)"
          },
          {
            "name": "X-Correlation-ID",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Optional agent session ID for distributed tracing and persistence."
          }
        ],
        "responses": {
          "200": {
            "description": "Balance result; includes `X-API-Version: 1`",
            "headers": {
              "X-API-Version": { "schema": { "type": "string", "example": "1" } },
              "X-Correlation-ID": { "schema": { "type": "string" } },
              "PAYMENT-RESPONSE": {
                "description": "Base64-encoded UTF-8 JSON of the facilitator settlement result (x402 v2 **SettleResponse** shape, aligned with pr402 `/settle`). Fields include at least `success`, `network` (CAIP-2), and `transaction` (**always a string**: Solana signature base58, or **`\"\"`** when there is no new on-chain signature—e.g. idempotent duplicate-settle). May include `payer`, `errorReason`, `correlationId`, or extension keys such as `settlementNote`.",
                "schema": { "type": "string" }
              }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BalanceCheckResponse" }
              }
            }
          },
          "400": {
            "description": "Invalid query or addresses",
            "headers": {
              "X-API-Version": { "schema": { "type": "string", "example": "1" } },
              "X-Correlation-ID": { "schema": { "type": "string" } }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JsonError" }
              }
            }
          },
          "402": {
            "description": "Payment required; includes `X-API-Version: 1`",
            "headers": {
              "X-API-Version": { "schema": { "type": "string", "example": "1" } },
              "X-Correlation-ID": { "schema": { "type": "string" } },
              "Payment-Required": {
                "description": "Base64(UTF-8 JSON) of the payment requirements body below.",
                "schema": { "type": "string" }
              }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PaymentRequired" }
              }
            }
          },
          "503": {
            "description": "Payment is required but server could not build `accepts[]` (configuration or DB)",
            "headers": {
              "X-API-Version": { "schema": { "type": "string", "example": "1" } },
              "X-Correlation-ID": { "schema": { "type": "string" } }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JsonError" },
                "example": {
                  "error": "Service Unavailable",
                  "message": "Payment is required for this service but pricing requirements could not be loaded",
                  "code": "PAYMENT_REQUIREMENTS_UNAVAILABLE"
                }
              }
            }
          },
          "500": {
            "description": "Internal error",
            "headers": {
              "X-API-Version": { "schema": { "type": "string", "example": "1" } },
              "X-Correlation-ID": { "schema": { "type": "string" } }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JsonError" }
              }
            }
          }
        },
        "security": [{ "x402_payment": [] }]
      }
    },
    "/api/v1/mint-metadata": {
      "get": {
        "summary": "SPL mint metadata and risk flags (x402 v2)",
        "description": "Requires `mint` (or alias `spl-token`) query param. Returns mint authorities, supply, Metaplex name/symbol/uri, and deterministic rug/freeze risk flags for agents. Same payment flow as check-balance.",
        "operationId": "mintMetadata",
        "parameters": [
          {
            "name": "mint",
            "in": "query",
            "required": false,
            "schema": { "type": "string" },
            "description": "SPL mint pubkey (base58). Alias: `spl-token`."
          },
          {
            "name": "spl-token",
            "in": "query",
            "required": false,
            "schema": { "type": "string" },
            "description": "Alias for `mint` (parity with check-balance)."
          },
          {
            "name": "X-Correlation-ID",
            "in": "header",
            "required": false,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Mint metadata result",
            "headers": {
              "X-API-Version": { "schema": { "type": "string", "example": "1" } },
              "PAYMENT-RESPONSE": { "schema": { "type": "string" } }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/MintMetadataResponse" }
              }
            }
          },
          "400": {
            "description": "Invalid mint",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JsonError" }
              }
            }
          },
          "402": {
            "description": "Payment required",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PaymentRequired" }
              }
            }
          },
          "503": {
            "description": "Payment requirements unavailable or RPC failure after settle",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JsonError" }
              }
            }
          }
        },
        "security": [{ "x402_payment": [] }]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "x402_payment": {
        "type": "apiKey",
        "in": "header",
        "name": "PAYMENT-SIGNATURE",
        "description": "x402 v2 payment proof: UTF-8 JSON (or base64 JSON) with `x402Version`, `paymentPayload`, `paymentRequirements` per spec §7; built after facilitator verify/settle for the chosen `accepts` line."
      }
    },
    "schemas": {
      "MintMetadataResponse": {
        "type": "object",
        "required": ["mint", "tokenProgram", "decimals", "supply", "supplyUi", "metadata", "risk"],
        "properties": {
          "mint": { "type": "string" },
          "tokenProgram": { "type": "string", "enum": ["spl-token", "token-2022", "unknown"] },
          "decimals": { "type": "integer" },
          "supply": { "type": "string" },
          "supplyUi": { "type": "number" },
          "mintAuthority": { "type": "string", "nullable": true },
          "freezeAuthority": { "type": "string", "nullable": true },
          "metadata": {
            "type": "object",
            "required": ["present"],
            "properties": {
              "present": { "type": "boolean" },
              "name": { "type": "string" },
              "symbol": { "type": "string" },
              "uri": { "type": "string" },
              "updateAuthority": { "type": "string" },
              "isMutable": { "type": "boolean" }
            }
          },
          "risk": {
            "type": "object",
            "required": ["level", "score", "scoringVersion", "summary", "flags"],
            "properties": {
              "level": { "type": "string", "enum": ["low", "medium", "high", "critical"] },
              "score": { "type": "integer", "minimum": 0, "maximum": 100 },
              "scoringVersion": { "type": "string", "example": "1" },
              "summary": { "type": "string" },
              "flags": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": ["code", "severity", "message"],
                  "properties": {
                    "code": { "type": "string" },
                    "severity": { "type": "string" },
                    "message": { "type": "string" }
                  }
                }
              }
            }
          }
        }
      },
      "BalanceCheckResponse": {
        "type": "object",
        "required": ["wallet", "token", "has_token", "balance", "balance_ui", "balance_met", "decimals"],
        "properties": {
          "wallet": { "type": "string" },
          "token": { "type": "string" },
          "has_token": { "type": "boolean" },
          "balance": { "type": "string", "description": "Raw token amount as string (integer units)" },
          "balance_ui": { "type": "number" },
          "balance_met": { "type": "boolean" },
          "decimals": { "type": "integer", "minimum": 0, "maximum": 9 }
        }
      },
      "ResourceInfo": {
        "type": "object",
        "required": ["url", "description", "mimeType"],
        "properties": {
          "url": { "type": "string", "format": "uri" },
          "description": { "type": "string" },
          "mimeType": { "type": "string", "example": "application/json" }
        }
      },
      "PaymentAcceptLine": {
        "type": "object",
        "description": "One payment option from `accepts[]` (e.g., v2:solana:exact or v2:solana:sla-escrow). Facilitator enriched with `extra` (e.g. feePayer, capabilitiesUrl).",
        "required": ["scheme", "network", "asset", "amount", "payTo", "maxTimeoutSeconds"],
        "properties": {
          "scheme": { "type": "string", "example": "exact" },
          "network": {
            "type": "string",
            "description": "CAIP-2 network id",
            "example": "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1"
          },
          "asset": { "type": "string", "description": "Mint pubkey (base58); native SOL uses 11111111111111111111111111111111" },
          "amount": { "type": "string", "description": "Required raw token amount (stringified integer)" },
          "payTo": { "type": "string", "description": "Seller/vault owner pubkey — drives UniversalSettle SplitVault PDA seeds when applicable" },
          "maxTimeoutSeconds": { "type": "integer", "format": "int64" },
          "extra": { 
            "type": "object", 
            "properties": {
              "feePayer": { "type": "string", "description": "Sponsoring fee payer for the payment transaction" },
              "capabilitiesUrl": { "type": "string", "format": "uri", "description": "Facilitator discovery endpoint for agents to ingest REST manifest" }
            },
            "additionalProperties": true 
          }
        },
        "additionalProperties": true
      },
      "PaymentRequired": {
        "type": "object",
        "required": ["x402Version", "resource", "accepts", "extensions"],
        "properties": {
          "x402Version": { "type": "integer", "const": 2 },
          "error": { "type": "string", "description": "Optional human context when body is sent with a 402" },
          "resource": { "$ref": "#/components/schemas/ResourceInfo" },
          "accepts": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/PaymentAcceptLine" },
            "minItems": 1
          },
          "extensions": { "type": "object", "additionalProperties": true }
        }
      },
      "JsonError": {
        "type": "object",
        "required": ["error", "message", "code"],
        "properties": {
          "error": { "type": "string" },
          "message": { "type": "string" },
          "code": { "type": "string", "description": "Machine-readable error enum" }
        }
      },
      "SettleResponse": {
        "type": "object",
        "description": "Decoded payload inside PAYMENT-RESPONSE after base64 decode (x402 v2 §5.3 / pr402 SettleResponse). `transaction` is always a JSON string; use empty string when no signature applies.",
        "required": ["success", "transaction"],
        "properties": {
          "success": { "type": "boolean" },
          "errorReason": { "type": "string" },
          "payer": { "type": "string" },
          "transaction": {
            "type": "string",
            "description": "Settlement transaction signature (base58 on Solana), or empty string for idempotent/no-sig cases"
          },
          "network": { "type": "string", "description": "CAIP-2 chain id" },
          "correlationId": { "type": "string" },
          "settlementNote": { "type": "string" },
          "settleErrorPreview": { "type": "string" }
        },
        "additionalProperties": true
      }
    }
  }
}
