EDDYMENS

Published a month ago

Setting Up The IBKR API On Windows WSL

Table of contents

When I am on the move, I work from a MacBook, but when I am at home, I use a Windows desktop with WSL [↗] installed.

Accessing IBKR's API on any operating system is pretty straightforward. However, when using a virtual machine or any setup that requires a network configuration, there are a few extra steps you need to take.

At first, I wanted to write this article just to point out those details. But then I figured I should cover the entire IBKR API setup on Windows instead.

I have already written a bit about accessing Windows localhost from WSL [↗], so feel free to check that out if needed.

This article will walk you through setting up and working with an IBKR account using the Python SDK.

Prerequisites

Before you begin, make sure you have:

  • Windows 10/11 with WSL2 installed
  • IBKR Account
  • TWS (Trader Workstation) or IB Gateway installed on Windows

Install Python and Required Libraries

Open your WSL terminal and install Python:

$ sudo apt update && sudo apt install python3 python3-pip -y

Verify the installation:

01: python3 --version 02: pip3 --version

Install the ib_insync package (recommended for working with IBKR API):

$ pip3 install ib_insync

You can set everything up inside a virtual environment (venv) [↗] but its not a requirement.

Enable API Access in TWS/IB Gateway

IBKR serves its API [↗] through TWS [↗] or IB Gateway [↗], which act as intermediaries. To get started, we need to enable API access.

Finding the required IP Addresses

We need two IP addresses:

  1. WSL IP address

  2. Windows host IP address

  3. Find your WSL IP (inside WSL, run)

    $ hostname -I

This will output your WSL IP, which you need to whitelist in IB Gateway or TWS.

  1. Whitelist Your WSL IP in IB Gateway/TWSIBRK Gateway screenshot [→]

  2. Open TWS or IB Gateway on Windows.

  3. Go to Edit → Global Configuration → API → Settings.

  4. Disable Allow connections from localhost only (can be found at the very bottom)

  5. Add your IP address to the list of Trusted IPs

  6. Also note down the socket port value, you will need it in the next step.

  7. Click Apply & OK.

By whitelisting this IP, requests from WSL will be handled instead of blocked.

  1. Get Your Windows Host IPThe next IP address you need is your Windows host IP, you will later be using this IP address in your code to reach IBKR gateway or TWS.
  • Find your Host (Window) IP (inside WSL, run):
$ echo $(ip route | awk '/default/ {print $3}') # eg output: 192.168.1.100

Testing the setup

Now, create a simple Python script to test the IBKR API:

test.py

01: from ib_insync import * 02: 03: ib = IB() 04: ib.connect('192.168.1.100', 4002, clientId=1) # Replace with your Windows Host IP and socket port 05: 06: print("Connected to IBKR API!") 07: ib.disconnect()

Replace 192.168.1.100 with your actual Windows host IP, and 4002 with the socket port you noted earlier.

Run the script:

$ python test.py

If you see “Connected to IBKR API!”, your setup is complete!

That’s it! Happy coding or trading—whichever suits you best! :)

Here is another article you might like 😊 How To Access A Windows Localhost App From WSL