Ibkr CP Api
Ibkr CP Api
• User authentication.
• Market data subscriptions.
• Chart subscription.
• All additional functionality supported on CP Web API as a proxy only
2. Implementation
This API uses window.postMessage that allows third party to get data from IB Server.
Requirements:
Both incoming and outgoing messages are JSON objects. The data JSON Object will
contain ACTION property that identifies which package should handle the message. We
use the following ACTION Values: MD for market data, and CHART for chart data. The
output package could also contain error codes in an ERRORS property (array type).
Requests
Third party makes a call to IB API, and that API invokes call to backend that generates
response that is sent back to caller.
4.1 Initialization
1. Load the IB page with GET parameters from the IB server to the iframe. The
parameters are the following:
https://api.ibkr.com/sso/cpapi/?lang=en&WB_ID=Asterix&CLIENT_VERSION=1.4
{ method: 'loaded'}
Response:
{ method: 'initialized'}
If the INIT call is made successfully, the frame URL and whitebranded ID are validated
and the login page within the iframe is shown.
If INIT is not successful, one of the following messages is sent back to parent:
{ method: 'message', message: 'Initialization failed', ERRORS: <Array> }
WTE099
WTE018
WTE019
WTE023
4.2 Authentication
Authentication is done within the iframe. If login succeeds, the following messages are
sent back to the parent. 2 Signature messages are sent along with accounts message.
{"BACKEND_SESSION_ID":"581c0b12.00000291","ERRORS":[],"ACCOUNTS":"D
U91474","MESSAGE_TYPE":"FORWARD","ID":"d53d96e0-0095-4ef0-bc33-
2b7117d6ca03","USER":"qapap908","CURRENT_TIME":1478294565714}
{"SIGNATURE_TYPE":"USER_SESSION","MESSAGE_TYPE":"FORWARD","ID":"
":"d53d96e0-0095-4ef0-bc33-
2b7117d6ca03","USER":"qapap908","RESULT":true,"SIGNATURE":"OBi7YFp3lBY1
d+jMvGg7NwJtsJpNVYqyD67WbQR+wAfRzHvwLVGTEd3EeIaITw3tOtTMzzKuQR
4/BJKp1qcLryGPHo5r3sJcgnNEXOqal9g8P/plNpjkgLVaVd0uDgp0aiis+EXW2Z38LK
hOEP/3vdGvR4Lvin9jjtmEts724aUnMfVg9ZiNK/dbWd1ADgXgid/f1fkRekDDbDmGp
hYaQ3jEv0bSkCfg6d8rc/jW+gDAiBGMbPnu9Jxvx1vjbT4vkGPL9PkcpgEJHgFKDXb
w6IlNDJ6sHvX41WXZuv/6o49DpgXyITQQhgPwIf7HbKs5uAA4IJSoNEfHVB+sHyv/
hnONZIE1vNPjwewtRC84Sob1y7pblYY3eUusfzy756y63qTVBfOoLynUEj12pihs6zvg
hZd26kMGE1TexO+hs4EkepY1Fnq5jzR0IA5cp0dBXpehGiXbdXp1FwEFFDjWlNUlD
pLSgIwcy4SKy6xstPviiasZktlUKbQP1ruTXw4ANcUXLcdggbhABhXWjqsFz8WN9M6
5hHTCx2CpM3gZn2uLRXz4gg2aRUL4ilXlgMRKi2ILDgWSxVn7R80Q1lhRalt1jmmU
BjNY7pQJvGsSOQ3+66i4hiedctWcVugDHsF3IsxVgrp04PmIUjd4XNYKfuhrFsJeQVj
CYHAfxduC+bo="}
{"SIGNATURE_TYPE":"USER_ACCOUNTS","MESSAGE_TYPE":"FORWARD","I
D":"":"d53d96e0-0095-4ef0-bc33-
2b7117d6ca03","USER":"qapap908","RESULT":true,"SIGNATURE":"REJSCdTG23xm
iPOgACV8uyoeq3vIiTnGo/3EygQ/C3R1FAiXn3e59tAEm6FER15LikWjNw9IXLcsEdi
RsKbc+MHZ6iFsU7UrPY/ZORdWL0KkXEqu1swo2ZE4KaT3O0Dujnh+GFGSQcNdY
P2I7lSWXS5442Bub06pShXuGOmC1Dd9FdmwKWCrR1kTwm9RsylhLB32OHaNtdax
MYgULPbDH8WVUjAYheRZ389F9yHoSqSRlzSQUdZIch66/747vABMuFzaAYqxxb
M0LL0k1fpXpifuFAAqOTMjwtZB6HmSbpgwH45NRD5b7WRrwyFcm8HLpa2P2isc6l
Ck/AxY42Ay8IQUvluFzSmE3N+4vCbiWf9IiB2jcovZoZb0J/twm2E98J9ryP76IvgdULd
MBDciy7J728dSoHr0SOPmBkjGWHrF8sLrKpVFm6YJ3t6UrOhMboOiPIIUe/m/e3WA
Un48NOYaOpwiKjjkmBfi5H05Z+UtMMLZErt7w4vRdgbfEb+IgsLX75HBSUwcOhyH
P0/Tm/DljFRKNYhTJ2yPE+oKqiC2M8iP8XNChurxFQKqIgT2CaIqCRSAd48C6hBEZ
qacX4CUGbeKDaptnRwkaIopzlqIQ+NWVLLlPulV3aIOS81W8+oq73dNa6/aXnR8fEoI
Kk9acvM7Sjy1NbSPumnYw+Q="}
A ping message needs to be sent to the iframe every 1 minute (defined as timeout below).
The ping should start after a successful login and stops when the logout() method is
called.
If ping stops and the max_timeout defined in Section 4.1 is reached, the server will clean
out the session and no market data or chart data will be sent from the server.
After that the parent needs to send a ping message to iframe, at the timeout interval
defined above.
{ method: 'ping' }
4.4 Competition
If the user is logged into any other IB trading application like TWS or CP or with the
same username and tries to login into CP API, there will be a competition message. This
competition callback function is called with a message 'competition'. The callback
function should display a popup message (with the native confirm or a custom one like
jQuery Dialog) asking the user to continue the session or stop it.
Competition message:
{ method: 'competition', message: <string> }
If the user wants to continue the current session, the app should call:
{ method: 'keepSession' }
To kill the current session, call the method:
{ method: 'logout' }
To subscribe to market data the application should call the 'mdSubscribe' method with a
conid-exchange-pairs parameter. This parameter can contain one or more conid-exchange
pairs separated by a semicolon character. Ex: 8314:SMART;43645865:SMART
Input below:
After a successful subscribe the data callback function will be called. For market data,
MD packages contain static data describing the contract properties, prices, and other
market data attributes The data callback function will call the correct package based on
the ACTION property in the message. See parseMarketDataPackage() function in
client.js for detailed info.
DATA (MD):
{"ACTION":"MD","MD":[{"last":"132.30","symbol":"IBM","high":"132.39","low":"12
7.97","market_value":"1.26M","average_price":"125.00","change_price":"-
2.73","change_percent":-
2.02,"bid_price":"132.31","ask_size":"5","ask_price":"132.34","volume":"8.14M","bid_s
ize":"4","sec_type":"STK","expiry_type":"0","company_name":"INTL BUSINESS
MACHINES
CORP","last_size":"1","bbo_exchange":"a6","contract_description_1":"IBM","listing_ex
change":"NYSE","close_price":"8/10","dividend_yield":"5.0%","open_price":"130.50","
unformatted_volume":8140000.5,"conid":8314,"conidEx":"8314@SMART","exchange":
"SMART"}],"MESSAGE_TYPE":"FORWARD"}
DATA (MD):
{"ACTION":"MD","MD":[{"bid_price":"132.27","ask_price":"132.30","conid":8314,"c
onidEx":"8314@SMART","exchange":"SMART"}],"MESSAGE_TYPE":"FORWARD"
}
DATA (MD):
{"ACTION":"MD","MD":[{"volume":"8.15M","unformatted_volume":8149999.5,"coni
d":8314,"conidEx":"8314@SMART","exchange":"SMART"}],"MESSAGE_TYPE":"F
ORWARD"}
If you subscribe to an incorrect conid-exchange pair, you get an error like below:
WTE008
WTE020
WTE028
Data fields returned after subscription in MD Action is:MD Object key value pair of:
con_id
exchange
change_percent
change_price
bid_size
bid_price
ask_size
ask_price
volume (in K, M, B)
high
low
market_value
average_price
symbol
company_name
last_price
yield_last
mark_price
open
close
open_price
close_price
dividend_yield
To unsubscribe market data the app should call 'mdUnsubscribe' method with a conid-
exchange-pairs parameter. This parameter can contain one or more conid-exchange pairs
separated by a semicolon character. Ex: 8314:SMART;43645865:SMART
Input below:
After un-subscribe, market data stops ticking for the un-subscribed conid, exchange pair.
Only unsubscribe for conid-exchange pair, to which you have subscribed earlier.
If you unsubscribe to incorrect conid-exchange pair, you get an error like below:
WTE008
WTE020
To subscribe to chart data the application should call 'chartSubscribe' method with three
parameters. The first parameter contains one (and only one) conid-exchange-pair. The
second parameter is the length of the chart. This value can be selected from a predefined
list (1d, 1w, 2w, 1m, 6m, 1y, 5y). The last one is a boolean for “Outside Regular Trading
Hours”.
Input below:
After a successful Chart subscribe the data callback function called the chart packages is
invoked. The CHART type packages contain the data in following format.
WTE008
WTE021
WTE022
WTE028
To unsubscribe chart data the app should call 'chartUnsubscribe' method with a one (and
only one) conid-exchange-pair. Once unsubscribe is called, the chart details stop being
sent to parent every 1 minute.
Input:
If chart unsubscribe fails, you will a get message like the below:
WTE008
WTE021
WTE028
If the client application is using version 1.4, then they can request all calls available here:
https://www.interactivebrokers.com/api/doc.html using the cpapi application too.
Request message:
Response message:
Request message:
{ method: "cpwebapiRequest", httpMethod: "GET", url: "/portfolio/accounts }
Response message:
For additional help with the Client Portal Web API documentation and end points
available, please reach out to [email protected]
{ method: 'data',
message:{"MESSAGE_TYPE":"FORWARD","ACTION":"MD","MD":[{"symbol":"IB
KR","conidex":"43645865","company_name":"INTERACTIVE BROKERS GRO-CL
A","expiry_type":"0","sec_type":"STK","listing_exchange":"NASDAQ.NMS","contract
_description_1":"IBKR","exchange":"SMART","conid":43645865}]}}
{ method: 'data',
message:{"MESSAGE_TYPE":"FORWARD","ACTION":"MD","MD":[{"change_price
":"-
1.90","bid_size":"3","last_price":"145.47","is_delayed":false,"open_price":"147.37","cha
nge_percent":"-
1.29%","market_value":"1,018","conid":8314,"mark_price":"145.47","bid_price":"145.4
7","ask_price":"145.48","last_trading_day":"20150911","volume":"1.61M","close_price"
:"147.37","dividend_yield":"3.6%","high":"147.37","low":"145.41","exchange":"SMAR
T","ask_size":"8"}]}}
Additionally, the Chart message is sent to parent frame every 1 minute by IB Server.
The following messages are sent from the server if there are any application problems.
The client should either try to reconnect or show login window again.
WT Error What to do
DATA 1:
{"BACKEND_SESSION_ID":"581c0b12.00000280","ERRORS":[],"ACCOUNTS":"D
U91474","MESSAGE_TYPE":"FORWARD","ID":"":"d53d96e0-0095-4ef0-bc33-
2b7117d6ca03","USER":"qapap908","CURRENT_TIME":1478290612110,"RESULT":t
rue}
DATA 2:
{"SIGNATURE_TYPE":"USER_SESSION","MESSAGE_TYPE":"FORWARD","ID":"
":"d53d96e0-0095-4ef0-bc33-
2b7117d6ca03","USER":"qapap908","RESULT":true,"SIGNATURE":"OBi7YFp3lBY1
d+jMvGg7NwJtsJpNVYqyD67WbQR+wAfRzHvwLVGTEd3EeIaITw3tOtTMzzKuQR
4/BJKp1qcLryGPHo5r3sJcgnNEXOqal9g8P/plNpjkgLVaVd0uDgp0aiis+EXW2Z38LK
hOEP/3vdGvR4Lvin9jjtmEts724aUnMfVg9ZiNK/dbWd1ADgXgid/f1fkRekDDbDmGp
hYaQ3jEv0bSkCfg6d8rc/jW+gDAiBGMbPnu9Jxvx1vjbT4vkGPL9PkcpgEJHgFKDXb
w6IlNDJ6sHvX41WXZuv/6o49DpgXyITQQhgPwIf7HbKs5uAA4IJSoNEfHVB+sHyv/
hnONZIE1vNPjwewtRC84Sob1y7pblYY3eUusfzy756y63qTVBfOoLynUEj12pihs6zvg
hZd26kMGE1TexO+hs4EkepY1Fnq5jzR0IA5cp0dBXpehGiXbdXp1FwEFFDjWlNUlD
pLSgIwcy4SKy6xstPviiasZktlUKbQP1ruTXw4ANcUXLcdggbhABhXWjqsFz8WN9M6
5hHTCx2CpM3gZn2uLRXz4gg2aRUL4ilXlgMRKi2ILDgWSxVn7R80Q1lhRalt1jmmU
BjNY7pQJvGsSOQ3+66i4hiedctWcVugDHsF3IsxVgrp04PmIUjd4XNYKfuhrFsJeQVj
CYHAfxduC+bo="}
DATA 3:
{"SIGNATURE_TYPE":"USER_ACCOUNTS","MESSAGE_TYPE":"FORWARD","I
D":"":"d53d96e0-0095-4ef0-bc33-
2b7117d6ca03","USER":"qapap908","RESULT":true,"SIGNATURE":"REJSCdTG23xm
iPOgACV8uyoeq3vIiTnGo/3EygQ/C3R1FAiXn3e59tAEm6FER15LikWjNw9IXLcsEdi
RsKbc+MHZ6iFsU7UrPY/ZORdWL0KkXEqu1swo2ZE4KaT3O0Dujnh+GFGSQcNdY
P2I7lSWXS5442Bub06pShXuGOmC1Dd9FdmwKWCrR1kTwm9RsylhLB32OHaNtdax
MYgULPbDH8WVUjAYheRZ389F9yHoSqSRlzSQUdZIch66/747vABMuFzaAYqxxb
M0LL0k1fpXpifuFAAqOTMjwtZB6HmSbpgwH45NRD5b7WRrwyFcm8HLpa2P2isc6l
Ck/AxY42Ay8IQUvluFzSmE3N+4vCbiWf9IiB2jcovZoZb0J/twm2E98J9ryP76IvgdULd
MBDciy7J728dSoHr0SOPmBkjGWHrF8sLrKpVFm6YJ3t6UrOhMboOiPIIUe/m/e3WA
Un48NOYaOpwiKjjkmBfi5H05Z+UtMMLZErt7w4vRdgbfEb+IgsLX75HBSUwcOhyH
When the calling application gets the data, they will take the Signature and verify it
against the unsigned data using the downloaded certificate.
The public certificate to be used for verifying the data can be downloaded from:
https://api.ibkr.com/sso/cpapi/downloadCertificate
1. Download and save the public certificate from IB (at a certain file location on your
end). This doesn't need to be done every day. It can be once, and then every time the
key changes (IB will notify application team).
2. Ensure that the current_time is within a few minutes of the application's server time.
This ensures that no one is playing back old messages.
3. Run Verify (sample java code attached). If Verify is true and 2 is true, then you can
proceed knowing that the user has logged into the IB system in the last few minutes.
4. Send the backend_session_id on orders for additional security.
7. ERROR Codes
All error codes with descriptions. The last column indicates if the message can be seen in
the local login page, is sent to parent, or both.
Also check with IB for the correct iframe domain to be used for PostMessage validations
on the parent side. This could be different for test and production environments.
The custom style sheet can overwrite the colors, background colors, font family, font
size, etc. Recommended selectors to use are body, input.text, button.submit, h1, .title.
Institutions will have to test their modified style sheet on every major supported browser
before sending to IB. IB will not be held responsible for the broken layout of a login page
after incorporating the custom style sheet.
Style sheets to be incorporated for each white branded user (if any) will be confirmed
during setup.
https://cdcdyn.interactivebrokers.com/sso/cpapi/frame/
https://cdcdyn.interactivebrokers.com/sso/cpapi/frame/wtapi.js
https://cdcdyn.interactivebrokers.com/sso/cpapi/frame/client.js
Please copy these 2 files (wtapi.js and client.js) and customize as needed in your
application.