Skip to main content

Interactive Brokers OAuth

Project description

IBKR Authentication Workflow

Interactive Brokers provides an extensive API that can be used for trading and account management.

It's also possible to authenticate for the API via OAuth.

ibauth is a Python client for handling the full Interactive Brokers (IBKR) Web API authentication flow.
It wraps the OAuth2 + session lifecycle steps (access_token, bearer_token, ssodh_init, tickle, etc.) into a simple, reusable interface.

🔑 Features:

  • Obtain and refresh IBKR OAuth2 tokens.
  • Manage brokerage sessions (ssodh_init, validate_sso, tickle, logout).
  • YAML-based configuration (easy to keep credentials outside of code).
  • Logging of requests and responses for troubleshooting.

Documentation for the IBKR Web API can be found in the official reference.


Requirements

  • Python 3.11+
  • A valid IBKR account with Web API access enabled.
  • An RSA private key (.pem) registered with IBKR.

Dependencies are listed in requirements.txt.


Installation

You can install either from PyPI (preferred) or GitHub (which may give access to updates not yet published on PyPI).

# Install from PyPI.
pip install ibauth

# Install from GitHub.
pip install git+https://github.com/datawookie/ibkr-oauth-flow

Configuration

Authentication parameters are supplied via a YAML configuration file:

client_id: "your-client-id"
client_key_id: "your-client-key-id"
credential: "your-credential"
private_key_file: "/path/to/privatekey.pem"
domain: "api.ibkr.com"
  • client_id: Application client ID from IBKR.
  • client_key_id: Key identifier associated with your private key.
  • credential: IBKR credential string.
  • private_key_file: Path to your RSA private key (.pem).
  • domain: Usually api.ibkr.com, but IBKR supports numbered subdomains (1.api.ibkr.com, 5.api.ibkr.com, …).

How It Works

The IBKR Web API requires multiple steps to establish and maintain a brokerage session.
ibauth automates these steps:

  1. Access Token
    Exchange your client credentials + JWS for an access token.
    auth.get_access_token()

  2. Bearer Token
    Use the access token and your public IP to obtain a bearer token.
    auth.get_bearer_token()

  3. Session Initialisation
    Start a brokerage session using the bearer token.
    auth.ssodh_init()

  4. Session Validation (optional)
    Confirm that your session is active.
    auth.validate_sso()

  5. Keepalive ("Tickle")
    Periodically ping the API to keep the session alive.
    auth.tickle()

  6. Logout
    End the session when finished.
    auth.logout()

    +--------------+        +--------------+        +---------------+
    |  Access      |        |  Bearer      |        |  Brokerage    |
    |  Token       | -----> |  Token       | -----> |  Session      |
    +--------------+        +--------------+        +---------------+
           |                        |                        |
           v                        v                        v
    get_access_token()     get_bearer_token()       ssodh_init() / tickle()

Quick Start

import logging
import time
import ibauth

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)7s] %(message)s",
)

logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("charset_normalizer").setLevel(logging.WARNING)

if __name__ == "__main__":
    auth = ibauth.auth_from_yaml("config.yaml")

    auth.get_access_token()
    auth.get_bearer_token()

    auth.ssodh_init()
    auth.validate_sso()

    # Keep session alive
    for _ in range(3):
        auth.tickle()
        time.sleep(10)

    # Dynamically change the API domain
    auth.domain = "5.api.ibkr.com"
    auth.tickle()

    auth.logout()

Testing

This project uses pytest. To run the test suite:

pytest

To include coverage:

pytest --cov=src/ibauth --cov-report=term-missing

Development

Clone the repo and install dependencies into a virtual environment:

git clone https://github.com/datawookie/ibkr-oauth-flow.git cd ibkr-oauth-flow python -m venv .venv source .venv/bin/activate pip install -r requirements.txt

Deployment to PyPI

Publishing requires a PyPI token (UV_PUBLISH_TOKEN) to be available in your environment.

make deploy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ibauth-0.0.7.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ibauth-0.0.7-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file ibauth-0.0.7.tar.gz.

File metadata

  • Download URL: ibauth-0.0.7.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.2

File hashes

Hashes for ibauth-0.0.7.tar.gz
Algorithm Hash digest
SHA256 3ad3b55fa96404544a5307b31870d4e6be632ddd7236762e90748b753d26526d
MD5 ce16a3310c6131ae56475c52f7651179
BLAKE2b-256 f9acc37b45063f1d9507478685012ce98a2db3aedbd0ef222cf2795a59b1c29b

See more details on using hashes here.

File details

Details for the file ibauth-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: ibauth-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.2

File hashes

Hashes for ibauth-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 6252dcbf5eb4a8c657b2f78577b79723d62700aad2f3f567a95589353f0c9a2f
MD5 5295bb5a92c7899e0fd6482ac93af4ba
BLAKE2b-256 79e391cddfd9fa6254bdf5d7f4e0308bfb34f8964640a596fb0592a0fa624386

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page