PYTHON WORKFLOW · NO EXTRA PACKAGES
Get insider trading data with Python
A standard-library Python example for filtering and paginating SEC insider transactions while preserving decimal strings, footnotes and immutable snapshot IDs.
Sources checked October 27, 2026 · SEC API editorial

Retrieve purchases without losing source precision.
This example uses Python’s standard library. Set SEC_API_KEY in your environment, then request the imported 2026 Q3 archive. It reads at most ten pages and fails explicitly when that budget is exhausted, so partial output cannot quietly masquerade as the complete population.
import json
import os
from urllib.parse import urlencode
from urllib.request import Request, urlopen
key = os.environ["SEC_API_KEY"]
params = {"period": "2026q3", "transactionCode": "P",
"form": "4", "limit": 100, "offset": 0}
# Bounded example: at most 10 pages / 1,000 rows.
for _ in range(10):
request = Request(
"https://sec-api.dev/api/v1/insider-transactions?" + urlencode(params),
headers={"Authorization": "Bearer " + key})
with urlopen(request, timeout=60) as response:
payload = json.load(response)
for row in payload["data"]:
# Keep original strings, source URLs, and footnotes intact.
print(json.dumps(row))
next_offset = payload["pagination"].get("nextOffset")
if next_offset is None:
break
params["offset"] = next_offset
params["snapshot"] = payload["meta"]["snapshotId"]
else:
raise RuntimeError("Page budget reached; output is intentionally partial")Set SEC_API_KEY on your server. This builder does not execute requests.
The code prints source rows as JSON. Do not cast monetary strings to binary floating point just to export them. Use Decimal only when you have checked units, missing values and the instrument represented by each source field.
Keep pagination tied to one import.
Follow pagination.nextOffset, then send meta.snapshotId back as snapshot. If the server returns snapshot_changed, restart the traversal rather than combining pages from different archive versions. Handle 429 with its Retry-After response and retry transient failures with a bounded backoff. Do not automatically retry authentication or invalid-filter errors.
transactionCode=P and form=4 select one exact code and form. This does not include P/K composite codes or Form 4/A amendments. Query those explicitly when your research requires them; preserve their identities instead of deduplicating by date and price.
Build a reproducible export.
- Record quarter, filters, snapshot ID and source archive hash.
- Retain every source accession and row identity with its footnotes.
- Write a separate import log with the page budget and completion status.
- Validate empty results before concluding that no transactions occurred.
Read one ownership XML filing with JavaScript ↗ · Response fields and errors ↗
RESEARCH CHECKLIST
From source row to usable evidence.
| Keep this field | Why it matters | Common analytical mistake |
|---|---|---|
| Accession and source row | Makes a result traceable to its filing and table. | Deduplicating unrelated records because their dates and amounts match. |
| Transaction code and direction | Separates purchases, awards, exercises and withholding. | Treating every acquisition as a discretionary purchase. |
| Derivative versus nonderivative | Keeps instrument categories distinct. | Adding option-related activity directly to common-share activity. |
| Ownership and footnotes | Preserves direct/indirect relationships and explanatory context. | Assigning a transaction to the wrong person or ignoring a qualification. |
| Snapshot and filters | Lets another analyst reproduce the same traversal. | Mixing pages across different imports or silently truncating results. |
Before you ship a screener
- Test an empty result and an invalid filter separately; they represent different outcomes.
- Store the query, snapshot and completion status next to your export.
- Inspect representative source footnotes before assigning investment meanings to codes.
- Separate filings, transaction rows, reporting owners and unique securities in every count.
Can these records establish insider trading or investment returns?
No. A disclosed transaction alone does not establish unlawful trading. Returns require additional execution, position, corporate-action and valuation information. These pages show reported activity and its source context; they do not supply a verified performance history.
Sources, coverage and next steps
SEC insider transaction archive catalog ↗ · Official Form 4 instructions ↗ · Our data-quality limits ↗
Corporate insider ownership reports do not by themselves establish illegal trading. Do not conflate this API with congressional financial disclosures.