{
  "openapi": "3.0.3",
  "info": {
    "title": "PolyTest API",
    "description": "PolyTest is a Polymarket backtesting API that lets traders replay historical orderbooks and test trading strategies on crypto prediction markets. 240M+ snapshots, full orderbook depth (up to 15 levels bid/ask), sub-second resolution. Supports BTC, ETH & SOL. Clean REST API with JSON responses. Works seamlessly with Cursor, Claude Code, Codex, Gemini CLI, OpenClaw and other AI coding tools. Typed TypeScript client: @polytest/sdk (https://www.polytest.io/docs/sdk).",
    "version": "1.0.0",
    "contact": {
      "name": "PolyTest Support",
      "url": "https://www.polytest.io/contact",
      "email": "support@polytest.io"
    },
    "license": {
      "name": "Proprietary"
    },
    "x-logo": {
      "url": "https://www.polytest.io/icon.svg"
    }
  },
  "servers": [
    {
      "url": "https://api.polytest.io/api/v1",
      "description": "Production"
    }
  ],
  "security": [
    {
      "ApiKeyAuth": []
    }
  ],
  "paths": {
    "/markets": {
      "get": {
        "operationId": "listMarkets",
        "summary": "List prediction markets",
        "description": "List Polymarket crypto Up/Down prediction markets with filtering by coin, market type, and time range. Supports pagination. Limit values above the maximum are clamped server-side; the response includes an X-Limit-Clamped header and a warning field when this happens.",
        "tags": ["Markets"],
        "parameters": [
          {
            "name": "coin",
            "in": "query",
            "description": "Filter by cryptocurrency: btc, eth, sol (xrp coming soon)",
            "schema": { "type": "string", "enum": ["btc", "eth", "sol", "xrp"], "default": "btc" }
          },
          {
            "name": "market_type",
            "in": "query",
            "required": true,
            "description": "Market duration: 5m, 15m, 1hr, 4hr, 24hr",
            "schema": { "type": "string", "enum": ["5m", "15m", "1hr", "4hr", "24hr"] }
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Results per page (1-1000). Values above 1000 are clamped to 1000.",
            "schema": { "type": "integer", "default": 100, "minimum": 1, "maximum": 1000 }
          },
          {
            "name": "offset",
            "in": "query",
            "description": "Rows to skip for pagination",
            "schema": { "type": "integer", "default": 0, "minimum": 0 }
          },
          {
            "name": "start_time",
            "in": "query",
            "description": "Inclusive lower bound on each market's opening time (ISO 8601 / RFC 3339). Maximum lookback depends on your plan.",
            "schema": { "type": "string", "format": "date-time" }
          },
          {
            "name": "end_time",
            "in": "query",
            "description": "Inclusive upper bound on each market's opening time (ISO 8601 / RFC 3339). If no markets fall in range for your plan, results may be empty and include a warning.",
            "schema": { "type": "string", "format": "date-time" }
          }
        ],
        "responses": {
          "200": {
            "description": "List of markets",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "markets": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/Market" }
                    },
                    "total": { "type": "integer", "description": "Total matching markets for pagination, scoped to your plan and filters" },
                    "limit": { "type": "integer", "description": "Applied limit. May be lower than the requested value if clamped." },
                    "offset": { "type": "integer" },
                    "warning": { "type": "string", "description": "Optional notice when limits or your plan affect the response (e.g. adjusted page size or no rows for the requested range)" }
                  }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/markets/{id}": {
      "get": {
        "operationId": "getMarketById",
        "summary": "Get market by ID",
        "description": "Get a single Polymarket crypto Up/Down market by its numeric ID.",
        "tags": ["Markets"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Numeric market ID",
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Market details",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Market" }
              }
            }
          },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    },
    "/markets/by-slug/{slug}": {
      "get": {
        "operationId": "getMarketBySlug",
        "summary": "Get market by slug",
        "description": "Get a single market by its human-readable slug (e.g. btc-updown-5m-1772689200).",
        "tags": ["Markets"],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "description": "Human-readable market slug",
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Market details",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Market" }
              }
            }
          },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    },
    "/markets/{id}/snapshots": {
      "get": {
        "operationId": "listSnapshots",
        "summary": "List historical snapshots",
        "description": "List historical price snapshots for a Polymarket Up/Down market, sorted by time ascending. Optionally include full orderbook depth (up to 15 levels bid/ask). Ideal for backtesting trading strategies with realistic fill simulation. The `total` field is an estimated count — paginate with `offset` until the returned array has fewer items than `limit` to determine the true last page. Values of `limit` above 500 are clamped server-side; the response includes an X-Limit-Clamped header and a warning field when this happens.",
        "tags": ["Snapshots"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Market ID to fetch snapshots for",
            "schema": { "type": "string" }
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Results per page (1-500). Values above 500 are clamped to 500.",
            "schema": { "type": "integer", "default": 100, "minimum": 1, "maximum": 500 }
          },
          {
            "name": "offset",
            "in": "query",
            "description": "Rows to skip for pagination",
            "schema": { "type": "integer", "default": 0 }
          },
          {
            "name": "start_time",
            "in": "query",
            "description": "ISO 8601 lower bound (inclusive)",
            "schema": { "type": "string", "format": "date-time" }
          },
          {
            "name": "end_time",
            "in": "query",
            "description": "ISO 8601 upper bound (inclusive)",
            "schema": { "type": "string", "format": "date-time" }
          },
          {
            "name": "include_orderbook",
            "in": "query",
            "description": "Include full orderbook depth (up to 15 levels bid/ask per side)",
            "schema": { "type": "string", "enum": ["true", "false"], "default": "false" }
          }
        ],
        "responses": {
          "200": {
            "description": "List of snapshots",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "snapshots": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/Snapshot" }
                    },
                    "total": { "type": "integer", "description": "Estimated total count (PostgreSQL planner statistics — fast but approximate). Paginate with offset until the returned array has fewer items than limit to find the last page." },
                    "limit": { "type": "integer", "description": "Applied limit. May be lower than the requested value if clamped." },
                    "offset": { "type": "integer" },
                    "warning": { "type": "string", "description": "Present when limit was clamped to the maximum (500)" }
                  }
                }
              }
            }
          },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/markets/{id}/snapshots/at": {
      "get": {
        "operationId": "getSnapshotAt",
        "summary": "Get snapshot at timestamp",
        "description": "Find the closest snapshot to a given timestamp within a +/-2 second window. Useful for point-in-time lookups when backtesting.",
        "tags": ["Snapshots"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Market ID to search within",
            "schema": { "type": "string" }
          },
          {
            "name": "timestamp",
            "in": "query",
            "required": true,
            "description": "ISO 8601 target timestamp",
            "schema": { "type": "string", "format": "date-time" }
          },
          {
            "name": "include_orderbook",
            "in": "query",
            "description": "Include full orderbook depth",
            "schema": { "type": "string", "enum": ["true", "false"], "default": "false" }
          }
        ],
        "responses": {
          "200": {
            "description": "Closest snapshot",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "snapshot": { "$ref": "#/components/schemas/Snapshot" }
                  }
                }
              }
            }
          },
          "404": {
            "description": "No snapshot found within +/-2 seconds",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" }
              }
            }
          }
        }
      }
    },
    "/health": {
      "get": {
        "operationId": "healthCheck",
        "summary": "Health check",
        "description": "Health check endpoint. No authentication required.",
        "tags": ["Health"],
        "security": [],
        "responses": {
          "200": {
            "description": "Service healthy",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": { "type": "string", "example": "ok" },
                    "timestamp": { "type": "string", "format": "date-time" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/limits": {
      "get": {
        "operationId": "getLimits",
        "summary": "Get plan limits",
        "description": "Returns the plan tier of the API key and the limits that come with it: rate limits, concurrent requests, how many of the most recent markets of each type the key can read, and how many active keys the account can hold. null means unlimited; 0 means the market type is not included in the plan. trial_budget lists the 3-day free trial's total caps. It has no remaining count because this endpoint never reads the trial meter; during a trial, the X-Trial-Budget-Requests-Remaining, X-Trial-Budget-Records-Remaining and X-Trial-Budget-Bytes-Remaining headers on /markets responses carry the balance. Calls count toward the rate limit but not the trial budget. Responses carry Cache-Control: private, max-age=300.",
        "tags": ["Account"],
        "responses": {
          "200": {
            "description": "Limits for the API key's plan",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Limits" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/SubscriptionInactive" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/ServiceUnavailable" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "API key from https://www.polytest.io/app/keys. Requires an active trial or subscription (3-day free trial available)."
      }
    },
    "schemas": {
      "Market": {
        "type": "object",
        "properties": {
          "market_id": { "type": "string", "example": "1496946" },
          "event_id": { "type": "string", "example": "245081" },
          "slug": { "type": "string", "example": "btc-updown-5m-1772689200" },
          "market_type": { "type": "string", "enum": ["5m", "15m", "1hr", "4hr", "24hr"] },
          "coin": { "type": "string", "enum": ["btc", "eth", "sol", "xrp"] },
          "start_time": { "type": "string", "format": "date-time" },
          "end_time": { "type": "string", "format": "date-time" },
          "price_start": { "type": "number", "example": 72404.54 },
          "price_end": { "type": "number", "example": 72423.59 },
          "winner": { "type": "string", "enum": ["Up", "Down", null] },
          "final_volume": { "type": "number" },
          "final_liquidity": { "type": "number" },
          "condition_id": { "type": "string" },
          "clob_token_up": { "type": "string" },
          "clob_token_down": { "type": "string" },
          "resolved_at": { "type": "string", "format": "date-time" },
          "created_at": { "type": "string", "format": "date-time" },
          "updated_at": { "type": "string", "format": "date-time" }
        }
      },
      "Snapshot": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "market_id": { "type": "string" },
          "time": { "type": "string", "format": "date-time", "description": "Millisecond-precision timestamp" },
          "spot_price": { "type": "number", "description": "Underlying coin price (e.g. BTC spot)" },
          "price_up": { "type": "number", "description": "Best ask for the Up token (0-1), read as the implied probability; null when there are no asks" },
          "price_down": { "type": "number", "description": "Best ask for the Down token (0-1), read as the implied probability; null when there are no asks" },
          "orderbook_up": { "$ref": "#/components/schemas/Orderbook" },
          "orderbook_down": { "$ref": "#/components/schemas/Orderbook" }
        }
      },
      "Orderbook": {
        "type": "object",
        "description": "Full orderbook depth with up to 15 levels per side",
        "properties": {
          "bids": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/OrderbookLevel" },
            "description": "Up to 15 bid levels, sorted by price descending"
          },
          "asks": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/OrderbookLevel" },
            "description": "Up to 15 ask levels, sorted by price ascending"
          }
        }
      },
      "OrderbookLevel": {
        "type": "object",
        "properties": {
          "price": { "type": "number", "example": 0.52 },
          "size": { "type": "number", "example": 1200 }
        }
      },
      "Limits": {
        "type": "object",
        "description": "Limits for the plan behind the API key. null means unlimited.",
        "properties": {
          "tier": { "type": "string", "enum": ["free", "builder", "pro", "enterprise"], "example": "builder" },
          "rate_limits": {
            "type": "object",
            "properties": {
              "requests_per_minute": { "type": "integer", "description": "Requests allowed per minute", "example": 250 },
              "burst_per_second": { "type": "integer", "description": "Requests allowed per second in a burst", "example": 12 }
            }
          },
          "concurrency": {
            "type": "object",
            "properties": {
              "max_concurrent": { "type": "integer", "nullable": true, "description": "Requests that can be in flight at once. null means unlimited.", "example": 1 }
            }
          },
          "data_access": {
            "type": "object",
            "description": "How many of the most recent markets of each type the plan can read. null means unlimited; 0 means the market type is not included.",
            "properties": {
              "5m_markets": { "type": "integer", "nullable": true, "example": 4100 },
              "15m_markets": { "type": "integer", "nullable": true, "example": 1400 },
              "1hr_markets": { "type": "integer", "nullable": true, "example": 350 },
              "4hr_markets": { "type": "integer", "nullable": true, "example": 85 },
              "24hr_markets": { "type": "integer", "nullable": true, "example": 14 },
              "snapshots_per_market": { "type": "integer", "nullable": true, "description": "Snapshot cap per market. null means unlimited." }
            }
          },
          "api_keys": {
            "type": "object",
            "properties": {
              "max_active": { "type": "integer", "nullable": true, "description": "Active API keys the account can hold. null means unlimited.", "example": 3 }
            }
          },
          "trial_budget": {
            "type": "object",
            "description": "Total caps for the 3-day free trial. Returned on every plan but enforced only during a trial. There is no remaining count because this endpoint never reads the trial meter.",
            "properties": {
              "requests": { "type": "integer", "example": 20000 },
              "records": { "type": "integer", "example": 2000000 },
              "bytes": { "type": "integer", "format": "int64", "example": 3221225472 }
            }
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" },
          "message": { "type": "string", "description": "Human-readable detail, sent with 403, 429 and 503 responses" },
          "retry_after": { "type": "integer", "description": "Seconds to wait before retrying, sent with rate-limit and database-unavailable responses" },
          "max_concurrent": { "type": "integer", "description": "Your plan's concurrent request limit, sent when it is exceeded" }
        }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Missing or invalid parameters",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "Unauthorized": {
        "description": "Missing or invalid API key",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "NotFound": {
        "description": "Resource not found",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "RateLimited": {
        "description": "Rate limit or concurrency limit exceeded. Back off and retry. Check X-RateLimit-Reset header.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "SubscriptionInactive": {
        "description": "The API key is valid but the account's trial or subscription is not active",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "ServiceUnavailable": {
        "description": "Service temporarily unavailable. Retry shortly; a Retry-After header is sent when the database is under heavy load.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "Markets",
      "description": "Query Polymarket crypto Up/Down prediction markets. Filter by coin (BTC, ETH, SOL) and timeframe (5m, 15m, 1h, 4h, 1d)."
    },
    {
      "name": "Snapshots",
      "description": "Historical price snapshots with optional full orderbook depth (up to 15 levels bid/ask). Sub-second resolution for precise backtesting."
    },
    {
      "name": "Health",
      "description": "Service health check endpoint."
    },
    {
      "name": "Account",
      "description": "Plan limits for the API key in use."
    }
  ],
  "externalDocs": {
    "description": "Full documentation and quickstart guide",
    "url": "https://www.polytest.io/docs"
  }
}
