npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hastobegood/crypto-clients-binance

v0.11.0

Published

* [Exchange website](https://www.binance.com/) * [API documentation](https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md)

Downloads

223

Readme

Binance

Client

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

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
  },
});

Event emitters for HTTP requests/responses

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));

Commands

Account

Get account information

import { GetAccountInfoCommand, GetAccountInfoCommandOutput } from '@hastobegood/crypto-clients-binance';

const output: GetAccountInfoCommandOutput = await client.send(new GetAccountInfoCommand());

General

Test connectivity

import { TestConnectivityCommand, EmptyCommandOutput } from '@hastobegood/crypto-clients-binance';

const output: EmptyCommandOutput = await client.send(new TestConnectivityCommand());

Get server time

import { GetServerTimeCommand, GetServerTimeCommandOutput } from '@hastobegood/crypto-clients-binance';

const output: GetServerTimeCommandOutput = await client.send(new GetServerTimeCommand());

Get exchange information

import { GetExchangeInfoCommand, GetExchangeInfoCommandInput, GetExchangeInfoCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetExchangeInfoCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetExchangeInfoCommandOutput = await client.send(new GetExchangeInfoCommand(input));

Market

Get candlestick data

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));

Get average price

import { GetAveragePriceCommand, GetAveragePriceCommandInput, GetAveragePriceCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetAveragePriceCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetAveragePriceCommandOutput = await client.send(new GetAveragePriceCommand(input));

Get price change

import { GetPriceChangeCommand, GetPriceChangeCommandInput, GetPriceChangeCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetPriceChangeCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetPriceChangeCommandOutput = await client.send(new GetPriceChangeCommand(input));

Get current price

import { GetCurrentPriceCommand, GetCurrentPriceCommandInput, GetCurrentPriceCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetCurrentPriceCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetCurrentPriceCommandOutput = await client.send(new GetCurrentPriceCommand(input));

Get order book price

import { GetOrderBookPriceCommand, GetOrderBookPriceCommandInput, GetOrderBookPriceCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetOrderBookPriceCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetOrderBookPriceCommandOutput = await client.send(new GetOrderBookPriceCommand(input));

Order

Send order

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));

Get order

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));

Get open orders list

import { GetOpenOrdersListCommand, GetOpenOrdersListCommandInput, GetOpenOrdersListCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetOpenOrdersListCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetOpenOrdersListCommandOutput = await client.send(new GetOpenOrdersListCommand(input));

Get all orders list

import { GetAllOrdersListCommand, GetAllOrdersListCommandInput, GetAllOrdersListCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetAllOrdersListCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetAllOrdersListCommandOutput = await client.send(new GetAllOrdersListCommand(input));

Cancel order

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));

Get order count usage

import { GetOrderCountUsageCommand, GetOrderCountUsageCommandOutput } from '@hastobegood/crypto-clients-binance';

const output: GetOrderCountUsageCommandOutput = await client.send(new GetOrderCountUsageCommand());

Trade

Get account trades list

import { GetAccountTradesListCommand, GetAccountTradesListCommandInput, GetAccountTradesListCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetAccountTradesListCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetAccountTradesListCommandOutput = await client.send(new GetAccountTradesListCommand(input));

Get recent trades list

import { GetRecentTradesListCommand, GetRecentTradesListCommandInput, GetRecentTradesListCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetRecentTradesListCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetRecentTradesListCommandOutput = await client.send(new GetRecentTradesListCommand(input));

Get old trades list

import { GetOldTradesListCommand, GetOldTradesListCommandInput, GetOldTradesListCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetOldTradesListCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetOldTradesListCommandOutput = await client.send(new GetOldTradesListCommand(input));

Get aggregate trades list

import { GetAggregateTradesListCommand, GetAggregateTradesListCommandInput, GetAggregateTradesListCommandOutput } from '@hastobegood/crypto-clients-binance';

const input: GetAggregateTradesListCommandInput = {
  symbol: 'BTCUSDT',
};

const output: GetAggregateTradesListCommandOutput = await client.send(new GetAggregateTradesListCommand(input));