{
  "openapi": "3.1.0",
  "info": {
    "title": "SPL-Token Balance Check API",
    "version": "1.0.0",
    "description": "x402 v2 pay-per-call API: verify an SPL token balance for a Solana wallet. Unpaid `GET /api/v1/check-balance` returns HTTP 402 with a PaymentRequired JSON body (and `PAYMENT-REQUIRED` header). Retry the same GET with header `PAYMENT-SIGNATURE` after settlement via your facilitator (e.g. pr402). If payment is enabled but pricing/`accepts[]` cannot be built, the service returns **503** (`PAYMENT_REQUIREMENTS_UNAVAILABLE`).\n\nSuccessful and error responses include `X-API-Version: 1`.\n\nCanonical OpenAPI: this file (`/openapi.json`); `/openai.json` redirects here. Agent discovery manifest: `/.well-known/x402.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 settlement result JSON (x402 v2) if request was paid.",
                "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": [] }]
      }
    }
  },
  "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": {
      "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" }
        }
      }
    }
  }
}
