hs-api-crypto-exchanges v0.3.0
hs-api-crypto-exchanges
A simple normalized API and WebSocket for crypto exchanges. This project is mainly focused on Public REST Endpoints and WebSocket channels.
Available Exchanges
- Binance
- Bitfinex
- Bitstamp
- Bittrex
- CoinbasePro
Available Functions
- exchange.pairs()
- exchange.tickers()
- exchange.webSocket().ticker()
- exchange.webSocket().trade()
- exchange.webSocket().allTicker()
- exchange.webSocket().allTrade()
- exchange.api().getJson()
Notice
- Bitstamp - NodeJS version 12 will throw "Invalid header value char" error. Make sure to use the option
--http-parser=legacy. - Coinbasepro - API request is slow becuase it is done in series to bypass request limit per/sec.
Install
npm install hs-api-crypto-exchanges --saveGetting started
const Exchange = require('hs-api-crypto-exchanges');REST Endpoints
exchange.pairs()
Fetch all available pairs by request-promise. Depends on the exchange, this will include inactive pairs.
Exchange.binance.pairs().then(pairs => {
console.log(pairs);
});
Exchange.bitfinex.pairs().then(pairs => {
console.log(pairs);
});
// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.pairs().then(pairs => {
console.log(pairs);
});
Exchange.bittrex.pairs().then(pairs => {
console.log(pairs);
});
Exchange.coinbasepro.pairs().then(pairs => {
console.log(pairs);
});Pair
| Param | Type | Description |
|---|---|---|
| exchange | String | Exchange ID. Always lower case with no space. |
| symbol | String | The symbol for the associated exchange. Exchange specific symbol. |
| base | String | The base unit for the associated exchange symbol. Always upper case. |
| quote | String | The quote unit for the associated exchange symbol. Always upper case. |
| active | Boolean | Depends on the exchange API, this value can be all true and exclude inactive pairs. |
[
Pair { exchange: 'binance', symbol: 'ETHBTC', base: 'ETH', quote: 'BTC', active: true },
Pair { exchange: 'binance', symbol: 'LTCBTC', base: 'LTC', quote: 'BTC', active: true },
Pair { exchange: 'binance', symbol: 'BNBBTC', base: 'BNB', quote: 'BTC', active: true },
// -- many more --
]exchange.tickers()
Fetch all available tickers by request-promise. Depends on the exchange, this will include inactive pairs. Make sure to check ticker.pair.active to confirm.
Exchange.binance.tickers().then(tickers => {
console.log(tickers);
});
Exchange.bitfinex.tickers().then(tickers => {
console.log(tickers);
});
// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.tickers().then(tickers => {
console.log(tickers);
});
Exchange.bittrex.tickers().then(tickers => {
console.log(tickers);
});
Exchange.coinbasepro.tickers().then(tickers => {
console.log(tickers);
});Ticker
| Param | Type | Description |
|---|---|---|
| pair | Object | Pair. |
| last | Number | Last price. |
| lowestAsk | Number / null | Lowest ask price. |
| highestBid | Number / null | Highest bid price. |
| percentChange | Number / null | Daily price percentage change. |
| baseVolume | Number / null | Daily base volume. |
| quoteVolume | Number / null | Daily quote volume. |
| high | Number / null | Daily highest price. |
| low | Number / null | Daily lowest price. |
| vwap | Number / null | Daily volume weighted average price. |
| updatedAt | Date / null | Ticker updated at. |
| rawData | Array / Object / null | Original raw ticker data. |
[
Ticker {
pair: Pair {
exchange: 'binance',
symbol: 'BTCUSDT',
base: 'BTC',
quote: 'USDT',
active: true
},
last: 8051.07,
lowestAsk: 8051.64,
highestBid: 8050.11,
percentChange: 0.9,
baseVolume: 29969.890927,
quoteVolume: 240744122.84976387,
high: 8124.92,
low: 7929.03,
vwap: 8032.86616679,
updatedAt: '2019-10-17T21:30:20.796Z',
rawData: {
symbol: 'BTCUSDT',
priceChange: '71.81000000',
priceChangePercent: '0.900',
weightedAvgPrice: '8032.86616679',
prevClosePrice: '7979.26000000',
lastPrice: '8051.07000000',
lastQty: '0.03216600',
bidPrice: '8050.11000000',
bidQty: '0.04719500',
askPrice: '8051.64000000',
askQty: '0.03329000',
openPrice: '7979.26000000',
highPrice: '8124.92000000',
lowPrice: '7929.03000000',
volume: '29969.89092700',
quoteVolume: '240744122.84976388',
openTime: 1571261420796,
closeTime: 1571347820796,
firstId: 190861699,
lastId: 191221055,
count: 359357
}
},
// -- many more --
]exchange.api(cache).getJson()
Fetch exchange specific endpoints by hs-cache-request-promise.
// no cache
Exchange.binance.api().getJson('/v1/ticker/24hr').then(rawTicker => {
console.log(rawTicker);
});
// no cache
Exchange.coinbasepro.api().getJson('/products/BTC-USD/ticker').then(rawTicker => {
console.log(rawTicker);
});
// no cache; NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.api().tradingPairsInfo().then(tradingPairsInfo => {
console.log(tradingPairsInfo)
});
// cache ttl 30 sec to a folder
Exchange.binance.api({ttl: 30000, dir: './cache/folder'}).getJson('/v1/exchangeInfo').then(exchangeInfo => {
console.log(exchangeInfo);
});
// cache ttl 60 sec to a folder
Exchange.bitfinex.api(ttl: 60000, dir: './cache/folder'}).pubListCurrency().then(pubListCurrency => {
console.log(pubListCurrency)
});WebSocket
By default, the web socket will automatically reconnect if there is any interruption. If unable to reconnect, it will throw an Error.
exchange.webSocket().ticker(symbols, callback)
Stream live ticker by callback.
| Param | Type | Description |
|---|---|---|
| symbols | Array | The associated exchange symbols. |
Exchange.binance.webSocket().ticker(['BTCUSDT', 'ETHUSDT'], ticker => {
console.log(ticker);
});
Exchange.bitfinex.webSocket().ticker(['BTCUSD', 'ETHUSD'], ticker => {
console.log(ticker);
});
// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.webSocket().ticker(['btcusd', 'ethusd'], ticker => {
console.log(ticker); // bitstamp do not support ws ticker. Only last price & updatedAt available.
});
Exchange.bittrex.webSocket().ticker(['USD-BTC', 'USD-ETH'], ticker => {
console.log(ticker);
});
Exchange.coinbasepro.webSocket().ticker(['BTC-USD', 'BTC-USD'], ticker => {
console.log(ticker);
});Ticker
| Param | Type | Description |
|---|---|---|
| pair | Object | Pair. |
| last | Number | Last price. |
| lowestAsk | Number / null | Lowest ask price. |
| highestBid | Number / null | Highest bid price. |
| percentChange | Number / null | Daily price percentage change. |
| baseVolume | Number / null | Daily base volume. |
| quoteVolume | Number / null | Daily quote volume. |
| high | Number / null | Daily highest price. |
| low | Number / null | Daily lowest price. |
| vwap | Number / null | Daily volume weighted average price. |
| updatedAt | Date / null | Ticker updated at. |
| rawData | Array / Object / null | Original raw ticker data. |
{
Ticker {
pair: Pair {
exchange: 'binance',
symbol: 'BTCUSDT',
base: 'BTC',
quote: 'USDT',
active: true
},
last: 8051.07,
lowestAsk: 8051.64,
highestBid: 8050.11,
percentChange: 0.9,
baseVolume: 29969.890927,
quoteVolume: 240744122.84976387,
high: 8124.92,
low: 7929.03,
vwap: 8032.86616679,
updatedAt: '2019-10-17T21:30:20.796Z',
rawData: {
symbol: 'BTCUSDT',
priceChange: '71.81000000',
priceChangePercent: '0.900',
weightedAvgPrice: '8032.86616679',
prevClosePrice: '7979.26000000',
lastPrice: '8051.07000000',
lastQty: '0.03216600',
bidPrice: '8050.11000000',
bidQty: '0.04719500',
askPrice: '8051.64000000',
askQty: '0.03329000',
openPrice: '7979.26000000',
highPrice: '8124.92000000',
lowPrice: '7929.03000000',
volume: '29969.89092700',
quoteVolume: '240744122.84976388',
openTime: 1571261420796,
closeTime: 1571347820796,
firstId: 190861699,
lastId: 191221055,
count: 359357
}
}
}exchange.webSocket().trade(symbols, callback)
Stream live trade by callback.
| Param | Type | Description |
|---|---|---|
| symbols | Array | The associated exchange symbols. |
Exchange.binance.webSocket().trade(['BTCUSDT', 'ETHUSDT'], trade => {
console.log(trade);
});
Exchange.bitfinex.webSocket().trade(['BTCUSD', 'ETHUSD'], trade => {
console.log(trade);
});
// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.webSocket().trade(['btcusd', 'ethusd'], trade => {
console.log(trade);
});
Exchange.bittrex.webSocket().trade(['USD-BTC', 'USD-ETH'], trade => {
console.log(trade);
});
Exchange.coinbasepro.webSocket().trade(['BTC-USD', 'BTC-USD'], trade => {
console.log(trade);
});Trade
| Param | Type | Description |
|---|---|---|
| pair | Object | Pair from exchange.pairs() |
| type | String | The only possible value is 'buy' or 'sell'. |
| price | Number / null | Trade price. |
| baseAmount | Number / null | Trade base amount. |
| quoteAmount | Number / null | Trade quote amount. |
| tradeID | Number / null | Trade ID. |
| updatedAt | Date / null | Trade updated at. |
| rawData | Array / Object / null | Original raw trade data. |
{
Trade {
pair: Pair { exchange: 'binance', symbol: 'BTCUSDT', base: 'BTC', quote: 'USDT', active: true },
type: 'buy',
price: 8087.93,
baseAmount: 0.034148,
quoteAmount: 276.18663364,
tradeID: 173090135,
updatedAt: '2019-10-18T00:52:04.500Z',
rawData: {
eventType: 'aggTrade',
eventTime: 1571359924500,
symbol: 'BTCUSDT',
price: '8087.93000000',
quantity: '0.03414800',
maker: false,
isBuyerMaker: true,
tradeId: 173090135
}
}
}exchange.webSocket().allTicker(callback)
Stream all available live ticker by callback. It will automatically subscribe to new symbols.
Exchange.binance.webSocket().allTicker(ticker => {
console.log(ticker);
});
Exchange.bitfinex.webSocket().allTicker(ticker => {
console.log(ticker);
});
// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.webSocket().allTicker(ticker => {
console.log(ticker);
});
Exchange.bittrex.webSocket().allTicker(ticker => {
console.log(ticker);
});
Exchange.coinbasepro.webSocket().allTicker(ticker => {
console.log(ticker);
});exchange.webSocket().allTrade(callback)
Stream all available live trade by callback. It will automatically subscribe to new symbols. This is resource-intensive function. Unless you really need all available trades, use exchange.webSocket().trade().
Exchange.binance.webSocket().allTrade(trade => {
console.log(trade);
});
Exchange.bitfinex.webSocket().allTrade(trade => {
console.log(trade);
});
// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.webSocket().allTrade(trade => {
console.log(trade);
});
Exchange.bittrex.webSocket().allTrade(trade => {
console.log(trade);
});
Exchange.coinbasepro.webSocket().allTrade(trade => {
console.log(trade);
});License
This project is licensed under the MIT License - see the LICENSE.md for details.