Acquiring Credentials
Once we are connected to the server, we can send a message to it, and the server
will respond to our message too, but the problem is because server does not know
who we are, it will respond to any message we send by sending an error message
back (payloadType
of 2142
). In order to introduce ourself properly to the
server, we need to have the proper credentials, so let's see how we can them.
How to Get ClientID, ClientSecret, AccessToken and RefreshToken
Part 1: Getting ClientID
and ClientSecret
-
Go to this page: https://openapi.ctrader.com/apps
-
Log-in with your cTrader account (if not already logged-in).
-
Click
= Add new App
button.- Fill-in the mandatory fields:
Application name
andDescription
, etc. - Click
+ Add redirect URL
and puthttps://example.com
. - Click
Save
and now your entry is created.
- Fill-in the mandatory fields:
-
Click
Credentials
on created entry and there you have theClient ID
andSecret
. -
Wait untill
Status
of created entry changes fromSubmitted
toActive
.
(might take up to 3 bussinus days) -
So far we have the following pieces:
REDIRECT_URI=...
CLIENT_ID=...
CLIENT_SECRET=...
Part 2: Getting Authorization Code
, AccessToken
and RefreshToken
-
Must open below URL in browser:
https://id.ctrader.com/my/settings/openapi/grantingaccess/
with below query parameters:Query Param Where Value Comes From redirect_uri=
from Part 1, step 3.2 client_id=
from Part 1, step 3.4 client_secret=
from Part 1, step 3.4 -
In opened page click
Allow access
-
After clicking
Allow access
, you will be redirected to a url that has a query parameter namedcode
, save its value:CODE=...
-
Must open below URL: (in under 60 seconds of Part 2, step 1) https://openapi.ctrader.com/apps/token?grant_type=authorization_code
with below query parameters:Query Param Its Value REDIRECT_URI = same as before CLIENT_ID= same as before CLIENT_SECRET= same as before CODE= from previous step (Part 2, step 3) -
Now you have the
AccessToken
andRefreshToken
Summary of the Whole Process
Second link must be opened in under 60 seconds from when first link was opened.
- Windows
- Linux
@echo off
set /p CLIENT_ID="ClientID: "
set /p CLIENT_SECRET= "ClientSecret: "
set /p REDIRECT_URI="Redicrect URI: "
set SCOPE=trading
start chrome "https://id.ctrader.com/my/settings/openapi/grantingaccess/?client_id=%CLIENT_ID%&redirect_uri=%REDIRECT_URI%&scope=%SCOPE%&product=web"
set /p CODE="Authorization Code: "
start chrome "https://openapi.ctrader.com/apps/token?grant_type=authorization_code&code=%CODE%&redirect_uri=%REDIRECT_URI%&client_id=%CLIENT_ID%&client_secret=%CLIENT_SECRET%"
read -p "ClientID: " CLIENT_ID
read -p "ClientSecret: " CLIENT_SECRET
read -p "Redicrect URI: " REDIRECT_URI
SCOPE = 'trading'
firefox "https://id.ctrader.com/my/settings/openapi/grantingaccess/?client_id=$CLIENT_ID&redirect_uri=$REDIRECT_URI&scope=$SCOPE&product=web"
read -p "Authorization Code: " CODE
URL = "https://openapi.ctrader.com/apps/token?grant_type=authorization_code&code=$CODE&redirect_uri=$REDIRECT_URI&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET"
curl -X GET $URL -H "Accept: application/json" -H "Content-Type: application/json"
How to Get AccountID
- Go to this page https://openapi.ctrader.com/apps.
- Log-in with your cTrader account (if not already logged-in).
- Click
Sandbox
on one of the entries.- Under "Scope", select
Account info
. - Click
Get token
. - Click
Allow access
. - Copy the
Access token
(this is called "sandbox access token").
- Under "Scope", select
- Open below URL with an
accessToken
query param set to the token acquired in previous step:
https://api.spotware.com/connect/tradingaccounts?access_token=
(Note: You can also use your "real access token" here too)
Now you will have a list of all of your trading accounts associated with your
cTrader accounts in JSON
format.
You will have below properties for each trading account:
// prettier-ignore
{
"accountId": 0,
"accountNumber": 0,
"live": false,
"brokerName": "",
"brokerTitle": "",
"depositCurrency": "",
"traderRegistrationTimestamp": 0,
"traderAccountType": "",
"leverage": 0,
"leverageInCents": 0,
"balance": 0,
"deleted": false,
"accountStatus": "",
"swapFree": false,
"moneyDigits": 0
}