Interactive Brokers Python API: ibapi vs ib_insync vs ib_async (2026)
Interactive Brokers lets you trade from Python, but there is more than one way to do it. The official ibapi package, the community libraries ib_insync and ib_async, and the newer Web API all connect to the same account in different ways. This guide explains what each one is, shows working code for each, and helps you pick the right one for your bot.
The Interactive Brokers APIs at a glance
- TWS API: the main API. Your code connects to Trader Workstation or IB Gateway running on your machine or VPS, usually on port 7497 (TWS paper), 7496 (TWS live), 4002 (Gateway paper) or 4001 (Gateway live). It has the most complete order types and streaming data.
- Client Portal Web API: a REST API over HTTPS. No TWS needed, but sessions expire and need re-authentication, and some features are more limited. It suits dashboards and light trading.
- FIX: for institutions and high volume. Not covered here.
The three Python options below all use the TWS API.
Option 1: ibapi, the official Python client
ibapi comes with the TWS API download from Interactive Brokers. It is built from two classes: EClient sends requests and EWrapper receives every response through callback methods. You run its message loop in a thread.
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import threading, time
class App(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId):
self.req_price()
def req_price(self):
c = Contract()
c.symbol, c.secType, c.exchange, c.currency = "AAPL", "STK", "SMART", "USD"
self.reqMktData(1, c, "", False, False, [])
def tickPrice(self, reqId, tickType, price, attrib):
print("tick", tickType, price)
app = App()
app.connect("127.0.0.1", 7497, clientId=1)
threading.Thread(target=app.run, daemon=True).start()
time.sleep(10)
app.disconnect()
Good for: full control, tick-level data, and staying on code IBKR officially supports. Downside: a lot of boilerplate, and you track request IDs and state yourself.
Option 2: ib_insync
ib_insync wraps the TWS API in a much simpler interface. It keeps your positions, orders and market data in sync automatically and lets you write code that reads top to bottom.
from ib_insync import IB, Stock, MarketOrder
ib = IB()
ib.connect("127.0.0.1", 7497, clientId=1)
aapl = Stock("AAPL", "SMART", "USD")
ib.qualifyContracts(aapl)
bars = ib.reqHistoricalData(aapl, "", "5 D", "1 hour", "TRADES", True)
print(bars[-1])
trade = ib.placeOrder(aapl, MarketOrder("BUY", 1))
ib.sleep(2)
print(trade.orderStatus.status)
ib.disconnect()
Its original author passed away in 2024 and the project is no longer updated. It still works for many bots, but new projects should look at its successor.
Option 3: ib_async
ib_async is the community-maintained continuation of ib_insync. The API is almost identical, so most code only needs the import changed:
from ib_async import IB, Stock, LimitOrder
ib = IB()
ib.connect("127.0.0.1", 4002, clientId=7)
spy = Stock("SPY", "SMART", "USD")
ib.qualifyContracts(spy)
ib.placeOrder(spy, LimitOrder("BUY", 10, 500.00))
Good for: most new trading bots. It is maintained, readable, and handles the event loop for you.
Which one should you use?
- New bot, normal strategy:
ib_async. - Existing bot on ib_insync: keep it running, and plan a move to
ib_asyncwhen you next change the code. - Tick data, very custom order handling, or a company policy to use official code:
ibapi. - No server to run TWS or Gateway on: the Client Portal Web API.
Things every IBKR Python bot must handle
- Enable the API. In TWS or Gateway, turn on ActiveX and Socket Clients and turn off Read-Only API if the bot places orders.
- Unique client IDs. Two scripts using the same client ID will knock each other off.
- Daily restarts. TWS and Gateway restart once a day and drop your connection. The bot must reconnect on its own.
- Market data subscriptions. Without them you get delayed data or errors such as 354.
- Pacing limits. Too many historical data requests in a short time trigger pacing violations. Cache data and space out requests.
- Paper first. Test on the paper account, then switch the port to live with the same code.
Frequently asked questions
Is the Interactive Brokers API free?
Yes. API access is included with an IBKR account. You pay normal commissions and any market data subscriptions you need.
Can I use the IBKR API without TWS running?
With the TWS API, no: you need TWS or IB Gateway running. IB Gateway is lighter and better for a VPS. The Client Portal Web API works without it.
Which Python version do I need?
Use a current Python 3 release. ib_async needs a recent Python 3 version, so check its documentation before you set up the server.
Does the API work for options and futures?
Yes. Stocks, options, futures, forex and more are supported, as long as your account has the trading permissions and data subscriptions.
Need an IBKR bot built or fixed?
We build and repair Interactive Brokers bots in Python with ib_async and ibapi, deployed on a VPS with auto restart. See IBKR API development and IBKR trading bot development. Related: how to build an IBKR trading bot in Python and IB Gateway auto restart.