poloniex-api-node is a simple node.js wrapper for Poloniex REST and WebSocket API.
REST API supports both Callback and Promise.
WebSocket API is supported for the public order book and your private account as well.
- Changelog
- Install
- Quick examples
- Usage
- Constructor
- REST API
- WebSocket API
- Contributors
- License
See detailed Changelog
Push API using WAMP is deprecated and removed. Only WebSocket API is supported.
Parameter version
for openWebSocket
method has been removed and version = 2
(WebSocket) is now the default and the only option.
WebSocket channel footer
has been renamed to volume
. The change is in line with Poloniex API documentation.
- Added WebSocket API for account notifications channel (private account)
- Added new
returnOrderStatus
API method (REST API)
npm install --save poloniex-api-node
See additional examples in examples folder
When calling the REST API methods, Callback is always the last parameter. When callback parameter is not present, the method will return a Promise.
Example 1 (REST API using Callback):
const Poloniex = require('poloniex-api-node');
let poloniex = new Poloniex();
poloniex.returnTicker((err, ticker) => {
if (err) {
console.log(err.message);
} else {
console.log(ticker);
}
});
Example 2 (REST API using Callback):
const Poloniex = require('poloniex-api-node');
let poloniex = new Poloniex();
poloniex.returnTicker(function (err, ticker) {
if (err) {
console.log(err.message);
} else {
console.log(ticker);
}
});
Example 3 (REST API using Promise):
const Poloniex = require('poloniex-api-node');
let poloniex = new Poloniex();
poloniex.returnTicker().then((ticker) => {
console.log(ticker);
}).catch((err) => {
console.log(err.message);
});
Example 4 (set socketTimeout
to 15 seconds):
const Poloniex = require('poloniex-api-node');
let poloniex = new Poloniex('your_key', 'your_secret', { socketTimeout: 15000 });
poloniex.returnLoanOrders('BTC', null, function (err, ticker) {
if (!err) console.log(ticker);
});
Example 4 (set nonce
to custom function):
const Poloniex = require('poloniex-api-node');
let poloniex = new Poloniex('your_key', 'your_secret', { nonce: () => new Date().time() });
poloniex.returnLoanOrders('BTC', null, function (err, ticker) {
if (!err) console.log(ticker);
});
Example (WebSocket API):
const Poloniex = require('poloniex-api-node');
let poloniex = new Poloniex();
poloniex.subscribe('ticker');
poloniex.subscribe('BTC_ETC');
poloniex.on('message', (channelName, data, seq) => {
if (channelName === 'ticker') {
console.log(`Ticker: ${data}`);
}
if (channelName === 'BTC_ETC') {
console.log(`order book and trade updates received for currency pair ${channelName}`);
console.log(`data sequence number is ${seq}`);
}
});
poloniex.on('open', () => {
console.log(`Poloniex WebSocket connection open`);
});
poloniex.on('close', (reason, details) => {
console.log(`Poloniex WebSocket connection disconnected`);
});
poloniex.on('error', (error) => {
console.log(`An error has occured`);
});
poloniex.openWebSocket();
To access the private Poloniex API methods you must supply your API key id and key secret as the first two arguments. If you are only accessing the public API endpoints you can leave these two arguments out.
Default options:
{
socketTimeout: 60000,
keepAlive: true,
nonce: nonce(16),
headers: { 'User-Agent': 'poloniex-api-node *version*' }
}
Examples:
let poloniex;
poloniex = new Poloniex();
poloniex = new Poloniex({ socketTimeout: 10000 });
poloniex = new Poloniex('myKey', 'mySecret');
poloniex = new Poloniex('myKey', 'mySecret', { socketTimeout: 130000 });
socketTimeout
- the number of milliseconds to wait for the server to send the response before aborting the request (REST API)keepAlive
- keep open and reuse the underlying TCP connection (REST API)proxy
- proxy to be used for requests (REST API)nonce
- custom function that returns an unique and ever increasing numberagent
- sets specific http.Agent for REST API calls and WebSocket connection. (It is useful for using socks proxy to avoid the 403 error with CAPTCHA. See #20)headers
- HTTP Headers, such as User-Agent, can be set in the object. See https://github.com/request/request#custom-http-headers
For details about the API endpoints see full documentation at https://poloniex.com/support/api/
Important: When calling an API method with optional parameters, the parameters, when not wanted, need to be passed as null
.
Both Callback and Promise are supported.
Callback is always the last parameter. When callback parameter is not present the method will return a Promise.
The arguments passed to the callback function for each method are:
- An error or
null
if no error occurred. - An object containing the data returned by the Poloniex API.
Examples:
poloniex.returnTicker((err, ticker) => {
if (err) {
console.log(err.message);
} else {
console.log(ticker);
}
});
poloniex.returnTicker().then((ticker) => {
console.log(ticker);
}).catch((err) => {
console.log(err.message);
});
Parameter
depth
is not documented in the official Poloniex API documentation. The parameter can be set tonull
or an integer value.
Parameter
limit
is not documented in the official Poloniex API documentation. The parameter can be set tonull
or an integer value.
Examples:
poloniex.returnLoanOrders('BTC', null, (err, loanOrders) => {
if (err) {
console.log(err.message);
} else {
console.log(loanOrders);
}
});
poloniex.returnLoanOrders('BTC', null).then((loanOrders) => {
console.log(loanOrders);
}).catch((err) => {
console.log(err.message);
});
poloniex.returnBalances(function (err, balances) {
if (err) {
console.log(err.message);
} else {
console.log(balances);
}
});
poloniex.returnBalances().then((balances) => {
console.log(balances);
}).catch((err) => {
console.log(err.message);
});
Parameter
paymentId
is used for certain withdrawals (e.g. XMR) and when not wanted/needed should be passed asnull
.
This module implements the WebSocket API for receiving push notifications about the public order book, and your private account.
The push API using WAMP is deprecated and no longer supported by this module.
A listener is registered using the Poloniex.on()
method (since Poloniex
class inherits the EventEmitter
) and the listener will be invoked every time the named event is emitted.
Example:
let poloniex = new Poloniex();
poloniex.on('open', () => {
console.log(`WebSocket connection is open.`);
});
The following events can be emitted:
'open'
'message'
'close'
'error'
'heartbeat'
Emitted when WebSocket connection is established.
Emitted when an update on a subscribed channel is received.
See method subscribe
for details on data received and its format.
Emitted when WebSocket connection is closed.
See method closeWebSocket
for details on data received and its format.
Emitted when an error occurs.
Important: You have to set
error
listener otherwise your app will throw anError
and exit iferror
event will occur (see: Node.js Error events)
Example:
poloniex.on('error', (error) => {
console.log(error);
});
Emitted if there is no update for more than 60 seconds.
Opens WebSocket connection to Poloniex server.
If WebSocket connection is already open and openWebSocket
is called again, the existing connection is closed and a new one is opened (equivalent to a full reset of the WebSocket connection).
Event 'open'
is emitted when connection is established.
Example:
let poloniex = new Poloniex();
poloniex.on('open', () => {
console.log(`WebSocket connection has be opened.`);
});
poloniex.openWebSocket();
In order to receive updates over WebSocket, subscribe to following channels:
'ticker'
- ticker data- currencyPair (examples:
'BTC_ETH'
,'BTC_XMR'
) - price aggregated book 'volume'
- 24 Hour Exchange Volumeheartbeat
- heartbeataccountNotifications
- account notifications
When an update on the subscribed channel is received Poloniex
object emits the event 'message'
.
You can subscribe to a channel either before or after the WebSocket connection is opened with
openWebSocket
. If WebSocket connection is already open, the subscription is activated immediately. If WebSocket connection is not open yet, thesubscribe
is registering the subscription; all registered connections will be activated whenopenWebSocket
is issued.
Provides ticker updates.
Example:
let poloniex = new Poloniex();
poloniex.subscribe('ticker');
poloniex.on('message', (channelName, data) => {
if (channelName === 'ticker') {
console.log(`Ticker: ${data}`);
}
});
poloniex.openWebSocket();
Ticker updates will be in following format:
{
"currencyPair": "BTC_XRP",
"last": "0.00005432",
"lowestAsk": "0.00005440",
"highestBid": "0.00005432",
"percentChange": "-0.02878598",
"baseVolume": "2862.99229490",
"quoteVolume": "52479031.35647538",
"isFrozen": 0,
"24hrHigh": "0.00005598",
"24hrLow": "0.00005264"
}
Provides order book and trade updates.
Subscribe to the desired currencyPair, e.g. 'BTC_ETC'
, to receive order book and trade updates.
Example:
let poloniex = new Poloniex();
poloniex.openWebSocket();
poloniex.subscribe('BTC_ETH');
poloniex.on('message', (channelName, data, seq) => {
if (channelName === 'BTC_ETH') {
console.log(`order book and trade updates received for currency pair ${channelName}`);
console.log(`data sequence number is ${seq}`);
}
});
There are four types of messages:
- "orderBook`
- "orderBookModify`
- "orderBookRemove`
- "newTrade`
Provides a snapshot of the order book for the subscribed currency pair. The message is emitted immediately after subscription to the currency pair is activated.
The data for 'orderBook'
snapshot is provided in the following format:
[
{
"type": "orderBook",
"data": {
"asks": {
"0.06964408": "0.16219637",
"0.06964599": "10.40000000",
"0.06964600": "33.11470000",
"0.06965590": "0.00427159",
},
"bids": {
"0.06963545": "14.03591058",
"0.06960000": "16.53833125",
"0.06957303": "3.46440626",
"0.06957300": "33.11720000",
}
}
}
]
There are two types of order book updates 'orderBookModify'
and 'orderBookRemove'
, provided in the following formats:
[
{
"type": "orderBookModify",
"data": {
"type": "bid",
"rate": "0.06961000",
"amount": "33.11630000"
}
}
]
[
{
"type": "orderBookRemove",
"data": {
"type": "bid",
"rate": "0.06957300",
"amount": "0.00000000"
}
}
]
Updates of type 'orderBookModify'
can be either additions to the order book or changes to existing entries. The value of amount
indicates the new total amount on the books at the given rate — in other words, it replaces any previous value, rather than indicates an adjustment to a previous value.
Each 'message'
event will pass the parameter seq
, indicating the a sequence number. In order to keep your order book consistent, you will need to ensure that messages are applied in the order of their sequence numbers, even if they arrive out of order.
Important: Several order book and trade history updates will often arrive in a single message. Be sure to loop through the entire array, otherwise you will miss some updates.
Trade history updates are provided in the following format:
[
{
"type": "newTrade",
"data": {
"tradeID": "34816326",
"type": "sell",
"rate": "0.07006406",
"amount": "0.14341700",
"total": "0.01004838",
"date": "2017-10-06T22:37:52.000Z"
}
}
]
Important: Several order book and trade history updates will often arrive in a single message. Be sure to loop through the entire array, otherwise you will miss some updates.
Provides 24 hour exchange volume updates.
Example:
let poloniex = new Poloniex();
poloniex.subscribe('volume');
poloniex.on('message', (channelName, data) => {
if (channelName === 'volume') {
console.log(data);
}
});
poloniex.openWebSocket();
The updates will be in following format:
[
{
"serverTime": "2017-10-04 12:55",
"usersOnline": 18438,
"volume": {
"BTC": "13014.809",
"ETH": "3725.101",
"XMR": "354.380",
"USDT": "24511953.302"
}
}
]
When no messages have been sent out for one second, the server will send a heartbeat message as follows. Absence of heartbeats indicates a protocol or networking issue and the client application is expected to close the socket and try again.
Provides real-time updates of trade and balance changes on your account. It is an authenticated WebSocket channel, so it requires the API key and secret to be provided in the constructor.
Example:
let poloniex = new Poloniex('your_key', 'your_secret');
poloniex.openWebSocket();
poloniex.subscribe('accountNotifications');
poloniex.on('message', (channelName, data) => {
if (channelName === 'accountNotifications') {
console.log(`account notifications update: ${JSON.stringify(data)}`);
}
});
There are five types of messages:
subscriptionSucceeded
balanceUpdate
newLimitOrder
orderUpdate
newTrade
'subscriptionSucceeded'
is an acknowledgement of the subscription, the first message received after a successful subscription.
Subsequent messages represent updates to your account. In general, a message consists of a combination of updates of different types.
Important: Several updates will often arrive in a single message. Be sure to loop through the entire array, otherwise you will miss some updates.
This is an acknowledgement of the subscription, the first message received after a successful subscription.
The data for 'subscriptionSucceeded'
is provided in the following format:
"subscriptionSucceeded"
'balanceUpdate'
message represents an available balance update, provided in the following format:
[
{
"type": "balanceUpdate",
"data": {
"currency": "BTC",
"wallet": "exchange",
"amount": "0.00130128"
}
}
]
wallet
can be 'exchange'
, 'margin'
, or 'lending'
.
'newLimitOrder'
message represents an available balance update, provided in the following format:
[
{
"type": "newLimitOrder",
"data": {
"currencyPair": "BTC_ETH",
"orderNumber": 519591595441,
"type": "buy",
"rate": "0.01017136",
"amount": "0.12793656",
"date": "2018-11-19 14:50:16"
}
}
]
type
can either be 'sell'
or 'buy'
.
'orderUpdate'
message represents an order update, provided in the following format:
[
{
"type": "orderUpdate",
"data": {
"orderNumber": 519591595441,
"amount": "0.00000000"
}
},
]
'newTrade'
message represents a trade notification, provided in the following format:
[
{
"type": "newTrade",
"data": {
"tradeID": 45376136,
"rate": "0.03029793",
"amount": "0.03100000",
"feeMultiplier": "0.00200000",
"fundingType": 0,
"orderNumber": 519616686325
}
]
Unsubscribes a previously established channel subscription. Once unsubscribed there will be no more channel updates received.
Example:
let poloniex = new Poloniex();
poloniex.openWebSocket();
poloniex.subscribe('BTC_ETH');
poloniex.on('message', (channelName, data, seq) => {
if (channelName === 'BTC_ETH') {
console.log(`order book and trade updates received for currency pair ${channelName}`);
console.log(`data sequence number is ${seq}`);
}
});
poloniex.unsubscribe('BTC_ETH');
Closes WebSocket connection previously opened.
Event 'close'
is emitted when connection closes.
Example:
let poloniex = new Poloniex();
poloniex.on('open', () => {
poloniex.closeWebSocket();
});
poloniex.on('close', (reason, code) => {
console.log(`WebSocket connection has been closed`);
});
poloniex.openWebSocket();
This project exists thanks to all the people who contribute.