To build the client you have to pass an object that will provide the API URL, API key and secret key. This allows maximum flexibility.
import { Client } from '@hastobegood/crypto-clients-binance';
const client = new Client({
apiInfoProvider: {
getApiUrl: async (): Promise<string> => 'binance-api-url',
getApiKey: async (): Promise<string> => 'binance-api-key',
getSecretKey: async (): Promise<string> => 'binance-secret-key',
},
});
For example, you can build a client using the API testnet URL or a client that will fetch and cache the API key and secret.
import { ApiInfoProvider, Client } from '@hastobegood/crypto-clients-binance';
class BinanceSecretsProvider implements ApiInfoProvider {
private secrets?: MySecretsWrapper;
async getApiUrl(): Promise<string> {
return (await this.#getSecrets()).apiUrl;
}
async getApiKey(): Promise<string> {
return (await this.#getSecrets()).apiKey;
}
async getSecretKey(): Promise<string> {
return (await this.#getSecrets()).secretKey;
}
async #getSecrets(): Promise<MySecretsWrapper> {
if (!this.secrets) {
// fetch your secrets here
}
return this.secrets;
}
}
const apiInfoProvider = new BinanceSecretsProvider();
const client = new Client(apiInfoProvider);
Additional options can be set to the client.
import { Client } from '@hastobegood/crypto-clients-binance';
const client = new Client({
apiInfoProvider: { ... },
httpOptions: {
timeout: 2_000 // timeout for HTTP calls to Binance API
},
});
You can subscribe to events linked to the HTTP requests and responses sent to Binance API.
This can be useful if you need for example to log those requests.
import { Client } from '@hastobegood/crypto-clients-binance';
const client = new Client({ ... });
// log HTTP request (api, endpoint, method and params)
client.onHttpRequest((httpRequest: HttpRequest) => console.log(httpRequest));
// log HTTP response (api, endpoint, method, params and status)
client.onHttpResponse((httpRequest: HttpRequest, httpResponse?: HttpResponse) => console.log(httpRequest, htppResponse));
import { GetAccountInfoCommand, GetAccountInfoCommandOutput } from '@hastobegood/crypto-clients-binance';
const output: GetAccountInfoCommandOutput = await client.send(new GetAccountInfoCommand());
import { TestConnectivityCommand, EmptyCommandOutput } from '@hastobegood/crypto-clients-binance';
const output: EmptyCommandOutput = await client.send(new TestConnectivityCommand());
import { GetServerTimeCommand, GetServerTimeCommandOutput } from '@hastobegood/crypto-clients-binance';
const output: GetServerTimeCommandOutput = await client.send(new GetServerTimeCommand());
import { GetExchangeInfoCommand, GetExchangeInfoCommandInput, GetExchangeInfoCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetExchangeInfoCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetExchangeInfoCommandOutput = await client.send(new GetExchangeInfoCommand(input));
import { GetCandlestickDataCommand, GetCandlestickDataCommandInput, GetCandlestickDataCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetCandlestickDataCommandInput = {
symbol: 'BTCUSDT',
interval: '1m',
};
const output: GetCandlestickDataCommandOutput = await client.send(new GetCandlestickDataCommand(input));
import { GetAveragePriceCommand, GetAveragePriceCommandInput, GetAveragePriceCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetAveragePriceCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetAveragePriceCommandOutput = await client.send(new GetAveragePriceCommand(input));
import { GetPriceChangeCommand, GetPriceChangeCommandInput, GetPriceChangeCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetPriceChangeCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetPriceChangeCommandOutput = await client.send(new GetPriceChangeCommand(input));
import { GetCurrentPriceCommand, GetCurrentPriceCommandInput, GetCurrentPriceCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetCurrentPriceCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetCurrentPriceCommandOutput = await client.send(new GetCurrentPriceCommand(input));
import { GetOrderBookPriceCommand, GetOrderBookPriceCommandInput, GetOrderBookPriceCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetOrderBookPriceCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetOrderBookPriceCommandOutput = await client.send(new GetOrderBookPriceCommand(input));
import { SendOrderCommand, SendOrderCommandInput, SendOrderCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: SendOrderCommandInput = {
symbol: 'BTCUSDT',
side: 'BUY',
type: 'MARKET',
quoteOrderQty: 100,
};
const output: SendOrderCommandOutput = await client.send(new SendOrderCommand(input));
import { GetOrderCommand, GetOrderCommandInput, GetOrderCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetOrderCommandInput = {
symbol: 'BTCUSDT',
orderId: 123456789,
};
const output: GetOrderCommandOutput = await client.send(new GetOrderCommand(input));
import { GetOpenOrdersListCommand, GetOpenOrdersListCommandInput, GetOpenOrdersListCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetOpenOrdersListCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetOpenOrdersListCommandOutput = await client.send(new GetOpenOrdersListCommand(input));
import { GetAllOrdersListCommand, GetAllOrdersListCommandInput, GetAllOrdersListCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetAllOrdersListCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetAllOrdersListCommandOutput = await client.send(new GetAllOrdersListCommand(input));
import { CancelOrderCommand, CancelOrderCommandInput, CancelOrderCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: CancelOrderCommandInput = {
symbol: 'BTCUSDT',
orderId: 123456789,
};
const output: CancelOrderCommandOutput = await client.send(new CancelOrderCommand(input));
import { GetOrderCountUsageCommand, GetOrderCountUsageCommandOutput } from '@hastobegood/crypto-clients-binance';
const output: GetOrderCountUsageCommandOutput = await client.send(new GetOrderCountUsageCommand());
import { GetAccountTradesListCommand, GetAccountTradesListCommandInput, GetAccountTradesListCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetAccountTradesListCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetAccountTradesListCommandOutput = await client.send(new GetAccountTradesListCommand(input));
import { GetRecentTradesListCommand, GetRecentTradesListCommandInput, GetRecentTradesListCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetRecentTradesListCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetRecentTradesListCommandOutput = await client.send(new GetRecentTradesListCommand(input));
import { GetOldTradesListCommand, GetOldTradesListCommandInput, GetOldTradesListCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetOldTradesListCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetOldTradesListCommandOutput = await client.send(new GetOldTradesListCommand(input));
import { GetAggregateTradesListCommand, GetAggregateTradesListCommandInput, GetAggregateTradesListCommandOutput } from '@hastobegood/crypto-clients-binance';
const input: GetAggregateTradesListCommandInput = {
symbol: 'BTCUSDT',
};
const output: GetAggregateTradesListCommandOutput = await client.send(new GetAggregateTradesListCommand(input));