Before you start
- An IBKR account (a paper account is created alongside a live one)
- A machine that can run TWS or IB Gateway (Windows, macOS or Linux)
- Market data subscriptions for the exchanges you will query
Create your Interactive Brokers API key
- 1
Download IB Gateway or TWS
Download the stable IB Gateway (recommended for bots) or Trader Workstation from interactivebrokers.com and install it.
- 2
Log in to the paper account
Start the app and choose Paper Trading at the login screen, using your paper username.
- 3
Enable socket clients
In TWS open File / Edit → Global Configuration → API → Settings and tick Enable ActiveX and Socket Clients. IB Gateway has the same API settings page.
- 4
Check the port
Note the Socket port: 7497 for paper TWS, 4002 for paper Gateway. Keep Allow connections from localhost only ticked unless your bot runs elsewhere.
- 5
Decide on read-only
Untick Read-Only API only when you are ready to place orders. Leave it on while testing data access.
- 6
Save the settings in AlgoGP or .env
Store
IBKR_HOST,IBKR_PORTand a uniqueIBKR_CLIENT_IDso your code connects to the right session.
Test the key
Before wiring the key into a strategy, make one request by hand. It separates “the key is wrong” from “my code is wrong”.
from ib_async import IB
ib = IB()
ib.connect("127.0.0.1", 7497, clientId=7)
print(ib.managedAccounts()) # e.g. ['DU1234567'] for paper
ib.disconnect()DU means the connection works. Error 502 means the app is not running, the API is disabled or the port is wrong.Keep the credentials safe
- Store values in a password manager or a git-ignored
.env, never in code or notebooks you share. - In an AlgoGP project you can still add them under APIs & keys (encrypted at rest) and read them with
strategy_runtime.secrets.get("IBKR_HOST"). - Give each bot its own credential so you can revoke one without stopping the others.
IBKR_HOST=paste-your-value-here
IBKR_PORT=paste-your-value-here
# IBKR_CLIENT_ID=Running it safely
- Keep the API on localhost, or add only your server’s IP under Trusted IPs.
- Use the paper account until the bot has run for weeks without surprises.
- Schedule the daily restart and enable auto-restart so a session does not silently drop.
Frequently asked questions
Where is my Interactive Brokers API key?
There is none. The API authenticates through your logged-in TWS or IB Gateway session.
Can I run IB Gateway headless on a server?
It needs a display to log in, but tools such as IBC automate login and restarts on Linux servers.
Design and backtest your Interactive Brokers strategy first
AlgoGP turns an idea into Python and tests it on years of history before you connect it to Interactive Brokers. The exported code keeps the broker behind four small adapter methods.
- AI copilot writes and revises the rules
- Backtests with realistic costs
- Exported Python ready to wire to Interactive Brokers
Free plan, no card required.
How the Interactive Brokers API works
AlgoGP is not affiliated with Interactive Brokers. Menus get renamed from time to time; if a label differs, the official documentation has the current path.