Interactive Brokers guide
How to create an IBKR Flex Query and Flex Web Service token
A portfolio app that syncs with Interactive Brokers asks for two things: a Flex Web Service token and the ID of an Activity Flex Query. This guide sets up both in Client Portal in about ten minutes, with the settings that bring in trades, dividends, deposits and cash transfers without double counting.
Last checked against Client Portal and IBKR's Flex Web Service documentation in .
The steps are the ones the Stock Stats Interactive Brokers sync asks for. You need a computer: Flex Queries are set up in Client Portal in a browser.
The settings at a glance
Everything this guide sets, on one card.
- Query type
- Activity Flex Query
- Sections
- Cash Transactions, Trades, Transfers
- Fields
- Select All in each section
- Options
- As they are: just Execution in Trades, just Transfer in Transfers
- Format
- XML
- Period
- Last 365 Calendar Days
- Token lifetime
- 1 year
- IP address
- Blank
Part 1: Create the Activity Flex Query
A Flex Query is a saved report template. You choose the sections and fields once, and the Flex Web Service fills it with your activity whenever an app asks, from data IBKR updates once a day, after the close.
Step 1: Open Flex Queries and add an Activity Flex Query
Log in to Client Portal and open Performance & Reports → Flex Queries. Click the + on Activity Flex Query. Give it a name you will recognise later, such as Stock Stats Sync.
If you manage several accounts, IBKR shows an account selector first. Pick the account you want to track.
Step 2: Add Cash Transactions, Trades and Transfers
Under Sections, click each of these in turn. Each one opens a window with its options and fields.
- Cash Transactions. Deposits, withdrawals, dividends, withholding tax, interest and fees.
- Trades. Every buy and sell, and currency conversions.
- Transfers. Cash and shares moved in or out, to or from another IBKR account or another broker.
In each window, leave Options as they are: just Execution in Trades and just Transfer in Transfers. Tick Select All for the fields, then click Save.
Don't tick extra Options such as Order, Symbol Summary or Summary. They are subtotals of the same activity, so a tool that reads them would count the same activity twice.
Step 3: Set Format to XML and Period to Last 365 Calendar Days
Under Delivery Configuration, leave Format as XML and set Period to Last 365 Calendar Days. Leave the General Configuration settings at IBKR's defaults. In particular, don't change Date Format: the sync reads IBKR's default date format. Click Continue, check the summary, then Create.
Back on the Flex Queries page, copy the Query ID: the number next to the query's name, also shown at the top of its Info pop-over. It is short, such as 800969 in IBKR's documentation.
Check the Period. IBKR offers Last Business Day first in the list. A query left on it returns a valid but empty statement on any quiet day, which looks exactly like a broken sync.
Part 2: Turn on the Flex Web Service and copy the token
The token lets an app ask IBKR for your Flex reports without your Client Portal password.
Step 4: Turn on Flex Web Service
On the Flex Queries page, click the gear icon next to Flex Web Service Configuration. Turn on Flex Web Service Status (a tick box or a switch, depending on your Client Portal) and click Save. The page looks almost empty until you do. Once saved, a Current Token appears.
If Flex Web Service Configuration is missing and you manage linked accounts, select the master account. IBKR may hide it while a sub-account is selected.
Step 5: Copy a token that lasts a year
Check the expiry shown next to the token. IBKR's default lifetime is 6 hours, which means a sync that works today and fails tonight. If it already says a year, copy the token and move on. If the expiry is hours rather than a year:
- Under Should Expire After, choose 1 year.
- Leave Valid For IP Address blank. A phone changes IP address as it moves between Wi-Fi and mobile data, so a restricted token stops working.
- Click Generate New Token.
Copy the new token straight away.
The token is much longer than the Query ID. Keep it private, like a password.
Part 3: Connect it in Stock Stats
Step 6: Paste the token and Query ID, then tap Connect
In Stock Stats, open Settings → Connected accounts → Interactive Brokers. If you have more than one portfolio, choose the one to sync into under Portfolio at the top. Paste the token into Flex token and the Query ID into Query ID, then tap Connect.
The app checks the connection with IBKR before it saves anything. If IBKR turns the request down, nothing is saved and you see why.
After the first sync:
- Today's trades arrive tomorrow. IBKR updates Activity Flex data once a day after the close, so a trade made today shows up in the next day's sync.
- New activity syncs on its own. When you open the app and the last sync is more than 12 hours old, it fetches a new statement. To sync sooner, pull down on Portfolio or tap Sync Now on the Interactive Brokers screen.
- Earlier years load by themselves. One request covers at most a year, so the app then fetches the earlier years itself, a year at a time. IBKR keeps Flex data for the four previous calendar years plus the current one, so in 2026 history reaches back as far as 1 January 2022, or to the day your account started if that is later.
- Duplicates are skipped. Each trade and cash row carries IBKR's own ID, so a row already imported is never added twice.
Interactive Brokers sync is part of the free app. Get Stock Stats on the App Store.
Why these settings
Why Transfers, when it is usually empty
Money moved in from another account (your own other IBKR account or another broker) shows up only in the Transfers section. Without it, that money is missing from your history and the shares it bought look as if they were paid for with borrowed cash.
Why XML
The Stock Stats sync reads only XML. One XML statement carries all three sections. If the query's Format is CSV or Text, the sync can't read what IBKR sends back and shows “Got an unexpected response from IBKR. Please try again.” You can also use this query without a token: in Client Portal, click Run, download the XML file, send it to your iPhone and open Settings → Import Data → Interactive Brokers. For a spreadsheet, make a second query with Format set to CSV.
Why Last 365 Calendar Days
It is the longest period one Flex request can cover: IBKR caps a Flex Web Service request at 365 days. Anything shorter means each sync sees less, and a query on Last Business Day sees only yesterday.
Why Select All
A sync needs particular fields, such as the trade date, quantity, price, commission, currency, buy or sell, asset class and IBKR's trade ID. Select All includes every one of them, and extra fields do no harm.
Token expiry and renewal
A Flex token lasts between 6 hours and 1 year, as you choose when you generate it. When it runs out, IBKR answers with error 1012, Token has expired.
To renew: open Flex Web Service Configuration, choose 1 year, click Generate New Token and paste the new token into every tool that uses it. Your Query ID stays the same.
An account has one current token. Generating a new token invalidates the old one, so if you also use the Flex Web Service in a script or another app, it needs the new token as well.
In Stock Stats, an expired or rejected token shows as Expired on the Interactive Brokers screen and as a banner on Portfolio. The Interactive Brokers row in Settings then reads Reconnect. Open it, paste the new token into Flex token and tap Save Changes. The app checks it with IBKR before saving. Your Query ID stays as it is.
Is a Flex token safe to give an app?
The Flex Web Service is a small reporting API. It generates and returns the Flex Query reports you defined in Client Portal, and nothing else. A Flex token can't place orders, move money or change account settings.
It can read everything in the queries on your account, so treat it as private. In Stock Stats:
- The token is stored in your iPhone's Keychain.
- Requests go from your iPhone straight to IBKR's Flex Web Service. The token and your statements never pass through our servers.
- There is no Stock Stats account. Your portfolio is stored on your device, with optional backup to your own iCloud. See the privacy policy.
To revoke the token, generate a new one in Client Portal or turn Flex Web Service Status off. Disconnect in Stock Stats deletes the token from your iPhone, but it stays valid at IBKR until it expires or you revoke it in Client Portal.
Flex Web Service error codes and fixes
When IBKR turns a request down, it sends a numeric code with a short message. These are the codes IBKR documents, plus 1023, which IBKR sends but doesn't list, and what to do about each.
| Code | IBKR's message | What to do |
|---|---|---|
| 1001, 1004 to 1009, 1021 | Statement could not be generated, is incomplete, data not ready, or server under heavy load. IBKR adds: Please try again shortly. | Temporary. Wait a minute and try again. Your token and query are fine. If a script gets 1001 every time, check the URL (see below). |
| 1003 | Statement is not available. | IBKR has no statement for the dates asked for. Most often the range starts before the account was opened or funded. Don't send the same request again. In our testing, refusals like this count toward the 1025 lock. |
| 1010 | Legacy Flex Queries are no longer supported. Please convert over to Activity Flex. | Create a new Activity Flex Query and use its Query ID. |
| 1011, 1016, 1020 | Service account is inactive. / Account is invalid. / Invalid request or unable to validate request. | Check Flex Web Service Status is on, and that the token and Query ID come from the same account. |
| 1012 | Token has expired. | Generate a new token with a 1-year lifetime. |
| 1013 | IP restriction. | Generate a new token with the IP address field left blank. |
| 1014 | Query is invalid. | Check the Query ID is copied correctly and belongs to an Activity Flex Query on the same account as the token. On linked or advisor accounts, IBKR shows a query only under the same account selection it was created with. |
| 1015 | Token is invalid. | Copy the current token again. A newer token may have replaced it. |
| 1017 | Reference code is invalid. | Start again from SendRequest and use the new reference code it returns. In Stock Stats, tap Sync Now; if the token shows as Expired, paste the current token from Client Portal. |
| 1018 | Too many requests have been made from this token. Please try again shortly. | Wait a few minutes. IBKR allows one request per second and 10 per minute per token. The token itself is fine. |
| 1019 | Statement generation in progress. | Nothing. IBKR builds statements in the background. Stock Stats keeps asking for a short while, then tells you to try again. |
| 1023 | Not in IBKR's published list. The message says the date range cannot exceed 366 days. | Ask for 365 days or fewer per request (fd and td). |
| 1025 | Too many failed attempts. Please review your configuration. | Stop retrying: each attempt keeps the lock on. Create a new Activity Flex Query with the settings above and use its Query ID, or wait. In our testing the lock cleared within 12 hours. |
Calling the Flex Web Service from a script
If every request returns 1001, check the URL. In our tests in July 2026, the retired address https://gdcdyn.interactivebrokers.com/Universal/servlet/FlexStatementService.SendRequest answered 1001 to every request. Use https://ndcdyn.interactivebrokers.com/AccountManagement/FlexWebService/SendRequest with t, q and v=3, then GetStatement at the same path with the reference code as q. IBKR requires a User-Agent header on every request.
About the 1025 lockout
IBKR doesn't publish how many failures trigger 1025 or how long it lasts. In our testing, about ten failed requests in 40 minutes locked a query, and it was clear again within 12 hours. The lock sat on the query, so a new token didn't lift it. The quickest way out is to create a fresh Activity Flex Query with the settings above and use its new Query ID.
Stock Stats limits itself to five failed requests in 24 hours while it loads earlier years, and pauses automatic sync for 12 hours after a 1025.
What the Stock Stats sync brings in
From the three sections above, Stock Stats imports:
- Stock and ETF buys and sells, with commissions.
- Currency conversions, recorded as cash moving from one currency to the other.
- Deposits and withdrawals, including cash transferred in or out.
- Dividends and payments in lieu of dividends, withholding tax, interest, and other fees.
It leaves these out and says so in the sync result:
- Transfers of shares from another account. They come without a price, so you enter them yourself.
- Options, futures and bonds. The app tracks stocks and ETFs. Those rows are counted as rows it could not read.
Corporate actions such as splits are not in these three sections, so the sync never sees them. Enter splits by hand in the app.
With deposits and withdrawals in place, Stock Stats works out your rate per year as an XIRR, a money-weighted return: it counts when you added cash, so a deposit never shows up as a gain. Dividends feed the dividend tracker: income per year and per month, yield and yield on cost.
IBKR Flex Query FAQ
Where do I find my IBKR Flex Query ID?
On the Flex Queries page in Client Portal. The Query ID is the number shown next to the query's name, and IBKR also lists it at the top of the query's Info pop-over. It is a short number, such as 800969 in IBKR's own example. The token is a much longer string, so the two are hard to mix up once you have seen both.
How long does a Flex Web Service token last?
Between 6 hours and 1 year. You pick the lifetime under Should Expire After when you generate it, and IBKR's default is 6 hours. For an app that syncs on its own, choose 1 year.
Can a Flex token place trades or withdraw money?
No. The Flex Web Service only generates and returns reports you have defined as Flex Queries. It has no way to place orders or move money.
Why does my Flex Query return an empty statement?
Usually because the query's Period is on Last Business Day, the first option in IBKR's list. On a day with no trades or cash movements that returns a valid, empty statement. Edit the query and set Period to Last 365 Calendar Days.
Can a Flex Query cover more than one year?
Not in one request. IBKR caps a Flex Web Service request at 365 days. Flex data goes back to the start of the fourth previous calendar year, so older years have to be requested one year at a time. Stock Stats does this for you after the first sync.
Does generating a new Flex token break my other tools?
Yes. An account has one current Flex token, and generating a new one invalidates the old one. Every tool that uses the old token, a spreadsheet script included, needs the new one pasted in.
See what your IBKR account earned
Stock Stats turns your Flex statement into your profit, your rate per year, your dividends and your allocation, on iPhone. Interactive Brokers sync is in the free app. No account to create, and your data stays on your phone.