IBKR knowledge base
Connect to IBKR with ib_insync
Minimal safe connection example for a local TWS or IB Gateway session.
BeginnerDraft
ib_insyncconnectionlocalhost
Purpose
Confirm that Python can connect to a local IBKR API endpoint.
Requirements
- ib_insync installed
- TWS or IB Gateway running
- API socket enabled
Notes
Confirm your TWS or IB Gateway API settings before connecting.
Common ports may differ depending on paper/live mode and local configuration.
Always verify the port inside TWS or IB Gateway.
Example
python
from ib_insync import IB
ib = IB()
try:
ib.connect("127.0.0.1", 7497, clientId=1)
print("Connected:", ib.isConnected())
finally:
ib.disconnect()Common errors
- Connection refused
- Wrong port
- Client ID conflict
Contributors
Code Contributor
