# Enable the API in TWS and IB Gateway

> Turn on Enable ActiveX and Socket Clients in TWS or IB Gateway, set the socket port, allow 127.0.0.1 and switch off read-only API so Scalpel can trade.

Source: https://scalpeltrade.com/guides/enable-tws-api
Guide 1.5, Getting started. Beginner, 5 min read.
Checked with Scalpel 1.12.8 on 2026-10-15. By Scalpel Team.

**In short:**

In TWS, open the global configuration, go to **API > Settings**, tick **Enable ActiveX and Socket Clients**, untick **Read-Only API**, keep **Allow connections from localhost only** ticked and note the **Socket port**. In IB Gateway the same page is under **Configure > Settings > API > Settings**, with the socket always on. Then set the same port in Scalpel's **Settings > Connection > Port** (7497 by default for TWS, 4001 for IB Gateway) and click **Apply**.

TWS and IB Gateway ship with the API closed or read-only, so a fresh install will not talk to Scalpel until you change three or four settings. You do this once; both programs remember it.

**Before you start:**

- TWS or IB Gateway installed and logged in ([guide 1.4](https://scalpeltrade.com/guides/connect-interactive-brokers) helps you choose).
- Scalpel installed and activated ([guide 1.3](https://scalpeltrade.com/guides/activate-licence)).

## Turn the API on in TWS

### 1. Open the API settings

In TWS, open **File > Global Configuration** (in the Classic layout, **Edit > Global Configuration**). In the tree on the left, expand **API** and click **Settings**. Scalpel's connection banner calls this page **Configure > API > Settings**.

*Screenshot: Simplified. TWS, API settings: Enable ActiveX and Socket Clients (1), Read-Only API off (2), the socket port (3), localhost only (4) and Trusted IPs (5).*

### 2. Tick Enable ActiveX and Socket Clients

This opens the socket Scalpel connects to (1). Without it, nothing answers on the port and Scalpel's banner says neither TWS nor IB Gateway is running.

### 3. Untick Read-Only API

With **Read-Only API** ticked (2), Scalpel can read data and your account but every LIVE order is refused. SIM works either way, because SIM orders never reach IBKR. Untick it before you trade LIVE; leave it ticked while you only practise if you prefer the extra lock.

### 4. Check the socket port

**Socket port** (3) is the number Scalpel must use. TWS suggests 7496 after a live login and 7497 after a paper one. Either set it to 7497 (Scalpel's default) or leave it and set Scalpel to match in the last step.

### 5. Keep localhost only

Leave **Allow connections from localhost only** ticked (4). Scalpel runs on the same computer and connects from 127.0.0.1, which this always allows, and nothing else on your network can reach your account. If you ever untick it, add **127.0.0.1** to **Trusted IPs** (5) with **Create**, or TWS asks you to accept every connection.

Leave **Master API client ID** empty.

### 6. Click Apply, then OK

TWS applies the settings at once; no restart needed.

## Turn the API on in IB Gateway

IB Gateway always has its socket open, so there is no Enable ActiveX checkbox. The rest is the same:

1. In IB Gateway's window, open **Configure > Settings**, then **API > Settings** in the tree.
2. Untick **Read-Only API** (it starts ticked) if you will trade LIVE.
3. Note the **Socket port**: **4001** after a live login, 4002 after a paper one.
4. Keep **Allow connections from localhost only** ticked.
5. Click **Apply**, then **OK**.

*Screenshot: Simplified. IB Gateway, API settings: the same page without the Enable ActiveX checkbox. Live logins listen on 4001.*

## Set the same port in Scalpel

Open **Settings** (`Ctrl`+`,`) > **Connection**. Set **Port** to the socket port you noted: 7497 or 7496 for TWS, 4001 or 4002 for IB Gateway. Keep **Host** at **127.0.0.1**. Click **Apply**; Scalpel reconnects at once, and the **IBKR** pill in the status bar turns green.

*Screenshot: Settings, Connection, set for a live TWS login on 7496. Host stays 127.0.0.1.*

**Tip.**

You do not have to get the port right first time. While the link is down, Scalpel looks for TWS on 7497 and 7496 and IB Gateway on 4001 and 4002, and the banner under the header offers **Use port 7496** or **Use IB Gateway** when it finds one ([guide 1.4](https://scalpeltrade.com/guides/connect-interactive-brokers)).

## Settings worth changing while you are there

These are optional, but traders who skip them meet the problems later:

| Setting (TWS or IB Gateway) | Change it to | Why |
|---|---|---|
| **Lock and Exit**: auto restart | On, at a time you never trade | Both programs log out daily otherwise, and Scalpel loses its connection until you log back in |
| **API > Precautions** | Review the order precautions | TWS can hold back API orders with its own size and price warnings. Scalpel has its own caps ([guide 7.2](https://scalpeltrade.com/guides/fat-finger-and-position-caps)) |
| Other API tools | Do not reuse Scalpel's client IDs | Scalpel opens several API connections with client IDs 1, 2, 10 and 70 to 72 (100 and up with data recording on). Another tool on the same ID knocks one of them off |

## What can go wrong

- **"Not accepting the connection" in Scalpel's banner.** The port is open but the API handshake fails: TWS is showing a window asking whether to accept the connection, or **Allow connections from localhost only** is off and 127.0.0.1 is not in **Trusted IPs**. See [guide 13.2](https://scalpeltrade.com/guides/fix-not-accepting-connection).
- **"Neither TWS nor IB Gateway is running" although TWS is open.** **Enable ActiveX and Socket Clients** is off, so TWS is not listening at all, or its socket port is not one of the standard four. See [guide 13.1](https://scalpeltrade.com/guides/fix-disconnected).
- **LIVE orders are rejected, SIM works.** **Read-Only API** is still ticked. See [guide 13.5](https://scalpeltrade.com/guides/fix-order-rejected).
- **It worked yesterday.** TWS logged out overnight. Log in again, and set auto restart under **Lock and Exit** ([guide 13.1](https://scalpeltrade.com/guides/fix-disconnected)).

## Questions

**Where is Enable ActiveX and Socket Clients in TWS?**

In the global configuration (File, Global Configuration, or Edit, Global Configuration in the Classic layout), under API, Settings. It is the first checkbox. IB Gateway has no such box: its socket is always on.

**What is the difference between TWS ports 7496 and 7497?**

TWS suggests 7496 after a live login and 7497 after a paper login. Either works with Scalpel, as long as Settings, Connection, Port in Scalpel is the same number.

**What port does IB Gateway use?**

4001 after a live login and 4002 after a paper login, unless you change it in Configure, Settings, API, Settings.

**Do I need to add 127.0.0.1 to Trusted IPs?**

Not while Allow connections from localhost only is ticked: local connections are allowed already. Add 127.0.0.1 only if you untick that box.

**Is it safe to turn off Read-Only API?**

It lets programs on this computer place orders, which Scalpel needs for LIVE trading. Keep Allow connections from localhost only ticked so nothing on your network can. While you only use SIM, you can leave read-only on.
