UNPKG

6.13 kBMarkdownView Raw
1BTC 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
6This library is a node.js wrapper for the private and public methods exposed by the [BTC Markets API](https://github.com/BTCMarkets/API).
7You will need to have a registered account with [BTC Markets](https://btcmarkets.net) and generated API keys to access the private methods.
8
9Please 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
16This 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
18Other 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
29The first parameter to each API function is a callback function which is passed error and data objects.
30
31The error object is an instance of [VError](https://github.com/davepacheco/node-verror) which is an extension of the standard Error object.
32The 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
38Do 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
40If you can't avoid committing API keys into your repo then use something like [git-crypt](https://github.com/AGWA/git-crypt).
41
42Most 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
48And 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
51If 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
56The following is from [examples.js](./examples.js)
57```javascript
58//const BTCMarkets = require('btc-markets').default; // if you are using JavaScript
59import 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.
66const key = process.argv[2] || 'your-api-key';
67const secret = process.argv[3] || 'your-api-secret';
68
69const client = new BTCMarkets(key, secret);
70
71// get latest prices
72const tick = await client.getTick("BTC", "AUD");
73
74// get order book
75const orderBook = await client.getOrderBook("BTC", "AUD");
76
77// get market trades since 728992317
78const trades = await client.getTrades("BTC", "AUD", 728992317);
79
80// limit buy order for of 0.001 ETH at 50 AUD
81const limitOrder = await client.createOrder("ETH", "AUD", 50 * BTCMarkets.numberConverter, 0.001 * BTCMarkets.numberConverter, 'Bid', 'Limit', "10001");
82
83//market sell for 0.001 BTC
84const 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
87const stopLossOrder = await client.createOrder("ETH", "AUD", 50 * BTCMarkets.numberConverter, 0.001 * BTCMarkets.numberConverter, 'Bid', 'Stop Limit', "10002", 40 * BTCMarkets.numberConverter);
88console.log(`Stop loss order: ${JSON.stringify(stopLossOrder)}`);
89
90// cancel two limit orders with id's 1132477709 and 1133590603
91const cancelledOrders = await client.cancelOrders([1132477709, 1133590603]);
92
93const accountBalances = await client.getAccountBalances();
94
95// get trading fee for a trading pair
96const tradingFee = await client.getTradingFee("BTC", "AUD");
97
98// get order details
99const orderDetails = await client.getOrderDetail([206855175, 23988196]);
100
101// get all trades since the start of time
102const tradeHistory = await client.getTradeHistory("BTC", "AUD", undefined, null);
103
104// get 50 orders since the start of time
105const orderHistory = await client.getOrderHistory("BTC", "AUD", 50, null);
106
107// get my open orders
108const openOrders = await client.getOpenOrders('BTC', 'AUD', 10, null);
109
110// withdrawal 0.05 ETH
111const cryptoWithdrawal = await client.withdrawCrypto(0.05 * BTCMarkets.numberConverter, "0x775053A6125cB51e618Eb132f00E93d6033934AD", "ETH");
112
113// withdrawal history
114const withdrawHistory = await client.withdrawHistory(10, 0, true);
115```
\No newline at end of file