跳到主要内容

Docs/Historical SQL Quickstart

Historical SQL Quickstart

Run a bounded, authenticated Historical SQL request against RawOptionTrades and recover safely from validation, read-limit, or timeout errors.

Open product page →View as Markdown

Ask ChatGPT or Claude Code

Copy this prompt, paste it into ChatGPT, Claude, Claude Code, Cursor, or Codex, then add your question. It tells the model to read our public docs first — no API key needed for that step.

You are helping me use OptionData (https://www.optiondata.io/), an OPRA-licensed U.S. equity options data API.

Before answering, fetch these public files (no login required) and treat them as the source of truth:
- https://www.optiondata.io/llms.txt — short product map (same content as https://www.optiondata.io/llm.txt)
- https://www.optiondata.io/llms-full.txt — full API reference
- https://www.optiondata.io/openapi.json — HTTP OpenAPI

Do not invent endpoints, fields, tables, or limits. Prefer `Authorization: Bearer apikey_…` for HTTP APIs. Realtime uses `wss://ws.optiondata.io` with a `token` query parameter.

Products:
- Realtime trades WebSocket: wss://ws.optiondata.io
- Historical SQL: POST https://www.optiondata.io/api/historical/sql
- Option chain: POST https://www.optiondata.io/api/option-chain
- Market structure: GET https://www.optiondata.io/api/v1/market-structure/{symbol}

I am asking about: Historical SQL Quickstart
- Markdown: https://www.optiondata.io/md/historical-sql-quickstart/
- HTML docs: https://www.optiondata.io/docs/historical-sql-quickstart/

My question:

Historical SQL accepts read-only SELECT queries over HTTPS. The endpoint applies server-owned row, byte, and execution-time limits, and every visible trade is delayed by at least 15 minutes.

1. Start with a bounded query

This query filters by trading date and symbol, selects explicit columns, and limits output:

SELECT date, time, symbol, put_call, strike, expiration_date, size, price, bid, ask
FROM RawOptionTrades
WHERE date = (SELECT max(date) FROM RawOptionTrades)
  AND symbol = 'AAPL'
ORDER BY time DESC
LIMIT 20

Filtering by date and symbol helps ClickHouse prune work. LIMIT caps returned rows, while OptionData's server-enforced read limits bound the scan itself.

2. Send the request

Store the key in OPTIONDATA_API_KEY, then use the canonical bearer header:

curl -X POST https://www.optiondata.io/api/historical/sql \
  -H "Authorization: Bearer $OPTIONDATA_API_KEY" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "sql=SELECT date, time, symbol, put_call, strike, expiration_date, size, price, bid, ask FROM RawOptionTrades WHERE date = (SELECT max(date) FROM RawOptionTrades) AND symbol = 'AAPL' ORDER BY time DESC LIMIT 20"

A successful response has status: "SUCCESS", a data array, and entitlement/limit information in meta.

Annotated OptionData historical-data page showing the REST endpoint and full SQL documentation link

Start from the documented REST endpoint, then use the bounded query shown below.

Trial responses are capped at 10 rows even when the SQL uses a larger LIMIT.

3. Keep queries safe

  • Query only published, whitelisted tables and columns.
  • Filter by date and symbol before widening the request.
  • Select only the columns you need.
  • Include a reasonable LIMIT.
  • Do not include semicolons, SQL comments, mutations, UNION, or query-level SETTINGS.

The public Historical Option Trades API reference is the schema source of truth for this restricted endpoint; system-table discovery is not part of the customer query surface.

4. Recover from errors

StatusMeaningNext action
400SQL failed the public query guardCorrect the statement or column names
401Missing or invalid API keyCheck the bearer header
403No active/trialing Pro entitlementReview Billing
422 QUERY_TOO_BROADServer read limits were exceededNarrow date, symbol, expiry, or strike filters
422 INVALID_QUERYClickHouse rejected the expressionCorrect the reported query problem
429Rate limitedHonor Retry-After
504Query timed outSplit the request into smaller windows

Avoid repeatedly retrying the same broad query. Narrow it first.