Skip to content

Docs/Historical SQL Quickstart

API quickstarts

Historical SQL Quickstart

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

Open productMarkdown
On this page

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. Paid (active) access covers only the past 15 days (360 elapsed hours, including weekends and holidays). Older trades are excluded before aggregation; trial access is unchanged. See the Historical API reference for the complete policy.

1. Start with a bounded query

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

SQL
SELECT date, time, symbol, put_call, strike, expiration_date, size, price, bid, ask
FROM RawOptionTrades
WHERE date = (
  SELECT max(date) FROM RawOptionTrades
  WHERE symbol = 'AAPL' AND date >= today() - 7
)
  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. Bound a latest-date lookup the same way, or read the latest date from RawOptionTradesMaxDateOnlyMV: an unbounded SELECT max(date) FROM RawOptionTrades reads the whole table and returns QUERY_TOO_BROAD.

2. Send the request

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

Shell
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 WHERE symbol = 'AAPL' AND date >= today() - 7) 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. Paid queries must target the past 15 days; a range crossing that boundary includes only eligible trades.
  • 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 scan limits were exceededFollow reason / hints; narrow filters or split the time window before retrying
422 INVALID_QUERYClickHouse rejected the expressionCorrect the reported query problem
429Rate limitedHonor Retry-After
504Query timed outSplit the request into smaller windows

For QUERY_TOO_BROAD, the response includes reason: "SCAN_LIMIT_EXCEEDED" and deterministic hints. Narrow the date range, add selective filters such as symbol, or split a large request into smaller time windows before retrying. Avoid automatically retrying the same unchanged query.

LIMIT is still recommended to cap returned rows, but it does not necessarily reduce the number of rows the server must scan. Do not treat a smaller LIMIT as the primary fix for QUERY_TOO_BROAD.

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: