1 | Node Bittrex API
|
2 | =========
|
3 |
|
4 | Node Bittrex API is an asynchronous node.js library for the Bittrex API - https://bittrex.com/.
|
5 | The 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 |
|
7 | Documentation to the Bittrex API: https://bittrex.com/Home/Api
|
8 |
|
9 | This 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 |
|
11 | Contributors
|
12 | ----
|
13 | Thanks 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 |
|
22 | Quick start
|
23 | ----
|
24 | ```sh
|
25 | $ npm install node.bittrex.api
|
26 | ```
|
27 |
|
28 | ```javascript
|
29 | var bittrex = require('node.bittrex.api');
|
30 | bittrex.options({
|
31 | 'apikey' : API_KEY,
|
32 | 'apisecret' : API_SECRET,
|
33 | });
|
34 | bittrex.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 |
|
47 | Advanced start
|
48 | ----
|
49 |
|
50 | fetch the project via git:
|
51 | ```sh
|
52 | $ git clone https://github.com/n0mad01/node.bittrex.api.git
|
53 | ```
|
54 |
|
55 | then meet the package dependencies:
|
56 | ```sh
|
57 | $ cd node-bittrex-api/
|
58 | $ npm install
|
59 | ```
|
60 |
|
61 | include node.bittrex.api.js into your project:
|
62 | ```javascript
|
63 | var bittrex = require('./node.bittrex.api.js');
|
64 | ```
|
65 |
|
66 | ##### configure
|
67 | ```javascript
|
68 | bittrex.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 |
|
77 | By 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 |
|
82 | The 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 |
|
88 | Change the callbacks arguments sequence
|
89 | ```javascript
|
90 | 'inverse_callback_arguments' : true,
|
91 | ```
|
92 | This simply changes the sequence in which the arguments are passed, instead of e.g.:
|
93 | ```javascript
|
94 | getmarkethistory({market : 'USDT-BTC'}, function(data, error) {});
|
95 | ```
|
96 | you'll get the reversed order:
|
97 | ```javascript
|
98 | getmarkethistory({market : 'USDT-BTC'}, function(error, data) {});
|
99 | ```
|
100 |
|
101 | Websockets
|
102 | --
|
103 | following methods are implemented:
|
104 | > websockets.listen, websockets.subscribe
|
105 |
|
106 | listen example
|
107 | ```javascript
|
108 | var 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 |
|
119 | subscribe example
|
120 | ```javascript
|
121 | var 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 |
|
130 | simple client & redefine serviceHandlers example
|
131 | ```javascript
|
132 | var websocketsclient = bittrex.websockets.client();
|
133 |
|
134 | websocketsclient.serviceHandlers.reconnecting = function (message) {
|
135 | return true; // set to true stops reconnect/retrying
|
136 | }
|
137 |
|
138 | websocketsclient.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 |
|
143 | all possible serviceHandlers
|
144 | ```javascript
|
145 | bound: function() { console.log("Websocket bound"); },
|
146 | connectFailed: function(error) { console.log("Websocket connectFailed: ", error); },
|
147 | connected: function(connection) { console.log("Websocket connected"); },
|
148 | disconnected: function() { console.log("Websocket disconnected"); },
|
149 | onerror: function (error) { console.log("Websocket onerror: ", error); },
|
150 | messageReceived: function (message) { console.log("Websocket messageReceived: ", message); return false; },
|
151 | bindingError: function (error) { console.log("Websocket bindingError: ", error); },
|
152 | connectionLost: function (error) { console.log("Connection Lost: ", error); },
|
153 | reconnecting: 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 |
|
161 | Streams - please notice that streams will be removed from future versions
|
162 | --
|
163 | To activate Streaming simply add to your options:
|
164 | ```javascript
|
165 | 'stream' : true
|
166 | ```
|
167 |
|
168 |
|
169 | Examples
|
170 | --
|
171 | After configuration you can use the object right away:
|
172 | example #1
|
173 | ```javascript
|
174 | bittrex.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 |
|
186 | example #2
|
187 | ```javascript
|
188 | bittrex.getbalance({ currency : 'BTC' }, function( data, err ) {
|
189 | if (err) {
|
190 | return console.error(err);
|
191 | }
|
192 | console.log( data );
|
193 | });
|
194 | ```
|
195 |
|
196 |
|
197 | Libraries
|
198 | --
|
199 |
|
200 | Websockets 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 |
|
204 | Streaming 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 |
|
208 | Other libraries utilized:
|
209 | - request https://www.npmjs.org/package/request
|
210 |
|
211 | For 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 |
|
215 | Error examples
|
216 | ---
|
217 |
|
218 | Example of request/domain based errors (not Bittrex API error)
|
219 | ```javascript
|
220 | var url = 'http://fake.bittrex.com/api/v1.1/public/getticker?market=USDT-BTCXXX';
|
221 | bittrex.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 |
|
246 | Example of request/url based errors (not Bittrex API error)
|
247 | ```javascript
|
248 | var url = 'http://bittrex.com/api/v1.1/public/getfakeendpoint';
|
249 | bittrex.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 |
|
269 | Example of Bittrex API error
|
270 | ```javascript
|
271 | bittrex.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 |
|
291 | Methods
|
292 | ----
|
293 |
|
294 | Optional 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 |
|
305 | example #1
|
306 | ```javascript
|
307 | var url = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC';
|
308 | bittrex.sendCustomRequest( url, function( data, err ) {
|
309 | console.log( data );
|
310 | });
|
311 | ```
|
312 |
|
313 | example #2 (credentials applied to request/stream)
|
314 | ```javascript
|
315 | bittrex.sendCustomRequest( 'https://bittrex.com/api/v1.1/account/getbalances?currency=BTC', function( data, err ) {
|
316 | console.log( data );
|
317 | }, true );
|
318 |
|
319 | will result in (the Header is being set too):
|
320 | https://bittrex.com/api/v1.1/account/getbalances?currency=BTC&apikey=API_KEY&nonce=4456490600
|
321 | ```
|
322 |
|
323 | ##### getticker
|
324 | ```javascript
|
325 | bittrex.getticker( { market : 'BTC-LTC' }, function( data, err ) {
|
326 | console.log( data );
|
327 | });
|
328 | ```
|
329 |
|
330 | ##### getbalances
|
331 | ```javascript
|
332 | bittrex.getbalances( function( data, err ) {
|
333 | console.log( data );
|
334 | });
|
335 | ```
|
336 |
|
337 | ##### getmarkethistory
|
338 | ```javascript
|
339 | bittrex.getmarkethistory({ market : 'BTC-LTC' }, function( data, err ) {
|
340 | console.log( data );
|
341 | });
|
342 | ```
|
343 |
|
344 | ##### getcandles (v2 method)
|
345 | ```javascript
|
346 | bittrex.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
|
357 | bittrex.getmarketsummaries( function( data, err ) {
|
358 | console.log( data );
|
359 | });
|
360 | ```
|
361 |
|
362 | ##### getmarketsummary
|
363 | ```javascript
|
364 | bittrex.getmarketsummary( { market : 'BTC-LTC'}, function( data, err ) {
|
365 | console.log( data );
|
366 | });
|
367 | ```
|
368 |
|
369 | ##### getorderbook
|
370 | ```javascript
|
371 | bittrex.getorderbook({ market : 'BTC-LTC', depth : 10, type : 'both' }, function( data, err ) {
|
372 | console.log( data );
|
373 | });
|
374 | ```
|
375 |
|
376 | ##### getwithdrawalhistory
|
377 | ```javascript
|
378 | bittrex.getwithdrawalhistory({ currency : 'BTC' }, function( data, err ) {
|
379 | console.log( data );
|
380 | });
|
381 | ```
|
382 |
|
383 | ##### getdepositaddress
|
384 | ```javascript
|
385 | bittrex.getdepositaddress({ currency : 'BTC' }, function( data, err ) {
|
386 | console.log( data );
|
387 | });
|
388 | ```
|
389 |
|
390 | ##### getdeposithistory
|
391 | ```javascript
|
392 | bittrex.getdeposithistory({ currency : 'BTC' }, function( data, err ) {
|
393 | console.log( data );
|
394 | });
|
395 | ```
|
396 |
|
397 | ##### getbalance
|
398 | ```javascript
|
399 | bittrex.getbalance({ currency : 'BTC' }, function( data, err ) {
|
400 | console.log( data );
|
401 | });
|
402 | ```
|
403 |
|
404 | ##### withdraw
|
405 | ```javascript
|
406 | bittrex.withdraw({ currency : 'BTC', quantity : '1.5112', address : 'THE_ADDRESS' }, function( data, err ) {
|
407 | console.log( data );
|
408 | });
|
409 | ```
|
410 |
|
411 |
|
412 | Testing
|
413 | ----
|
414 |
|
415 | Installing test gear
|
416 | ```bash
|
417 | npm install --only=dev
|
418 | ```
|
419 |
|
420 | Running all tests
|
421 | ```bash
|
422 | npm test tests
|
423 | ```
|
424 |
|
425 | or individually
|
426 | ```bash
|
427 | npm test tests/public.js
|
428 | npm test tests/private.js
|
429 | ```
|
430 |
|
431 | ##### Testing private methods
|
432 |
|
433 | Testing private method endpoints requires an api key/secret which should be
|
434 | installed in to ``tests/config.json`` - you will find an example file in
|
435 | ``tests/config_example.json``.
|
436 |
|
437 | ```bash
|
438 | cp tests/tests_example.json tests/config.json
|
439 | vim tests/config.json
|
440 | ```
|
441 |
|
442 |
|
443 | Donations welcome!
|
444 | ---
|
445 |
|
446 | BTC
|
447 | > 17gtixgt4Q8hZcSictQwj5WUdgFjegCt36
|