Getting started
SDKs are the preferred way to perform operations in the TradrAPI platform. Currently, there is only one SDK, made for JavaScript that can be used both in the back-end (NodeJS) or in the front-end (ES6 Module).
Before you start
Before using the SDK you will need a way to authenticate with the API. There are a few ways to do so:
-
API Key
: A key used to identify you as a brand, as a single user, or as a single account (based on the key type). API keys should never be used in a non-backend environment. It is your responsibility to keep your key safe. -
Access Token
: A token used to identify you as a user. This token is generated by the domain on which your trading account was registered. Access tokens are safe to use in a front-end environment.
Authentication
There are multiple ways to authenticate using this SDK. As mentioned above you can use an API Key
or an Access Token
to authenticate and perform actions on TradrAPI. It is important to re-iterate that API Keys
should never be used in
a front-end environment.
The different authentications grant access to different scopes of your data: user
or admin
. The user
scope is
restricted to the data of the user that is authenticated. The admin
scope is restricted to the data of the project
(brand) that is authenticated.
To learn more about the different scopes and how to use them, please refer to the Authentication section.
Initialization
To initialize the SDK you need to provide a configuration object:
import { Tradr, AuthConfig } from '@tradrapi/trading-sdk';
const auth: AuthConfig = {
apiKey: '<YOUR-API-KEY>',
};
export const tradrApi = Tradr.make({
// Set the environment to connect to
// Generally these values do not need to be defined
exchange: {
serviceUrl: 'https:/live.tradrapi.com',
socketUrl: 'wss://live.tradrapi.com',
},
// Assign the authentication method
auth: auth,
// Set to true to enable debug mode
debug: true,
});
// Start using the SDK, for example, get an account
const account = await tradrApi.accounts.get(2);
When using the SDK in a frontend environment, it is recommended to make use of the JWT Token authentication method.
import { Tradr, AuthConfig } from '@tradrapi/trading-sdk';
const auth: AuthConfig = {
jwtToken: '<YOUR-JWT-TOKEN>',
};
export const tradrApi = Tradr.make({
// Set the environment to connect to
// Generally these values do not need to be defined
exchange: {
serviceUrl: 'https://live.tradrapi.com',
socketUrl: 'wss://live.tradrapi.com',
},
// Assign the authentication method
auth: auth,
// Set to true to enable debug mode
debug: true,
});
// Start using the SDK, for example, get an account
const account = await tradrApi.accounts.get(2);
Only one of the of these authentication methods is required at any given time.
Configuration Overview
The following is an overview of the configuration options available to you when initializing the SDK. In most cases, these values do not need to be defined and can safely be omitted.
export enum ChartType {
TV = 'TradingView',
}
export interface Config {
/** The exchange configuration to use */
exchange?: ExchangeConfig;
/** Your auth credentials */
auth?: AuthConfig;
/** Enables debug logs (recommended for development environments) */
debug?: boolean;
/** Specify a logger interface for debug logs. Applicable if debug mode is enabled */
logger?: Logger;
/** The type of charting library used */
chart?: ChartType;
/** Optionally define middleware to bind with the request/response lifecycle */
http?: HttpConfig;
}
export interface ExchangeConfig {
/** The URL to the TradrAPI service (defaults to https://live.tradrapi.com) */
serviceUrl?: string;
/** The URL to the TradrAPI socket (defaults to wss://live.tradrapi.com) */
socketUrl?: string;
}
export interface AuthConfig {
/** The JWT token to use for authentication */
jwtToken?: string;
/** The API key to use for authentication */
apiKey?: string;
/** The manager secret key to use for authentication */
managerSecretKey?: string;
}