Overview

Complete B2TRADER API overview covering REST and WebSocket APIs. Learn when to use each API type, available endpoints, and integration guidelines for trading applications

Introduction

B2TRADER provides developers with two distinct methods for data delivery, each optimized for specific use cases and performance requirements: REST and WebSocket APIs.

The REST API provides read access to market data as well as both read and write access to trading operations. It serves as the foundation for synchronous data operations where immediate confirmation and guaranteed delivery are essential.

The WebSocket API provides access to public market data streaming as well as private account updates. It delivers real-time updates with low latency, making it ideal for live trading environments.

This dual approach provides developers with flexible options for building robust, scalable trading applications that can handle both operational requirements and real-time market dynamics.

When to use REST API

  • Account configuration and settings: Managing user preferences and system configurations.

  • Order placement and modification: Creating, updating, and canceling trading orders.

  • Historical data retrieval: Accessing past trading records and market data.

  • One-time data requests: Retrieving specific information that doesn't require continuous updates.

  • Administrative operations: Account management and system administration tasks.

When to use WebSocket API

  • Real-time price monitoring: Live market price feeds and ticker updates.

  • Live position tracking: Continuous monitoring of open and closed positions.

  • Order book visualization: Real-time depth of market data.

  • Market data feeds: Streaming market statistics and trading activity.

  • Account balance monitoring: Live updates of account equity and margin status.

General considerations

The following applies to all interface descriptions provided in this documentation:

  • Endpoints: All endpoints are relative and resolved based on a specified hostname (indicated as {host}).

  • Authentication: Only private methods are used to access data. Therefore, all requests, except for Authentication, must include an appropriate access token.

  • Data format: Both APIs return results in JSON format.

  • Security: All communications use secure protocols (HTTPS for REST, WSS for WebSocket).

Authentication

API access requires an access token for both REST and WebSocket connections.

Authentication follows a two-step process:

  1. Generate an offline token in the Trading terminal.

  2. Exchange the offline token for an access token via API call.

Token types

Offline token

  • Limit: 10 tokens per account

  • Validity: 1 year

  • Management: Can be revoked or deleted at any time

  • Purpose: Generate access tokens

Access token

  • Type: Bearer token

  • Validity: 60 minutes

  • Purpose: Authorize API requests

1

Generate offline token

To generate an offline token:

  1. In the Trading terminal, open Settings and select API token management.

  2. Click + Create new.

  3. In the New API token popup, fill in a Name for the token, to help you identify it later.

  4. Click Create.

The newly generated token will be displayed and available for copying, along with its name and expiration date.

2

Obtain access token

Request an access token using your offline token.

Endpoint: POST /frontoffice/api/v4/access-token

Request body:

Response (Success):

accessToken string

The access token for API authorization.

expiresIn integer

The token lifetime, in seconds.

tokenType string

The authentication type, always "Bearer".

3

Using access tokens

Include the access token in API requests:

Access tokens must be refreshed before expiration by repeating the Step 2 with your offline token.

REST API: Synchronous data operations

The REST API serves as the foundation for synchronous data operations within the B2TRADER platform. This approach follows standard HTTP protocols and is ideal for operations requiring immediate confirmation and guaranteed delivery.

Key characteristics

  • Request-response operations where immediate confirmation is required.

  • Account management including settings and configuration.

  • Order placement and modification with guaranteed delivery.

  • Historical data retrieval for analysis and reporting.

  • Stateless operations that don't require persistent connections.

HTTP response codes

B2TRADER API uses conventional HTTP response codes to indicate the success or failure of requests.

Success codes:

  • 200 OK — Request successful

Error codes:

  • 400 Bad Request — Invalid request parameters

  • 401 Unauthorized — Authentication required

  • 403 Forbidden — Insufficient permissions

  • 404 Not Found — Resource not found

  • 429 Too Many RequestsRate limit exceeded

  • 500 Internal Server Error — Server error

In case of an error, an object will be returned with the following structure:

Available endpoints

  • Trading operations: Create, modify, and cancel orders; open, close, and modify positions; control price trigger settings.

  • Trading history: Retrieve detailed execution records for positions and orders.

  • Settings and configurations: Access account information, market specifications, trading sessions, and asset details.

Rate limits

Rate limits are applied per minute for each unique AccountId to ensure fair resource usage and maintain optimal API performance. All limits use the Fixed Window strategy.

When rate limits are exceeded, the API returns a 429 Too Many Requests HTTP status code.

Trading methods

  • Default limit: 600 requests per minute for all methods.

  • Reduced limit (200 rpm) applies to:

    • Get order data methods

    • Bulk close positions method

    • Price triggers methods

History methods

  • All request types: 60 requests per minute.

Settings methods

  • GET requests: 100 requests per minute.

  • POST and DELETE requests: 60 requests per minute.

Rate limits are calculated independently for each method category. For example, you can make 100 GET requests to Settings methods and 60 requests to History methods within the same minute without hitting rate limits.

WebSocket API: Real-time data streaming

The WebSocket API delivers real-time updates with minimal latency, essential for modern trading applications. The implementation uses unidirectional communication from server to client, ensuring efficient data delivery.

Key characteristics

  • Unidirectional communication from server to client for optimal performance.

  • Real-time market data for live trading environments.

  • Position and order updates as they occur in real-time.

  • Low-latency data delivery for time-sensitive trading operations.

  • Persistent connections maintaining continuous data flow.

SignalR implementation

B2TRADER utilizes AspNetCore SignalR for WebSocket message organization and transmission, providing a robust and scalable real-time communication framework.

Resources:

SignalR provides a structured approach to real-time communication through standardized message formatting and connection management.

Connection lifecycle

The data transfer process consists of two essential phases:

  1. Connection establishment — Initial handshake, authentication, and subscription setup.

  2. Data streaming — Continuous real-time data flow with automatic reconnection handling.

Message types

SignalR utilizes numerical type indicators for different operations:

Type
Operation
Description

**4**

Subscribe

Initiate streaming subscription to a specific data feed

**5**

Unsubscribe

Terminate streaming subscription

**3**

Confirmation

Server confirmation of unsubscription

**2**

Streaming

Active data transmission in progress

Available stream types

Integration best practices

API selection strategy

  • Use REST API for operational tasks requiring confirmation (order placement, account management).

  • Use WebSocket API for real-time monitoring and market data visualization.

  • Implement both APIs in comprehensive trading applications for optimal functionality.

Performance optimization

  • Implement proper connection pooling for REST API requests.

  • Use WebSocket subscriptions efficiently by subscribing only to required data streams.

  • Handle reconnection logic for WebSocket connections to ensure data continuity.

  • Implement appropriate error handling and retry mechanisms.

Security considerations

  • Store authentication tokens securely and implement token refresh mechanisms.

  • Use secure connections (HTTPS/WSS) for all API communications.

  • Implement proper input validation and sanitization.

  • Monitor API usage and implement rate limiting on the client side.

This comprehensive API architecture enables developers to build sophisticated trading applications that can handle both real-time market dynamics and operational trading requirements efficiently.

Last updated