Skip to main content

ibkr-flex-client

Minimal async Python client for Interactive Brokers' Flex Web Service.

Handles the two-step API flow (SendRequest → GetStatement) with automatic retry on rate limits and processing delays. Returns a thin XML wrapper — you bring your own Pydantic models for parsing.

Prerequisites

You need a Flex Web Service token and at least one query ID from IB Account Management → Reports → Flex Queries → Flex Web Service. The token is account-scoped; each query ID corresponds to a specific report configuration.

Installation

pip install -e path/to/ibkr-flex-client

Or as a git submodule:

git submodule add git@github.com:vjt/ibkr-flex-client.git vendor/ibkr-flex-client
pip install -e vendor/ibkr-flex-client

Requires Python 3.12+ and aiohttp.

Usage

import aiohttp
from ibkr_flex_client import FlexClient, FlexStatement, FlexError

async def main():
    client = FlexClient(token="YOUR_TOKEN", query_id="YOUR_QUERY_ID")

    async with aiohttp.ClientSession() as session:
        statement: FlexStatement = await client.fetch(session)

    # Statement metadata
    print(statement.account_id)  # "U1234567"
    print(statement.from_date)   # date(2026, 1, 1)
    print(statement.to_date)     # date(2026, 3, 27)

    # Iterate over XML elements and parse into your own models
    for elem in statement.iter("Trade"):
        symbol = elem.get("symbol")
        quantity = float(elem.get("quantity", "0"))
        price = float(elem.get("tradePrice", "0"))
        print(f"{symbol}: {quantity} @ {price}")

    # Raw XML is available if needed
    print(len(statement.xml), "bytes")

API

FlexClient(*, token, query_id, max_retries=10, backoff_base=10.0)

Async client for IB's Flex Web Service.

  • token: Flex Web Service token from IB Account Management.
  • query_id: Flex Query ID for the specific report.
  • max_retries: Max poll attempts for GetStatement (default 10).
  • backoff_base: Base seconds for exponential backoff (default 10).

await client.fetch(session) -> FlexStatement

Fetches the report. Handles rate limiting (IB error 1018) and still-processing (1019) with automatic retry. Raises FlexError on auth failures, timeouts, or malformed responses.

FlexStatement(xml)

Thin wrapper around the IB FlexQuery XML response.

Attribute Type Description
account_id str IB account ID
from_date date Statement start date
to_date date Statement end date
xml str Raw XML string

statement.iter(tag) -> Iterator[Element]

Iterates all XML elements matching tag. Use this to extract trade fills, NAV entries, conversion rates, or any other section from the Flex report. Element attributes are strings — cast them to your own types.

FlexError

Exception raised on all API and parsing errors.

parse_ib_date(date_str) -> date

Parses IB's yyyyMMdd format. Raises ValueError on bad input.

Error Handling

IB's Flex Web Service has aggressive rate limiting. The client handles this transparently:

IB Error Meaning Client Behavior
1018 Rate limited Exponential backoff, retry
1019 Still processing Fixed short backoff, retry
1012 Token expired Raises FlexError immediately
Other Various failures Raises FlexError immediately

Testing

pip install -e ".[dev]"
pytest tests/ -x -q

License

MIT

Metadata

Release files for ibkr-flex-client 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ibkr-flex-client 0.1.0
File Size Uploaded Provenance
ibkr_flex_client-0.1.0.tar.gz 8.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ibkr-flex-client 0.1.0
File Interpreter ABI Platform
ibkr_flex_client-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.5 kB

Release files / ibkr_flex_client-0.1.0.tar.gz

Download URL ibkr_flex_client-0.1.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
aaf5d5bffa5602d5b958859faa21d910c4cbba9cef0c8c36eab60926f17f5ca7
BLAKE2b-256 checksum
How to use checksums
d5f510fc5e88fe4611ec088eda3de04660f3caa0f41cfe3c9ba16949833bdb5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / ibkr_flex_client-0.1.0-py3-none-any.whl

Download URL ibkr_flex_client-0.1.0-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df05a790d918c66f7f641fc5386125ef7e401f6feecd143217a1016f58913387
BLAKE2b-256 checksum
How to use checksums
fb499b471a91bbf31c8f9f2f8fc1345f51c4a0efa1dbd77e154e0c353103d9cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Security for ibkr-flex-client 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page