py-ibkr
A modern, Pydantic-based parser for Interactive Brokers (IBKR) Flex Query reports.
This version replaces the legacy ibflex library with strict type checking and improved support for newer IBKR XML fields.
Features
- Pydantic Models: All data is parsed into typed Pydantic models with validation.
- Robust Parsing: Handles "messy" IBKR data (e.g., legacy enums like
Deposits/Withdrawals, inconsistent date formats). - Forward Compatible: Designed to handle new fields gracefully.
Installation
uv pip install py-ibkr
# or
pip install py-ibkr
Command Line Interface
py-ibkr includes a CLI for downloading Flex Queries, built on
treaty so scripts and AI agents get a stable contract.
export PY_IBKR_TOKEN=your_token # or --token-from-file PATH / --token-from-env VAR
export PY_IBKR_QUERY_ID=your_query_id # or -q / --query-id, or query_id in .py-ibkr.toml
# Download the query's default period
py-ibkr download -o report.xml
# Download a date range (YYYY-MM-DD or YYYYMMDD)
py-ibkr download -o jan.xml --from-date 2026-01-01 --to-date 2026-01-31
- Output: the report is written to
-o/--output; stdout carries a JSON envelope (plain text at a terminal) with the path, size, and the date range actually requested.--format idprints only the path - Dates: IBKR accepts weekdays only and has no data for today, so weekend ends move inward and a to-date of today or later is capped at yesterday, each with a warning in the envelope
- Secrets: the token is never accepted on the command line and is redacted from all output
- Exit codes:
2invalid arguments or settings (nothing was sent),4missing query ID or output directory,10IBKR stopped responding (--request-timeout),11rate limited,79token or query rejected,80locked out after failed attempts,81report still generating after--max-retries,82other IBKR errors - Discovery:
py-ibkr manifestandpy-ibkr download --schemadescribe every flag, output field, and exit code;-v/--verbosebecomes--verbose,-vvbecomes--debug
Setup: Obtaining your Token and Query ID
To use the automated downloader, you must enable the Flex Web Service in your Interactive Brokers account:
- Log in to the IBKR Client Portal.
- Navigate to Performance & Reports > Flex Queries.
- Obtain Token: Click the gear icon or Flex Web Service Configuration. Enable the service and copy the Current Token.
- Obtain Query ID: Create a new Flex Query (e.g., Activity). After saving it, the Query ID will be visible in the list of your Flex Queries.
Usage
Downloading a Flex Query Report
You can automatically download reports using the FlexClient. This requires a Token and Query ID from the IBKR Client Portal.
from py_ibkr import FlexClient, parse
# 1. Initialize the client
client = FlexClient()
# 2. Download the report (handles the request-poll-fetch protocol)
xml_data = client.download(
token="YOUR_IBKR_TOKEN",
query_id="YOUR_QUERY_ID"
)
# 3. Parse the downloaded data
response = parse(xml_data)
print(f"Query Name: {response.queryName}")
Parsing a Flex Query File
for statement in response.FlexStatements: print(f"Account: {statement.accountId}")
# Access Trades
for trade in statement.Trades:
print(f"Symbol: {trade.symbol}, Quantity: {trade.quantity}, Price: {trade.tradePrice}")
# Access Cash Transactions
for cash_tx in statement.CashTransactions:
print(f"Type: {cash_tx.type}, Amount: {cash_tx.amount}")
### Models
You can import models directly for type hinting:
```python
from py_ibkr import Trade, CashTransaction
def process_trade(trade: Trade):
print(trade.symbol)
Metadata
Release files for py-ibkr 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py_ibkr-0.2.1.tar.gz | 28.6 kB | Without provenance | Details for py_ibkr-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py_ibkr-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for py_ibkr-0.2.1-py3-none-any.whl |
Total release size: 48.7 kB
Release files / py_ibkr-0.2.1.tar.gz
| Download URL | py_ibkr-0.2.1.tar.gz |
|---|---|
| Size | 28.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
79281611b6227ca3179a8d71eb7d103f410f883db45732ee7ba0e4c0d9c711a3
|
|
BLAKE2b-256 checksum How to use checksums |
8ef1d2dfe99e4e09b103df4f6092628bc1b0b45a8d98ebf16ab9b6b674ea3349
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / py_ibkr-0.2.1-py3-none-any.whl
| Download URL | py_ibkr-0.2.1-py3-none-any.whl |
|---|---|
| Size | 20.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1e0b3f3681ce9b5d154789a139dffe5a32c33b66472a618ede3aa3b5c04ef4f1
|
|
BLAKE2b-256 checksum How to use checksums |
615774aa5137933caa938308ca30ade8f4c6ddbe1a5f2ee028341d47781c1ef3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for py-ibkr 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.