{
  "components": {
    "schemas": {
      "ApiError": {
        "description": "A request that was refused for a reason other than the query text: a malformed body, a missing or rejected token, an account without beta access, or a failure on topspin's side.",
        "properties": {
          "code": {
            "$ref": "#/components/schemas/ApiErrorCode"
          },
          "message": {
            "type": "string"
          }
        },
        "required": [
          "code",
          "message"
        ],
        "title": "ApiError",
        "type": "object"
      },
      "ApiErrorCode": {
        "enum": [
          "bad_request",
          "unauthorized",
          "forbidden",
          "not_found",
          "unavailable",
          "internal"
        ],
        "title": "ApiErrorCode",
        "type": "string"
      },
      "Cell": {
        "description": "One value. `kind` says which of the value fields is set; none is set when `kind` is `null`.",
        "properties": {
          "booleanValue": {
            "type": "boolean"
          },
          "kind": {
            "$ref": "#/components/schemas/CellKind"
          },
          "numberValue": {
            "type": "number"
          },
          "stringValue": {
            "type": "string"
          }
        },
        "required": [
          "kind"
        ],
        "title": "Cell",
        "type": "object"
      },
      "CellKind": {
        "enum": [
          "null",
          "string",
          "number",
          "boolean"
        ],
        "title": "CellKind",
        "type": "string"
      },
      "Column": {
        "properties": {
          "align": {
            "$ref": "#/components/schemas/ColumnAlign"
          },
          "caveat": {
            "type": "string"
          },
          "description": {
            "description": "The glossary's sentence for the stat.",
            "type": "string"
          },
          "format": {
            "$ref": "#/components/schemas/ColumnFormat"
          },
          "hint": {
            "description": "The sentence the query wrote for this heading with `hint`.",
            "type": "string"
          },
          "key": {
            "description": "The column's name in the query language.",
            "type": "string"
          },
          "label": {
            "type": "string"
          }
        },
        "required": [
          "key",
          "label",
          "format",
          "align"
        ],
        "title": "Column",
        "type": "object"
      },
      "ColumnAlign": {
        "enum": [
          "left",
          "right"
        ],
        "title": "ColumnAlign",
        "type": "string"
      },
      "ColumnFormat": {
        "enum": [
          "integer",
          "decimal",
          "decimal2",
          "decimal3",
          "percentage",
          "percentage_whole",
          "batting_avg",
          "era",
          "text"
        ],
        "title": "ColumnFormat",
        "type": "string"
      },
      "ColumnPair": {
        "properties": {
          "guest": {
            "type": "string"
          },
          "host": {
            "type": "string"
          }
        },
        "required": [
          "host",
          "guest"
        ],
        "title": "ColumnPair",
        "type": "object"
      },
      "ComparisonOperator": {
        "enum": [
          "equal",
          "notEqual",
          "greaterThan",
          "greaterThanOrEqual",
          "lessThan",
          "lessThanOrEqual"
        ],
        "title": "ComparisonOperator",
        "type": "string"
      },
      "Diagnostic": {
        "description": "One problem with a query. `range` is where in the source it is, and is absent when the problem is about no part of the text, such as a dataset that could not be read.",
        "properties": {
          "code": {
            "$ref": "#/components/schemas/DiagnosticCode"
          },
          "message": {
            "type": "string"
          },
          "range": {
            "$ref": "#/components/schemas/Range"
          },
          "subject": {
            "description": "The name the problem is about, as written: a column, a dataset, a `composite(...)` call.",
            "type": "string"
          }
        },
        "required": [
          "code",
          "message"
        ],
        "title": "Diagnostic",
        "type": "object"
      },
      "DiagnosticCode": {
        "enum": [
          "SyntaxError",
          "UnknownLeague",
          "UnknownDataset",
          "UnknownField",
          "DimensionUnavailable",
          "BadValue",
          "BadOperator",
          "NotFilterable",
          "NotSortable",
          "OutOfCoverage",
          "BadLimit",
          "PercentileNeedsPopulation",
          "BadComposite",
          "BadRatio",
          "BadPercentile",
          "BadStanding",
          "BadFormat",
          "BadAsOf",
          "BadCalc",
          "BadPivot",
          "BadStage",
          "BadAlias",
          "BadBenchmark",
          "BadSpan",
          "NotGrouped",
          "NotGroupable",
          "AggregateInWhere",
          "ValueInHaving",
          "BadPair",
          "BadHint",
          "HighlightNotSelected",
          "KeepNotSelected",
          "ExecutionFailed",
          "HandlerMissing"
        ],
        "title": "DiagnosticCode",
        "type": "string"
      },
      "HighlightCondition": {
        "properties": {
          "column": {
            "type": "string"
          },
          "operator": {
            "$ref": "#/components/schemas/ComparisonOperator"
          },
          "value": {
            "$ref": "#/components/schemas/Cell"
          }
        },
        "required": [
          "column",
          "operator",
          "value"
        ],
        "title": "HighlightCondition",
        "type": "object"
      },
      "HighlightRule": {
        "description": "One `highlight` line. A row is highlighted when every condition of some rule holds.",
        "properties": {
          "conditions": {
            "items": {
              "$ref": "#/components/schemas/HighlightCondition"
            },
            "type": "array"
          }
        },
        "required": [
          "conditions"
        ],
        "title": "HighlightRule",
        "type": "object"
      },
      "OrderedBy": {
        "properties": {
          "direction": {
            "$ref": "#/components/schemas/SortDirection"
          },
          "key": {
            "type": "string"
          }
        },
        "required": [
          "key",
          "direction"
        ],
        "title": "OrderedBy",
        "type": "object"
      },
      "Pivot": {
        "description": "How the answer was read as a matrix by `pivot by`.",
        "properties": {
          "by": {
            "type": "string"
          },
          "headings": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "label": {
            "type": "string"
          }
        },
        "required": [
          "by",
          "label",
          "headings"
        ],
        "title": "Pivot",
        "type": "object"
      },
      "Position": {
        "properties": {
          "column": {
            "description": "1-based column, in UTF-16 code units.",
            "minimum": 1,
            "type": "integer"
          },
          "line": {
            "description": "1-based line.",
            "minimum": 1,
            "type": "integer"
          },
          "offset": {
            "description": "0-based UTF-16 offset into the source.",
            "minimum": 0,
            "type": "integer"
          }
        },
        "required": [
          "offset",
          "line",
          "column"
        ],
        "title": "Position",
        "type": "object"
      },
      "Principal": {
        "description": "Who an access token acts for.",
        "properties": {
          "apiKeyId": {
            "type": "string"
          },
          "apiKeyName": {
            "type": "string"
          },
          "displayName": {
            "type": "string"
          },
          "userId": {
            "type": "string"
          }
        },
        "required": [
          "userId",
          "apiKeyId",
          "apiKeyName"
        ],
        "title": "Principal",
        "type": "object"
      },
      "QueryRejected": {
        "description": "The query could not be run as written. Every diagnostic is something editing the source can fix.",
        "properties": {
          "diagnostics": {
            "items": {
              "$ref": "#/components/schemas/Diagnostic"
            },
            "minItems": 1,
            "type": "array"
          }
        },
        "required": [
          "diagnostics"
        ],
        "title": "QueryRejected",
        "type": "object"
      },
      "QueryRequest": {
        "properties": {
          "source": {
            "description": "Query-language text, exactly as typed in the topspin editor.",
            "maxLength": 20000,
            "minLength": 1,
            "type": "string"
          }
        },
        "required": [
          "source"
        ],
        "title": "QueryRequest",
        "type": "object"
      },
      "QueryResult": {
        "description": "A query's answer.",
        "properties": {
          "columns": {
            "items": {
              "$ref": "#/components/schemas/Column"
            },
            "type": "array"
          },
          "datasetId": {
            "type": "string"
          },
          "elapsedMs": {
            "type": "number"
          },
          "highlights": {
            "items": {
              "$ref": "#/components/schemas/HighlightRule"
            },
            "type": "array"
          },
          "notes": {
            "description": "Caveats about the answer: coverage, implied filters.",
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "orderedBy": {
            "$ref": "#/components/schemas/OrderedBy"
          },
          "pairs": {
            "items": {
              "$ref": "#/components/schemas/ColumnPair"
            },
            "type": "array"
          },
          "pivot": {
            "$ref": "#/components/schemas/Pivot"
          },
          "rowCount": {
            "type": "integer"
          },
          "rows": {
            "items": {
              "$ref": "#/components/schemas/Row"
            },
            "type": "array"
          },
          "sources": {
            "items": {
              "$ref": "#/components/schemas/Source"
            },
            "type": "array"
          },
          "truncated": {
            "description": "True when the row limit was reached and more rows exist.",
            "type": "boolean"
          },
          "warnings": {
            "description": "Problems in the source the query ran despite — a line that could not be read is skipped, as the editor skips it. Empty when the source is clean.",
            "items": {
              "$ref": "#/components/schemas/Diagnostic"
            },
            "type": "array"
          }
        },
        "required": [
          "datasetId",
          "columns",
          "rows",
          "rowCount",
          "truncated",
          "pairs",
          "highlights",
          "notes",
          "warnings",
          "sources",
          "elapsedMs"
        ],
        "title": "QueryResult",
        "type": "object"
      },
      "Range": {
        "description": "A half-open range of the source text: `end` is the first position after it.",
        "properties": {
          "end": {
            "$ref": "#/components/schemas/Position"
          },
          "start": {
            "$ref": "#/components/schemas/Position"
          }
        },
        "required": [
          "start",
          "end"
        ],
        "title": "Range",
        "type": "object"
      },
      "Row": {
        "properties": {
          "cells": {
            "description": "One per column, in column order.",
            "items": {
              "$ref": "#/components/schemas/Cell"
            },
            "type": "array"
          },
          "highlighted": {
            "description": "True when the query's `highlight` rules point at this row.",
            "type": "boolean"
          }
        },
        "required": [
          "cells",
          "highlighted"
        ],
        "title": "Row",
        "type": "object"
      },
      "SortDirection": {
        "enum": [
          "asc",
          "desc"
        ],
        "title": "SortDirection",
        "type": "string"
      },
      "Source": {
        "description": "Who the numbers belong to.",
        "properties": {
          "id": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "url": {
            "type": "string"
          }
        },
        "required": [
          "id",
          "label"
        ],
        "title": "Source",
        "type": "object"
      },
      "TokenRequest": {
        "description": "An API key, to trade for an access token.",
        "properties": {
          "clientId": {
            "description": "The API key's client id, `topspin-api-…`.",
            "minLength": 1,
            "type": "string"
          },
          "clientSecret": {
            "description": "The secret shown once when the key was created.",
            "minLength": 1,
            "type": "string"
          }
        },
        "required": [
          "clientId",
          "clientSecret"
        ],
        "title": "TokenRequest",
        "type": "object"
      },
      "TokenResponse": {
        "description": "A Keycloak access token for the topspin API.",
        "properties": {
          "accessToken": {
            "description": "Send in the `Authorization` header with the `Bearer` scheme.",
            "type": "string"
          },
          "expiresIn": {
            "description": "Seconds until the token expires. Fetch a new one rather than refreshing.",
            "type": "integer"
          },
          "tokenType": {
            "type": "string"
          }
        },
        "required": [
          "accessToken",
          "tokenType",
          "expiresIn"
        ],
        "title": "TokenResponse",
        "type": "object"
      },
      "ValidationResult": {
        "description": "What checking a query without running it found.",
        "properties": {
          "diagnostics": {
            "items": {
              "$ref": "#/components/schemas/Diagnostic"
            },
            "type": "array"
          },
          "valid": {
            "description": "True when the query would run: it parses and every name in it resolves.",
            "type": "boolean"
          }
        },
        "required": [
          "valid",
          "diagnostics"
        ],
        "title": "ValidationResult",
        "type": "object"
      }
    },
    "securitySchemes": {
      "bearerAuth": {
        "bearerFormat": "JWT",
        "description": "A Keycloak access token, from `POST /v1/token` or the token endpoint.",
        "scheme": "bearer",
        "type": "http"
      },
      "keycloakClientCredentials": {
        "description": "The API key's client id and secret, traded at Keycloak for an access token.",
        "flows": {
          "clientCredentials": {
            "scopes": {},
            "tokenUrl": "https://auth.topspin.example/realms/topspin/protocol/openid-connect/token"
          }
        },
        "type": "oauth2"
      }
    }
  },
  "info": {
    "description": "Run topspin query-language documents and get typed answers or located diagnostics.\n\nAuthentication: create an API key on topspin's settings page. The key is a Keycloak client (client id + secret). Trade it for an access token with the client-credentials grant — either `POST /v1/token`, or Keycloak's token endpoint directly — and send it in the `Authorization` header with the `Bearer` scheme.",
    "title": "topspin API",
    "version": "1.0.0"
  },
  "openapi": "3.0.3",
  "paths": {
    "/v1/me": {
      "get": {
        "operationId": "getPrincipal",
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Principal"
                }
              }
            },
            "description": "Who an access token acts for."
          },
          "400": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The request body does not match the schema. Never about the query text — see 422."
          },
          "401": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "No access token, or one that was not accepted: expired, for another audience, or for a revoked key."
          },
          "403": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The API key's owner is not approved for the topspin beta."
          },
          "500": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "Something failed on topspin's side."
          },
          "503": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The authorization server or the database is not reachable."
          }
        },
        "security": [
          {
            "bearerAuth": []
          },
          {
            "keycloakClientCredentials": []
          }
        ],
        "summary": "Who the access token acts for",
        "tags": [
          "auth"
        ]
      }
    },
    "/v1/queries/run": {
      "post": {
        "description": "Parses, resolves and runs the source, and returns the answer. Nothing is saved. A query that cannot run as written answers 422 with every problem located in the source.",
        "operationId": "runQuery",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/QueryRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/QueryResult"
                }
              }
            },
            "description": "A query's answer."
          },
          "400": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The request body does not match the schema. Never about the query text — see 422."
          },
          "401": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "No access token, or one that was not accepted: expired, for another audience, or for a revoked key."
          },
          "403": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The API key's owner is not approved for the topspin beta."
          },
          "422": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/QueryRejected"
                }
              }
            },
            "description": "The query cannot run as written. Every diagnostic is located in the source and fixable by editing it."
          },
          "500": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "Something failed on topspin's side."
          },
          "503": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The authorization server or the database is not reachable."
          }
        },
        "security": [
          {
            "bearerAuth": []
          },
          {
            "keycloakClientCredentials": []
          }
        ],
        "summary": "Run a query",
        "tags": [
          "queries"
        ]
      }
    },
    "/v1/queries/validate": {
      "post": {
        "description": "Parses the source and resolves every name in it against the datasets, exactly as the editor does while you type. Nothing is executed. Always 200 for a well-formed request; read `valid` and `diagnostics`.",
        "operationId": "validateQuery",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/QueryRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationResult"
                }
              }
            },
            "description": "What checking a query without running it found."
          },
          "400": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The request body does not match the schema. Never about the query text — see 422."
          },
          "401": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "No access token, or one that was not accepted: expired, for another audience, or for a revoked key."
          },
          "403": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The API key's owner is not approved for the topspin beta."
          },
          "500": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "Something failed on topspin's side."
          },
          "503": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The authorization server or the database is not reachable."
          }
        },
        "security": [
          {
            "bearerAuth": []
          },
          {
            "keycloakClientCredentials": []
          }
        ],
        "summary": "Check a query without running it",
        "tags": [
          "queries"
        ]
      }
    },
    "/v1/token": {
      "post": {
        "description": "Posts the key's client credentials to Keycloak's token endpoint and returns the access token it issues. Identical to calling that endpoint yourself (see the `keycloakClientCredentials` security scheme). Tokens are not refreshable: fetch a new one when `expiresIn` runs out.",
        "operationId": "createToken",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/TokenRequest"
              }
            }
          },
          "description": "An API key, to trade for an access token.",
          "required": true
        },
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TokenResponse"
                }
              }
            },
            "description": "A Keycloak access token for the topspin API."
          },
          "400": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The request body does not match the schema. Never about the query text — see 422."
          },
          "401": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "No access token, or one that was not accepted: expired, for another audience, or for a revoked key."
          },
          "500": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "Something failed on topspin's side."
          },
          "503": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "description": "The authorization server or the database is not reachable."
          }
        },
        "summary": "Exchange an API key for an access token",
        "tags": [
          "auth"
        ]
      }
    }
  },
  "servers": [
    {
      "url": "https://topspin.example/api"
    }
  ],
  "tags": [
    {
      "description": "API keys and access tokens",
      "name": "auth"
    },
    {
      "description": "Validate and run query-language documents",
      "name": "queries"
    }
  ]
}
