GDAX
The official Node.js library for the GDAX
API (formerly Coinbase Exchange).
Note: this library may be subtly broken or buggy. The code is released under
the MIT License – please take the following message to heart:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Features
- Easy programmatic trading.
- A customizable, websocket-synced Order Book implementation.
- API clients with convenient methods for every API endpoint.
- Abstracted interfaces – don't worry about HMAC signing or JSON formatting,
the library does it for you.
- Semantic versioning.
Installation
npm install gdax
npm install coinbase/gdax-node
Quick Start
The Public API Client
The GDAX API has both public and private endpoints. If you're only
interested in the public endpoints, you should use a PublicClient
.
var Gdax = require('gdax');
var publicClient = new Gdax.PublicClient(productID*, endpoint*);
productID
is OPTIONAL: defaults to 'BTC-USD' if not specified.endpoint
is OPTIONAL: defaults to https://api.gdax.com if not specified.
All API methods are callback based. Your callback should accept three arguments:
var callback = function(err, response, data) {
// your code here.
};
This callback will be passed directly to the underlying request
library's
request
method. err
will be either
null
or an Error
. response
will be a generic HTTP response abstraction
created by the request
library. data
will be the result of JSON-decoding
the server's response, or null
if the response was not parseable. You can
learn about the API responses of each endpoint by reading our
documentation.
Public API Methods
publicClient.getProducts(callback);
publicClient.getProductOrderBook(callback);
publicClient.getProductOrderBook({'level': 3}, callback);
publicClient.getProductTicker(callback);
publicClient.getProductTrades(callback);
publicClient.getProductTrades({'after': 1000}, callback);
getProductTradeStream
Wraps around getProductTrades
, fetches all trades with IDs >= tradesFrom
and <= tradesTo
.
Handles pagination and rate limits.
var trades = publicClient.getProductTradeStream(8408000, 8409000);
var trades publicClient.getProductTradeStream(8408000, function(trade) {
return Date.parse(trade.time) >= 1463068e6
})
publicClient.getProductHistoricRates(callback);
publicClient.getProductHistoricRates({'granularity': 3000}, callback);
publicClient.getProduct24HrStats(callback);
publicClient.getCurrencies(callback);
publicClient.getTime(callback);
The Authenticated API Client
The private exchange API
endpoints require you to
authenticate with an API key. You can create a new API key in your exchange
account's settings. You can also specify the
API uri.
var Gdax = require('gdax');
var apiURI = 'https://api.gdax.com';
var sandboxURI = 'https://api-public.sandbox.gdax.com';
var authedClient = new Gdax.AuthenticatedClient(
key, b64secret, passphrase, apiURI);
Like the PublicClient
, all API methods are callback based. The callback
should be in the same format:
var callback = function(err, response, data) {
// your code here.
};
The AuthenticatedClient
inherits all of the API methods defined by the
PublicClient
, so if you're hitting both public and private API endpoints you
only need to create a single client.
Private API Methods
authedClient.getAccounts(callback);
var accountID = '7d0f7d8e-dd34-4d9c-a846-06f431c381ba';
authedClient.getAccount(accountID, callback);
var accountID = '7d0f7d8e-dd34-4d9c-a846-06f431c381ba';
authedClient.getAccountHistory(accountID, callback);
authedClient.getAccountHistory(accountID, {'before': 3000}, callback);
var accountID = '7d0f7d8e-dd34-4d9c-a846-06f431c381ba';
authedClient.getAccountHolds(accountID, callback);
authedClient.getAccountHolds(accountID, {'before': 3000}, callback);
var buyParams = {
'price': '100.00',
'size': '1',
'product_id': 'BTC-USD',
};
authedClient.buy(buyParams, callback);
var sellParams = {
'price': '110.00',
'size': '1',
'product_id': 'BTC-USD',
};
authedClient.sell(sellParams, callback);
var orderID = 'd50ec984-77a8-460a-b958-66f114b0de9b';
authedClient.cancelOrder(orderID, callback);
authedClient.cancelOrders(callback);
authedClient.cancelAllOrders({product_id: 'BTC-USD'}, callback);
authedClient.getOrders(callback);
authedClient.getOrders({'after': 3000}, callback);
var orderID = 'd50ec984-77a8-460a-b958-66f114b0de9b';
authedClient.getOrder(orderID, callback);
authedClient.getFills(callback);
authedClient.getFills({'before': 3000}, callback);
authedClient.getFundings({}, callback);
var params = {
'amount': '2000.00',
'currency': 'USD'
};
authedClient.repay(params, callback);
var params =
'margin_profile_id': '45fa9e3b-00ba-4631-b907-8a98cbdf21be',
'type': 'deposit',
'currency': 'USD',
'amount': 2
};
authedClient.marginTransfer(params, callback);
var params = {
'repay_only': false
};
authedClient.closePosition(params, callback);
var depositParamsUSD = {
'amount': '100.00',
'coinbase_account_id': '60680c98bfe96c2601f27e9c',
};
authedClient.deposit(depositParamsUSD, callback);
var withdrawParamsUSD = {
'amount': '100.00',
'coinbase_account_id': '60680c98bfe96c2601f27e9c',
};
authedClient.withdraw(withdrawParamsUSD, callback);
var depositParamsBTC = {
'amount': '2.0',
'coinbase_account_id': '536a541fa9393bb3c7000023',
};
authedClient.deposit(depositParamsBTC, callback);
var withdrawParamsBTC = {
'amount': '2.0',
'coinbase_account_id': '536a541fa9393bb3c7000023',
};
authedClient.withdraw(withdrawParamsBTC, callback);
authedClient.getTrailingVolume(callback);
Websocket client
The WebsocketClient
allows you to connect and listen to the
exchange websocket messages.
var Gdax = require('gdax');
var websocket = new Gdax.WebsocketClient(['BTC-USD', 'ETH-USD']);
websocket.on('message', function(data) { console.log(data); });
The following events can be emitted from the WebsocketClient
:
Orderbook
Orderbook
is a data structure that can be used to store a local copy of the orderbook.
var Gdax = require('gdax');
var orderbook = new Gdax.Orderbook();
The orderbook has the following methods:
state(book)
get(orderId)
add(order)
remove(orderId)
match(match)
change(change)
Orderbook Sync
OrderbookSync
creates a local mirror of the orderbook on GDAX using
Orderbook
and WebsocketClient
as described here.
var Gdax = require('gdax');
var orderbookSync = new Gdax.OrderbookSync(['BTC-USD', 'ETH-USD']);
console.log(orderbookSync.books['ETH-USD'].state());
Testing
npm run test
npm install -g nsp
nsp check --output summary