1 | BTC Markets Javascript API Client
|
2 | ===============
|
3 | [![npm version](https://badge.fury.io/js/btc-markets.svg)](https://badge.fury.io/js/btc-markets)
|
4 | [![Known Vulnerabilities](https://snyk.io/test/github/naddison36/btc-markets/badge.svg)](https://snyk.io/test/github/naddison36/btc-markets)
|
5 |
|
6 | This library is a node.js wrapper for the private and public methods exposed by the [BTC Markets API](https://github.com/BTCMarkets/API).
|
7 | You will need to have a registered account with [BTC Markets](https://btcmarkets.net) and generated API keys to access the private methods.
|
8 |
|
9 | Please contact support@btcmarkets.net if you are having trouble opening an account or generating an API key.
|
10 |
|
11 | ### Install
|
12 |
|
13 | `npm install btc-markets`
|
14 |
|
15 | ### Version 1.x
|
16 | This library has bee upgraded to be written in TypeScript and use promises. If you want the old version that used to callbacks, then use [v0.0.10](https://github.com/naddison36/btc-markets/tree/v0.0.10).
|
17 |
|
18 | Other changes are:
|
19 | - Removed the Underscore dependency
|
20 | - Added static numberConverter to convert decimal numbers to BTC Markets API integers
|
21 | - Added withdrawalHistory API (still in preview so not tested)
|
22 |
|
23 | ### Design Principles
|
24 | - **thin** the client is just a simple wrapper to the BTC Markets API. There is no parameter validation as this is delegated to the BTC Market API server. Similarly, there is no data transformation.
|
25 | - **errors** all errors are returned as Error objects which can be used programmatically or for debugging
|
26 | - **no retries** it's up to the calling program to handle retries as it'll vary between programs. For example, error handling timeouts on mutable API calls like addTrade and cancelOrder is not as simple as retying the API call as the operation may have been successful on the exchange but the response back was not.
|
27 |
|
28 | ### Error handling
|
29 | The first parameter to each API function is a callback function which is passed error and data objects.
|
30 |
|
31 | The error object is an instance of [VError](https://github.com/davepacheco/node-verror) which is an extension of the standard Error object.
|
32 | The three main properties are:
|
33 | - **message** a description of the error with all the available information so problems in production can be diagnosed. For example, the url, http request method, parameters, error codes and messages
|
34 | - **name** the HTTP error code or BTC Markets error message so specific errors can be programatically detected.
|
35 | - **cause** the underlying error object. eg the error object from a failed request or json parse. Note there will be no cause error for BTC Markets errors
|
36 |
|
37 | ### Security Warning
|
38 | Do not commit your API keys into public source code repositories. These can be in code, config files or IDE config files used to run tests or processes.
|
39 |
|
40 | If you can't avoid committing API keys into your repo then use something like [git-crypt](https://github.com/AGWA/git-crypt).
|
41 |
|
42 | Most cloud providers now offer solutions for securely storing API keys. For example:
|
43 | * Google [Key Management Service (KMS](https://cloud.google.com/kms/)
|
44 | * AWS [Key Management Service (KMS)](https://aws.amazon.com/kms/)
|
45 | * Azure [Key Vault](https://azure.microsoft.com/en-au/services/key-vault/)
|
46 | * Kubernetes [Secrets](https://kubernetes.io/docs/concepts/configuration/secret/)
|
47 |
|
48 | And while I'm at it, make sure you enable two-factor authentication. Your account is easy to hack without 2FA enabled. You have been warned!
|
49 |
|
50 | ### Donations
|
51 | If you'd like to thank me for this library, you can always donate some of your crypto trading profits to:
|
52 | * BTC 13CPGPRf63nVWFkdnJgmvC4K69YGeR4zNn
|
53 | * ETH 0x775053A6125cB51e618Eb132f00E93d6033934AD
|
54 |
|
55 | ### Examples
|
56 | The following is from [examples.js](./examples.js)
|
57 | ```javascript
|
58 | //const BTCMarkets = require('btc-markets').default; // if you are using JavaScript
|
59 | import BTCMarkets from 'btc-markets'; // if you are using TypeScript or Babel
|
60 |
|
61 | // Either pass your API key and secret as the first and second parameters to examples.js. eg
|
62 | // node examples.js your-api-key your-api-secret
|
63 | //
|
64 | // Or enter them below.
|
65 | // WARNING never commit your API keys into a public repository.
|
66 | const key = process.argv[2] || 'your-api-key';
|
67 | const secret = process.argv[3] || 'your-api-secret';
|
68 |
|
69 | const client = new BTCMarkets(key, secret);
|
70 |
|
71 | // get latest prices
|
72 | const tick = await client.getTick("BTC", "AUD");
|
73 |
|
74 | // get order book
|
75 | const orderBook = await client.getOrderBook("BTC", "AUD");
|
76 |
|
77 | // get market trades since 728992317
|
78 | const trades = await client.getTrades("BTC", "AUD", 728992317);
|
79 |
|
80 | // limit buy order for of 0.001 ETH at 50 AUD
|
81 | const limitOrder = await client.createOrder("ETH", "AUD", 50 * BTCMarkets.numberConverter, 0.001 * BTCMarkets.numberConverter, 'Bid', 'Limit', "10001");
|
82 |
|
83 | //market sell for 0.001 BTC
|
84 | const marketOrder = await client.createOrder("BTC", "AUD", null, 0.001 * BTCMarkets.numberConverter, 'Ask', 'Market', null);
|
85 |
|
86 | // limit buy order for of 0.001 ETH at 50 AUD with 40 AUD stop loss
|
87 | const stopLossOrder = await client.createOrder("ETH", "AUD", 50 * BTCMarkets.numberConverter, 0.001 * BTCMarkets.numberConverter, 'Bid', 'Stop Limit', "10002", 40 * BTCMarkets.numberConverter);
|
88 | console.log(`Stop loss order: ${JSON.stringify(stopLossOrder)}`);
|
89 |
|
90 | // cancel two limit orders with id's 1132477709 and 1133590603
|
91 | const cancelledOrders = await client.cancelOrders([1132477709, 1133590603]);
|
92 |
|
93 | const accountBalances = await client.getAccountBalances();
|
94 |
|
95 | // get trading fee for a trading pair
|
96 | const tradingFee = await client.getTradingFee("BTC", "AUD");
|
97 |
|
98 | // get order details
|
99 | const orderDetails = await client.getOrderDetail([206855175, 23988196]);
|
100 |
|
101 | // get all trades since the start of time
|
102 | const tradeHistory = await client.getTradeHistory("BTC", "AUD", undefined, null);
|
103 |
|
104 | // get 50 orders since the start of time
|
105 | const orderHistory = await client.getOrderHistory("BTC", "AUD", 50, null);
|
106 |
|
107 | // get my open orders
|
108 | const openOrders = await client.getOpenOrders('BTC', 'AUD', 10, null);
|
109 |
|
110 | // withdrawal 0.05 ETH
|
111 | const cryptoWithdrawal = await client.withdrawCrypto(0.05 * BTCMarkets.numberConverter, "0x775053A6125cB51e618Eb132f00E93d6033934AD", "ETH");
|
112 |
|
113 | // withdrawal history
|
114 | const withdrawHistory = await client.withdrawHistory(10, 0, true);
|
115 | ``` |
\ | No newline at end of file |