Getting Started with IbkrApi
View SourceThis tutorial will guide you through the process of setting up and using the IbkrApi library to interact with Interactive Brokers' Client Portal API.
Prerequisites
Before you begin, make sure you have:
- An Interactive Brokers account
- The Client Portal Gateway running locally or on a server
- Elixir 1.15 or later
Installation
Add ibkr_api
to your list of dependencies in mix.exs
:
def deps do
[
{:ibkr_api, "~> 0.1.0"}
]
end
Then run:
mix deps.get
Configuration
Configure the library in your application's configuration file (config/config.exs
):
config :ibkr_api,
base_url: "http://localhost:5000/v1/api", # Default Client Portal Gateway URL
timeout: 30_000 # Request timeout in milliseconds
You can override these settings in your environment-specific configuration files.
Starting the Client Portal Gateway
Before using the library, you need to start the Interactive Brokers Client Portal Gateway:
- Download the Client Portal Gateway from the Interactive Brokers website
- Extract the downloaded file
- Start the gateway:
cd path/to/gateway
./bin/run.sh
The gateway will start on localhost:5000
by default.
Basic Usage
Establishing a Connection
First, you need to authenticate with the IBKR Client Portal API:
# Check if the server is running and authenticate
{:ok, response} = IbkrApi.ClientPortal.Auth.ping_server()
# If you need to reauthenticate
{:ok, _} = IbkrApi.ClientPortal.Auth.reauthenticate()
Listing Accounts
Once authenticated, you can list your accounts:
{:ok, accounts} = IbkrApi.ClientPortal.Account.list_accounts()
# Print account IDs
accounts |> Enum.each(fn account -> IO.puts(account.account_id) end)
Getting Account Information
You can retrieve detailed information about a specific account:
account_id = "U1234567" # Replace with your actual account ID
{:ok, summary} = IbkrApi.ClientPortal.Account.account_summary(account_id)
# Print available funds
IO.puts("Available funds: #{summary.available_funds}")
Next Steps
Now that you've set up the library and made your first API calls, you can:
- Learn more about Authentication processes
- Explore Account Management operations
- Start Trading with the API
For a complete reference of all available functions, check the API Reference.