Server Management
Servers are a core component of the TradrAPI system. Each server in TradrAPI belongs to a specific Trading Platform.
TradrAPI can be configured to support multiple servers for each Trading Platform. This allows you to have multiple servers for a single Trading Platform, each with different settings.
For example, you may have a server for live trading and another server for testing.
Fetching Servers
Servers can be fetched using the get()
method under the admin.server
namespace. The TradrAPI server ID should be passed
as the first argument.
import { ServerDto } from '@tradrapi/trading-sdk';
const result: ServerDto[] = await tradrApi.admin.server.get(11);
In order to list all available servers, the list()
method can be used:
import { PaginatedResponse, ServerDto } from '@tradrapi/trading-sdk';
const result: PaginatedResponse<ServerDto> = await tradrApi.admin.server.list();
The list()
method accepts an optional ListServerDto
parameter, which can be used filter and paginate the results:
interface ListServerDto {
/**
* The current page being returned.
* @default 1
*/
page?: number;
/**
* Number of records per page.
* @default 10
*/
limit?: number;
/**
* The sort direction of the records being returned
* @default "DESC"
*/
sortDir?: SortDir;
/** The id of the exchange which the server belongs to */
exchangeId?: number;
/** The id of the server */
serverId?: number;
/** If true, will contain the number of online users for each server */
includeOnlineUsers?: boolean;
/**
* If true, will also include disabled servers in the results
* @default false
*/
includeDisabled?: boolean;
/** If true, it will retrieve all servers with no pagination */
returnAll?: boolean;
}
The ServerDto
interface is defined as follows:
interface ServerDto {
/**
* The date the server was created
* @format date-time
*/
createdAt: string;
/** The exchange details */
exchange: object;
/** The id of the exchange the server belongs to */
exchangeId: number;
/** The TradrAPI ID of the entity */
id: number;
/** Indicates if the server is disabled */
isEnabled?: boolean;
/** The name of the server */
name: string;
/** Any offset in hours to apply to the server timezone */
offsetHours: number;
/** The number of online users on the server */
onlineUsers?: number;
/** The server ownership */
ownership?: object;
/** The timezone of the server to IANA standards */
serverTimezone: string;
/** Additional settings belonging to the server */
settings: object;
/** The type of the server */
type: 'demo' | 'real' | 'mixed' | 'margin';
/**
* The date the server was last updated
* @format date-time
*/
updatedAt: string;
/** The base url of the server API */
url: string;
}
Creating A New Server
A new server can be created using the create()
method under the admin.server
namespace.
The CreateServerDto
should be passed as the first argument.
import { ServerDto, ServerType, CreateServerDtoCredentialType } from '@tradrapi/trading-sdk';
const result: ServerDto = await tradrApi.admin.server.create({
exchangeId: 1,
name: 'server_name',
url: 'https://api.my-server.com',
serverTimezone: 'Etc/Greenwich',
offsetHours: 0,
username: 'admin',
password: 'password',
type: ServerType.Demo,
credentialType: CreateServerDtoCredentialType.Default,
});
The interface definition for CreateServerDto
is as follows:
interface CreateServerDto {
/** The type of the server */
type: ServerType;
/** The timezone of the server */
serverTimezone: string;
/**
* Any offset in hours to apply to the server timezone
* @default 0
*/
offsetHours?: number;
/** The name of the server */
name: string;
/** The base url of the server API */
url: string;
/** The username to use when authenticating with the server */
username: string;
/** The password to use when authenticating with the server */
password: string;
/** Defines the use of the credential, either as a default credential, or for logs or for a specific project */
credentialType: number | CreateServerDtoCredentialType;
/** The id of the exchange the server belongs to */
exchangeId: number;
/**
* Whether the server is enabled and can be used
* @default true
*/
isEnabled?: boolean;
}
Updating An Existing Server
Existing servers can be updated using the update()
method under the admin.server
namespace. The
UpdateServerDto
should be passed as the first argument.
import { ServerType, UpdateServerDtoCredentialType } from '@tradrapi/trading-sdk';
const result: boolean = await tradrApi.admin.server.update({
serverId: 1,
name: 'New Server Name',
});
Care must be taken when updating an active server. If the changes made are incorrect this will result in the server being disconnected from the Trading Platform.
interface UpdateServerDto {
/** The id of the server to update */
serverId: number;
/** The type of the server */
type?: ServerType;
/** The timezone of the server */
serverTimezone?: string;
/** Any offset in hours to apply to the server timezone */
offsetHours?: number;
/** The name of the server */
name?: string;
/** The base url of the server API */
url?: string;
/** The username to use when authenticating with the server */
username?: string;
/** The password to use when authenticating with the server */
password?: string;
/** Defines the use of the credential, either as a default credential, or for logs or for a specific project */
credentialType?: number | UpdateServerDtoCredentialType;
/** The id of the exchange the server belongs to */
exchangeId?: number;
/** Whether the server is enabled and can be used */
isEnabled?: boolean;
}
Deleting A Server
It is nly possible to delete a server if there are no active accounts associated with it. To delete a server, use the
delete()
method under the admin.server
namespace and pass the server ID as the first argument.
const result: boolean = await tradrApi.admin.server.delete(11);