The Coinbase API for Node.js
The official Node.js library for the Coinbase API.
npm install coinbase
coinbase package name used to refer to the unofficial coinbase library maintained by Matt Walters. Matt graciously allowed us to use the name for this package instead. You can still find that package on Github. Thanks, Matt.
The first thing you'll need to do is sign up for coinbase.
If you're writing code for your own Coinbase account, enable an API key. Next, create a
Client object for interacting with the API:
var Client = require'coinbase'Client;var client = 'apiKey': mykey 'apiSecret': mysecret;
If you're writing code that will act on behalf of another user, start by creating a new OAuth 2 application. You will need to do some work to obtain OAuth credentials for your users; while outside the scope of this document, please refer to our OAuth 2 tutorial and documentation. Once you have these credentials, create a client:
var Client = require'coinbase'Client;var client ='apiKey' : mykey'apiSecret' : mysecret'accessToken' : accessToken'refreshToken' : refreshToken;
coinbase.model.Transaction.complete maps to the complete bitcoin request API endpoint. The comments of each method in the code references the endpoint it implements. Each API method returns an
object representing the JSON response from the API.
Listing available accounts
var coinbase = require'coinbase';var client = 'apiKey': mykey 'apiSecret': mysecret;clientgetAccountsaccountsforEachconsole.log'my bal: ' + acctbalanceamount + ' for ' + acctname;;;
Get Balance from an Account Id
var Account = require'coinbase'modelAccount;var myBtcAcct = client 'id': '<SOME_ACCOUNT_ID>';myBtcAcctgetBalanceconsole.log'bal: ' + balamount + ' currency: ' + balcurrency;;
var args ="qty": "12";accountsellargsconsole.log'my xfer id is: ' + xferid;;
var args ="to": "firstname.lastname@example.org""amount": "1.234""notes": "Sample transaction for you";accountsendMoneyargsconsole.log'my txn id is: ' + txnid;;
var args ="from": "email@example.com""amount": "1.234""notes": "Sample transaction for you";accountrequestMoneyargsconsole.log'my txn id is: ' + txnid;;
Listing current transactions
accountgetTransactionsnull nulltxnsforEachconsole.log'my txn status: ' + txnstatus;;;
Checking bitcoin prices
clientgetBuyPrice'qty': 100 'currency': 'USD'console.log'total amount: ' + objtotalamount;;
Errors are thrown for invalid arguments but are otherwise returned as the first argument to callback functions.
type properties so that you can route the error to the
right handler code. The possible types are:
|AuthenticationError||returned if there was an authentication error|
|InvalidAccessToken||returned when the current access token is no longer valid|
|ExpiredAccessToken||returned when the current access token is expired|
|TokenRefreshError||returned when there is a failure refreshing the access token|
|TwoFactorTokenRequired||returned when a user's Two Factor Auth token needs to be included in the request|
|APIError||returned for errors related to interacting with the Coinbase API server|
acctgetBalanceswitch errtypecase 'ExpiredAccessToken':// use the client.refresh API to get a new access_tokenbreak;case 'InvalidAccessToken'://handle errorbreak;case 'AuthenticationError'://handle errorbreak;;
Errors are always of the type
Error and can print a trace:
acctgetBalanceif err console.log'Where did this error come from?\n' + errstack;;
Errors that are related to an http request will have a
response field with the entire http response:
acctgetBalanceif err console.log'http error code: ' + errresponsestatusCode;;
Any and all contributions are welcome! The process is simple:
You can also run the tests against various node environments using the Dockerfile.example file.
cp Dockerfile.example Dockerfile
[sudo] docker build -t coinbase-node .
[sudo] docker run -it coinbase-node
You can generate html documentation by running the command:
npm install && npm run docs
Open generated html files in
docs/ dir with a browser.