github.com/bishop-bot/ibgateway-go

A Go library for authenticating with the Interactive Brokers Client Portal Web API Gateway


Install
go get github.com/bishop-bot/ibgateway-go

Documentation

IB Gateway Go Library

A Go library for authenticating with the Interactive Brokers (IB) Client Portal Web API Gateway.

Overview

This library provides a pure Go implementation of the authentication flow used by the IB Gateway API. It handles:

  • SRP (Secure Remote Password) authentication
  • Two-factor authentication (SMS, IB Key, TOTP)
  • Cookie management for session persistence
  • RSA encryption for secure communication

Installation

go get github.com/bishop-bot/ibgateway-go

Usage

Simple Import (Recommended)

package main

import (
    "fmt"
    "log"
    
    "github.com/bishop-bot/ibgateway-go"
)

func main() {
    // Create authenticator with TOTP second factor
    authConfig := ibgateway.AuthConfig{
        Username:           "your_username",
        Password:           "your_password",
        BaseURL:            "https://localhost:5000",
        SecondFactorMethod: ibgateway.TOTP,
        TOTPSecret:         "YOUR_TOTP_SECRET", // e.g., from Google Authenticator
    }
    
    authenticator, err := ibgateway.NewAuthenticator(authConfig)
    if err != nil {
        log.Fatal(err)
    }
    defer authenticator.Close()
    
    // Authenticate
    if err := authenticator.Authenticate(); err != nil {
        log.Fatal(err)
    }
    
    // Finalize the authentication
    if err := authenticator.Finalize(); err != nil {
        log.Fatal(err)
    }
    
    fmt.Println("Authentication successful!")
    fmt.Printf("Is Paper Trading: %v\n", authenticator.IsPaper())
}

Alternative Import

The library can also be imported from the auth subpackage:

import "github.com/bishop-bot/ibgateway-go/auth"

authConfig := auth.AuthConfig{...}
authenticator, _ := auth.NewAuthenticator(authConfig)

Using IB Key Authentication

authConfig := ibgateway.AuthConfig{
    Username:           "your_username",
    Password:           "your_password",
    BaseURL:            "https://localhost:5000",
    SecondFactorMethod: ibgateway.IBKeyAndroid, // or ibgateway.IBKeyIOS
    OCRASecret:         "YOUR_OCRA_SECRET",
    OCRAPin:            "YOUR_PIN",
    OCRACounter:        2,
}

Using SMS Authentication

authConfig := ibgateway.AuthConfig{
    Username:           "your_username",
    Password:           "your_password",
    BaseURL:            "https://localhost:5000",
    SecondFactorMethod: ibgateway.SMS,
}

Making Authenticated Requests

After authentication, you can use the session to make API requests:

session := authenticator.GetSession()

// Make a request to get account info
resp, err := session.Get("/v1/api/portfolio/accounts")
if err != nil {
    log.Fatal(err)
}
defer resp.Body.Close()

// Read and parse the response
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))

Second Factor Methods

The library supports the following two-factor authentication methods:

Constant Description
ibgateway.SMS SMS-based verification code
ibgateway.TOTP Time-based one-time password (Google Authenticator, etc.)
ibgateway.IBKeyAndroid IB Key on Android device
ibgateway.IBKeyIOS IB Key on iOS device

API Reference

NewAuthenticator

Creates a new Authenticator instance.

func NewAuthenticator(config AuthConfig) (*Authenticator, error)

AuthConfig

Configuration for authentication:

Field Type Required Description
Username string Yes IB account username
Password string Yes IB account password
BaseURL string Yes Gateway URL (e.g., https://localhost:5000)
SecondFactorMethod string No Default: SMS
OCRASecret string For IB Key OCRA secret for IB Key auth
OCRAPin string For IB Key PIN for IB Key auth
OCRACounter int For IB Key Counter for IB Key auth
TOTPSecret string For TOTP TOTP secret (base32 encoded)

Authenticator Methods

Method Description
Authenticate() Perform the full authentication flow
Finalize() Complete authentication with dispatcher request
GetSession() Get the HTTP session for API requests
GetSessionToken() Get the session token
IsPaper() Check if authenticating to paper trading
Close() Close the authenticator and release resources

Error Handling

The library provides specific error types:

  • ibgateway.AuthenticationError - General authentication failures
  • ibgateway.TwoFactorError - Two-factor authentication errors
  • ibgateway.MaxLoginAttemptsError - Too many failed login attempts
if _, ok := err.(*ibgateway.MaxLoginAttemptsError); ok {
    // Handle max login attempts error
}

Session Management

The library manages cookies automatically. The session token can be retrieved and stored for later use:

// Get session token after authentication
token := authenticator.GetSessionToken()

// Store token for future use
// (In production, store securely)

// Reuse token in a new session
session, _ := ibgateway.NewSessionManager("https://localhost:5000")
session.SetSessionToken(storedToken)

Requirements

  • Go 1.21 or higher
  • IB Gateway running and accessible
  • IB account with API access enabled

IB Gateway Setup

  1. Download and install IB Gateway from Interactive Brokers
  2. Enable API access in the Gateway settings
  3. Configure the API port (default: 5000)
  4. Enable two-factor authentication in your IB account

Security Considerations

  • Never hardcode credentials in your code
  • Use environment variables or a secrets manager
  • The library uses TLS with certificate verification disabled by default (IB Gateway uses self-signed certs)
  • Store session tokens securely

References

License

MIT License