UNPKG

11.7 kBMarkdownView Raw
1Node Bittrex API
2=========
3
4Node Bittrex API is an asynchronous node.js library for the Bittrex API - https://bittrex.com/.
5The Bittrex API data can be received either as a GET request or via Websockets API (the Stream option will no longer be maintained and will be removed in further releases - please switch to Websockets if you want to use real Streams).
6
7Documentation to the Bittrex API: https://bittrex.com/Home/Api
8
9This Library was created by [Adrian Soluch (@n0mad01)](https://github.com/n0mad01/) [soluch.us](http://soluch.us) and is licensed under the [MIT license](https://github.com/n0mad01/node.bittrex.api/blob/master/LICENSE).
10
11Contributors
12----
13Thanks go to the people who have contributed code to this Library.
14
15* [dparlevliet](https://github.com/dparlevliet) Special kudos - thanks to him i was able to add the Websocket API, also did he added the error object/handling param and the getcandles method for the Bittrex API V2
16* [samuelhei](https://github.com/samuelhei) Special kudos - thanks to him all missing calls are complemented as also structural improvements have been made.
17* [192-sean](https://github.com/192-sean)
18* [caffeinewriter](https://github.com/caffeinewriter)
19* [apense](https://github.com/apense)
20
21
22Quick start
23----
24```sh
25$ npm install node.bittrex.api
26```
27
28```javascript
29var bittrex = require('node.bittrex.api');
30bittrex.options({
31 'apikey' : API_KEY,
32 'apisecret' : API_SECRET,
33});
34bittrex.getmarketsummaries( function( data, err ) {
35 if (err) {
36 return console.error(err);
37 }
38 for( var i in data.result ) {
39 bittrex.getticker( { market : data.result[i].MarketName }, function( ticker ) {
40 console.log( ticker );
41 });
42 }
43});
44```
45
46
47Advanced start
48----
49
50fetch the project via git:
51```sh
52$ git clone https://github.com/n0mad01/node.bittrex.api.git
53```
54
55then meet the package dependencies:
56```sh
57$ cd node-bittrex-api/
58$ npm install
59```
60
61include node.bittrex.api.js into your project:
62```javascript
63var bittrex = require('./node.bittrex.api.js');
64```
65
66##### configure
67```javascript
68bittrex.options({
69 'apikey' : API_KEY,
70 'apisecret' : API_SECRET,
71 'stream' : true, // will be removed from future versions
72 'verbose' : true,
73 'cleartext' : false
74});
75```
76
77By default the returned data is an object, in order to get clear text you have to add the option **cleartext** (streams will always return text):
78```javascript
79'cleartext' : true
80```
81
82The baseUrl itself can also be set via options
83```javascript
84'baseUrl' : 'https://bittrex.com/api/v1',
85'baseUrlv2' : 'https://bittrex.com/Api/v2.0',
86```
87
88Change the callbacks arguments sequence
89```javascript
90'inverse_callback_arguments' : true,
91```
92This simply changes the sequence in which the arguments are passed, instead of e.g.:
93```javascript
94getmarkethistory({market : 'USDT-BTC'}, function(data, error) {});
95```
96you'll get the reversed order:
97```javascript
98getmarkethistory({market : 'USDT-BTC'}, function(error, data) {});
99```
100
101Websockets
102--
103following methods are implemented:
104> websockets.listen, websockets.subscribe
105
106listen example
107```javascript
108var websocketsclient = bittrex.websockets.listen( function( data ) {
109 if (data.M === 'updateSummaryState') {
110 data.A.forEach(function(data_for) {
111 data_for.Deltas.forEach(function(marketsDelta) {
112 console.log('Ticker Update for '+ marketsDelta.MarketName, marketsDelta);
113 });
114 });
115 }
116});
117```
118
119subscribe example
120```javascript
121var websocketsclient = bittrex.websockets.subscribe(['BTC-ETH','BTC-SC','BTC-ZEN'], function(data) {
122 if (data.M === 'updateExchangeState') {
123 data.A.forEach(function(data_for) {
124 console.log('Market Update for '+ data_for.MarketName, data_for);
125 });
126 }
127});
128```
129
130simple client & redefine serviceHandlers example
131```javascript
132var websocketsclient = bittrex.websockets.client();
133
134websocketsclient.serviceHandlers.reconnecting = function (message) {
135 return true; // set to true stops reconnect/retrying
136}
137
138websocketsclient.serviceHandlers.messageReceived = function (message) {
139 console.log(message); // the messages received must be parsed as json first e.g. via jsonic(message.utf8Data)
140}
141```
142
143all possible serviceHandlers
144```javascript
145bound: function() { console.log("Websocket bound"); },
146connectFailed: function(error) { console.log("Websocket connectFailed: ", error); },
147connected: function(connection) { console.log("Websocket connected"); },
148disconnected: function() { console.log("Websocket disconnected"); },
149onerror: function (error) { console.log("Websocket onerror: ", error); },
150messageReceived: function (message) { console.log("Websocket messageReceived: ", message); return false; },
151bindingError: function (error) { console.log("Websocket bindingError: ", error); },
152connectionLost: function (error) { console.log("Connection Lost: ", error); },
153reconnecting: function (retry { inital: true/false, count: 0} ) {
154 console.log("Websocket Retrying: ", retry);
155 //return retry.count >= 3; // cancel retry true
156 return true;
157}
158```
159
160
161Streams - please notice that streams will be removed from future versions
162--
163To activate Streaming simply add to your options:
164```javascript
165'stream' : true
166```
167
168
169Examples
170--
171After configuration you can use the object right away:
172example #1
173```javascript
174bittrex.getmarketsummaries( function( data, err ) {
175 if (err) {
176 return console.error(err);
177 }
178 for( var i in data.result ) {
179 bittrex.getticker( { market : data.result[i].MarketName }, function( ticker ) {
180 console.log( ticker );
181 });
182 }
183});
184```
185
186example #2
187```javascript
188bittrex.getbalance({ currency : 'BTC' }, function( data, err ) {
189 if (err) {
190 return console.error(err);
191 }
192 console.log( data );
193});
194```
195
196
197Libraries
198--
199
200Websockets depends on the following npm packages:
201- signalR websockets client https://www.npmjs.com/package/signalrjs
202- jsonic JSON parser https://www.npmjs.com/package/jsonic
203
204Streaming depends on the following npm packages (will be removed in future versions):
205- JSONStream https://www.npmjs.org/package/JSONStream
206- event-stream https://www.npmjs.org/package/event-stream
207
208Other libraries utilized:
209- request https://www.npmjs.org/package/request
210
211For HmacSHA512 this package is using a part of Googles Crypto.js (the node crypt package could not provide any appropriate result).
212- http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha512.js
213
214
215Error examples
216---
217
218Example of request/domain based errors (not Bittrex API error)
219```javascript
220var url = 'http://fake.bittrex.com/api/v1.1/public/getticker?market=USDT-BTCXXX';
221bittrex.sendCustomRequest( url, function( data, err ) {
222 if (err) {
223 /**
224 {
225 success: false,
226 message: 'URL request error',
227 error:
228 { Error: getaddrinfo ENOTFOUND fake.bittrex.com fake.bittrex.com:80
229 at errnoException (dns.js:28:10)
230 at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
231 code: 'ENOTFOUND',
232 errno: 'ENOTFOUND',
233 syscall: 'getaddrinfo',
234 hostname: 'fake.bittrex.com',
235 host: 'fake.bittrex.com',
236 port: 80 },
237 result: undefined
238 }
239 */
240 return console.error(err);
241 }
242 console.log(data);
243});
244```
245
246Example of request/url based errors (not Bittrex API error)
247```javascript
248var url = 'http://bittrex.com/api/v1.1/public/getfakeendpoint';
249bittrex.sendCustomRequest( url, function( data, err ) {
250 if (err) {
251 /**
252 {
253 success: false,
254 message: 'URL request error',
255 error: undefined,
256 result: {
257 statusCode: 404,
258 statusMessage: 'Not Found',
259 body: '<!DOCTYPE html>\r\n<html > ...'
260 }
261 }
262 */
263 return console.error(err);
264 }
265 console.log(data);
266});
267```
268
269Example of Bittrex API error
270```javascript
271bittrex.getcandles({
272 marketName: 'USDT-BTC',
273 tickInterval: 300,
274 _: ((new Date()).getTime()/1000)-(300*5) // start timestamp
275}, function(data, err) {
276 if (err) {
277 /**
278 {
279 success: false,
280 message: 'INVALID_TICK_INTERVAL',
281 result: null
282 }
283 */
284 return console.error(err);
285 }
286 console.log(data);
287});
288```
289
290
291Methods
292----
293
294Optional parameters may have to be looked up at https://bittrex.com/Home/Api.
295
296> It may happen that some Bittrex API methods are missing, also they could have been forgotten in the documentation. In this case, if this strikes you, feel free to open a issue or send me a pull request.
297
298> Also: the method **sendCustomRequest** enables completely custom requests, regardless the specific API methods.
299
300##### sendCustomRequest
301- url String
302- callback Function
303- credentials Boolean optional whether the credentials should be applied to the request/stream or not, default is set to false.
304
305example #1
306```javascript
307var url = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC';
308bittrex.sendCustomRequest( url, function( data, err ) {
309 console.log( data );
310});
311```
312
313example #2 (credentials applied to request/stream)
314```javascript
315bittrex.sendCustomRequest( 'https://bittrex.com/api/v1.1/account/getbalances?currency=BTC', function( data, err ) {
316 console.log( data );
317}, true );
318
319will result in (the Header is being set too):
320https://bittrex.com/api/v1.1/account/getbalances?currency=BTC&apikey=API_KEY&nonce=4456490600
321```
322
323##### getticker
324```javascript
325bittrex.getticker( { market : 'BTC-LTC' }, function( data, err ) {
326 console.log( data );
327});
328```
329
330##### getbalances
331```javascript
332bittrex.getbalances( function( data, err ) {
333 console.log( data );
334});
335```
336
337##### getmarkethistory
338```javascript
339bittrex.getmarkethistory({ market : 'BTC-LTC' }, function( data, err ) {
340 console.log( data );
341});
342```
343
344##### getcandles (v2 method)
345```javascript
346bittrex.getmarkethistory({
347 marketName: 'USDT-BTC',
348 tickInterval: 'fiveMin', // intervals are keywords
349 _: ((new Date()).getTime()/1000)-(300*5) // start timestamp
350}, function( data, err ) {
351 console.log( data );
352});
353```
354
355##### getmarketsummaries
356```javascript
357bittrex.getmarketsummaries( function( data, err ) {
358 console.log( data );
359});
360```
361
362##### getmarketsummary
363```javascript
364bittrex.getmarketsummary( { market : 'BTC-LTC'}, function( data, err ) {
365 console.log( data );
366});
367```
368
369##### getorderbook
370```javascript
371bittrex.getorderbook({ market : 'BTC-LTC', depth : 10, type : 'both' }, function( data, err ) {
372 console.log( data );
373});
374```
375
376##### getwithdrawalhistory
377```javascript
378bittrex.getwithdrawalhistory({ currency : 'BTC' }, function( data, err ) {
379 console.log( data );
380});
381```
382
383##### getdepositaddress
384```javascript
385bittrex.getdepositaddress({ currency : 'BTC' }, function( data, err ) {
386 console.log( data );
387});
388```
389
390##### getdeposithistory
391```javascript
392bittrex.getdeposithistory({ currency : 'BTC' }, function( data, err ) {
393 console.log( data );
394});
395```
396
397##### getbalance
398```javascript
399bittrex.getbalance({ currency : 'BTC' }, function( data, err ) {
400 console.log( data );
401});
402```
403
404##### withdraw
405```javascript
406bittrex.withdraw({ currency : 'BTC', quantity : '1.5112', address : 'THE_ADDRESS' }, function( data, err ) {
407 console.log( data );
408});
409```
410
411
412Testing
413----
414
415Installing test gear
416```bash
417npm install --only=dev
418```
419
420Running all tests
421```bash
422npm test tests
423```
424
425or individually
426```bash
427npm test tests/public.js
428npm test tests/private.js
429```
430
431##### Testing private methods
432
433Testing private method endpoints requires an api key/secret which should be
434installed in to ``tests/config.json`` - you will find an example file in
435``tests/config_example.json``.
436
437```bash
438cp tests/tests_example.json tests/config.json
439vim tests/config.json
440```
441
442
443Donations welcome!
444---
445
446BTC
447> 17gtixgt4Q8hZcSictQwj5WUdgFjegCt36