Answers

IBKR Flex Query Setup: Token, Query ID and Errors

Turn on the Flex Web Service in Client Portal for a token, save an Activity Flex Query for its Query ID, then call SendRequest and GetStatement, per IBKR's docs.

Checked Not affiliated with Interactive Brokers.

Turn it onWherePerformance & Reports > Flex Queries, the gear on Flex Web Service Configuration, the toggleYou getA token
Build the queryWhere+ beside Activity Flex Query, sections, Continue, CreateYou getA saved query
Copy its IDWhereThe info icon beside itYou getThe Query ID
Fetch itWhereThe two calls belowYou getUp to 365 days per call
Which sections to pick3 rows
PositionsSectionOpen PositionsFieldsSymbol, Quantity, Mark Price, Position Value, Cost Basis Price, FIFO Unrealized PNL, Currency, FX Rate to Base
Account valueSectionChange in NAVFieldsStarting Value, Deposits/Withdrawals, Dividends, Commissions, Forex Translation, Ending Value, TWR
CashSectionCash ReportFieldsStarting Cash, Deposits, Withdrawals, Ending Cash, Ending Settled Cash, by currency

The two calls

bash
BASE=https://ndcdyn.interactivebrokers.com/AccountManagement/FlexWebService
curl -s -A "curl/8" "$BASE/SendRequest?t=$FLEX_TOKEN&q=$QUERY_ID&v=3"
# <Status>Success</Status> <ReferenceCode>1234567890</ReferenceCode>
sleep 10
curl -s -A "curl/8" "$BASE/GetStatement?t=$FLEX_TOKEN&q=1234567890&v=3" -o statement.xml

q is the Query ID, then the ReferenceCode. Send a User-Agent and v=3. Failures answer HTTP 200 too: a fake token sent on 5 November 2026 got 1015, Token is invalid.

Token life, dates and limits5 items
  • Generate A New Token sets its life, 6 hours to 1 year, and optionally one IP address. The old token stops working.
  • fd=yyyymmdd&td=yyyymmdd sets the range; send both.
  • p=N asks for the last N days instead. Either reaches 365 days per request.
  • Saved queries cover the four previous calendar years and this one.
  • One request a second and 10 a minute per token, or error 1018.
Every error code21 rows
1001IBKR's messageStatement could not be generated at this time. Please try again shortly.Check firstRetry later; IBKR gives no reason
1003IBKR's messageStatement is not available.Check firstThe query's period and accounts
1004IBKR's messageStatement is incomplete at this time. Please try again shortly.Check firstRetry later
1005IBKR's messageSettlement data is not ready at this time. Please try again shortly.Check firstRetry later
1006IBKR's messageFIFO P/L data is not ready at this time. Please try again shortly.Check firstRetry later
1007IBKR's messageMTM P/L data is not ready at this time. Please try again shortly.Check firstRetry later
1008IBKR's messageMTM and FIFO P/L data is not ready at this time. Please try again shortly.Check firstRetry later
1009IBKR's messageThe server is under heavy load. Statement could not be generated at this time. Please try again shortly.Check firstRetry later
1010IBKR's messageLegacy Flex Queries are no longer supported. Please convert over to Activity Flex.Check firstRebuild it as an Activity Flex Query
1011IBKR's messageService account is inactive.Check firstThe Flex Web Service toggle is on
1012IBKR's messageToken has expired.Check firstGenerate a new token
1013IBKR's messageIP restriction.Check firstThe token's IP address matches the caller
1014IBKR's messageQuery is invalid.Check firstThe Query ID, under the same account selection
1015IBKR's messageToken is invalid.Check firstThe Current Token, not an older one
1016IBKR's messageAccount in invalid.Check firstThe accounts in the query
1017IBKR's messageReference code is invalid.Check firstThe ReferenceCode goes in q of GetStatement
1018IBKR's messageToo many requests have been made from this token. Please try again shortly. Limited to one request per second, 10 requests per minute (per token).Check firstSlow down
1019IBKR's messageStatement generation in progress. Please try again shortly.Check firstWait, then call GetStatement again
1020IBKR's messageInvalid request or unable to validate request.Check firstThe URL, v=3 and the User-Agent; IBKR gives no reason
1021IBKR's messageStatement could not be retrieved at this time. Please try again shortly.Check firstRetry later
1025IBKR's messageToo many failed attempts.Check firstStop, then check the token and Query ID
Advisor and linked accounts3 items
  • Only the master account sees Flex Web Service Configuration; subaccounts do not.
  • One token can fetch every linked account the query includes.
  • A query shows only under the account selection it was created with.

Where Gloom fits

Gloom's Interactive Brokers plugin takes the same token and Query ID, from an XML query, under Flex (read-only) in Brokers (BR).

The Brokers pane in Gloom connecting Interactive Brokers in Flex (read-only) mode, with fields for the Flex Token, Query ID and Endpoint.
Captured in the Gloom terminal app, version 0.20.1.

Gloom covers

  • Positions with cost, mark, unrealized P&L and FX rate.
  • Net liquidation and cash.
  • Both calls, the wait and plain errors.

Gloom does not cover

  • The browser version: Flex needs the app or the terminal.
  • Trades, or anything newer than the statement.
  • Sources5 sources
  • Not verified3 claims
    • What causes error 1020 when the URL is right. A reply on 11 July 2026 blamed selecting every field in Open Positions (r/interactivebrokers).
    • Error 1001 from a second IP address with no IP restriction, reported on 9 May 2026 (r/interactivebrokers). No cause was confirmed.
    • That Gloom needs Open Positions at the Summary level of detail. It reads every row as a position; not tested on an account.