Skip to content

MarcSallent/gdax-node

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GDAX CircleCI

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

# From NPM
npm install gdax
# From Github
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();

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);
// Get the order book at the default level of detail.
publicClient.getProductOrderBook(callback);
// Get the order book at a specific level of detail.
publicClient.getProductOrderBook({'level': 3}, callback);
publicClient.getProductTicker(callback);
publicClient.getProductTrades(callback);
// To make paginated requests, include page parameters
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);
// tradesTo can also be a function
var trades publicClient.getProductTradeStream(8408000, function(trade) {
    return Date.parse(trade.time) >= 1463068e6
})
publicClient.getProductHistoricRates(callback);
// To include extra parameters:
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.

var Gdax = require('gdax');
var authedClient = new Gdax.AuthenticatedClient(
  key, b64secret, passphrase);

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);
// For pagination, you can include extra page arguments
authedClient.getAccountHistory(accountID, {'before': 3000}, callback);
var accountID = '7d0f7d8e-dd34-4d9c-a846-06f431c381ba';
authedClient.getAccountHolds(accountID, callback);
// For pagination, you can include extra page arguments
authedClient.getAccountHolds(accountID, {'before': 3000}, callback);
// Buy 1 BTC @ 100 USD
var buyParams = {
  'price': '100.00', // USD
  'size': '1',  // BTC
  'product_id': 'BTC-USD',
};
authedClient.buy(buyParams, callback);

// Sell 1 BTC @ 110 USD
var sellParams = {
  'price': '110.00', // USD
  'size': '1', // BTC
  'product_id': 'BTC-USD',
};
authedClient.sell(sellParams, callback);
var orderID = 'd50ec984-77a8-460a-b958-66f114b0de9b';
authedClient.cancelOrder(orderID, callback);
authedClient.cancelOrders(callback);
// `cancelOrders` may require you to make the request multiple times until
// all the orders are deleted.

// `cancelAllOrders` will handle making these requests for you asyncronously.
// Also you can add a product_id param to only delete orders of that product

// The response will be the response of the last call and the data will
// have the order ID's of all your deleted orders
authedClient.cancelAllOrders({product_id: 'BTC-USD'}, callback);
authedClient.getOrders(callback);
// For pagination, you can include extra page arguments
authedClient.getOrders({'after': 3000}, callback);
var orderID = 'd50ec984-77a8-460a-b958-66f114b0de9b';
authedClient.getOrder(orderID, callback);
authedClient.getFills(callback);
// For pagination, you can include extra page arguments
authedClient.getFills({'before': 3000}, callback);
// Deposit to your Exchange USD account from your Coinbase USD account.
var depositParamsUSD = {
  'amount': '100.00', // USD,
  'coinbase_account_id': '60680c98bfe96c2601f27e9c', // USD Coinbase Account ID
};
authedClient.deposit(depositParamsUSD, callback);
// Withdraw from your Exchange USD account to your Coinbase USD account.
var withdrawParamsUSD = {
  'amount': '100.00', // USD,
  'coinbase_account_id': '60680c98bfe96c2601f27e9c', // USD Coinbase Account ID
};
authedClient.withdraw(withdrawParamsUSD, callback);

// Deposit to your Exchange BTC account from your Coinbase BTC account.
var depositParamsBTC = {
  'amount': '2.0', // BTC,
  'coinbase_account_id': '536a541fa9393bb3c7000023', // BTC Coinbase Account ID
};
authedClient.deposit(depositParamsBTC, callback);
// Withdraw from your Exchange BTC account to your Coinbase BTC account.
var withdrawParamsBTC = {
  'amount': '2.0', // BTC,
  'coinbase_account_id': '536a541fa9393bb3c7000023', // BTC Coinbase Account ID
};
authedClient.withdraw(withdrawParamsBTC, callback);
// Get your 30 day trailing volumes
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();
websocket.on('message', function(data) { console.log(data); });

The following events can be emitted from the WebsocketClient:

  • open
  • message
  • close

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();
console.log(orderbookSync.book.state());

Testing

npm run test

# test for known vulnerabilities in packages
npm install -g nsp
nsp check --output summary

About

The official Node.js library for the GDAX API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%