Skip to main content

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

  1. Go to this page: https://openapi.ctrader.com/apps

  2. Log-in with your cTrader account (if not already logged-in).

  3. Click = Add new App button.

    1. Fill-in the mandatory fields: Application name and Description, etc.
    2. Click + Add redirect URL and put https://example.com.
    3. Click Save and now your entry is created.
  4. Click Credentials on created entry and there you have the Client ID and Secret.

  5. Wait untill Status of created entry changes from Submitted to Active.
    (might take up to 3 bussinus days)

  6. So far we have the following pieces:

    REDIRECT_URI=...
    CLIENT_ID=...
    CLIENT_SECRET=...

Part 2: Getting Authorization Code, AccessToken and RefreshToken

  1. Must open below URL in browser:
    https://id.ctrader.com/my/settings/openapi/grantingaccess/
    with below query parameters:

    Query ParamWhere 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
  2. In opened page click Allow access

  3. After clicking Allow access, you will be redirected to a url that has a query parameter named code, save its value:

    CODE=...
  4. 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 ParamIts Value
    REDIRECT_URI =same as before
    CLIENT_ID=same as before
    CLIENT_SECRET=same as before
    CODE=from previous step (Part 2, step 3)
  5. Now you have the AccessToken and RefreshToken

Summary of the Whole Process

note

Second link must be opened in under 60 seconds from when first link was opened.

@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%"

How to Get AccountID

  1. Go to this page https://openapi.ctrader.com/apps.
  2. Log-in with your cTrader account (if not already logged-in).
  3. Click Sandbox on one of the entries.
    1. Under "Scope", select Account info.
    2. Click Get token.
    3. Click Allow access.
    4. Copy the Access token (this is called "sandbox access token").
  4. 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
}