An OAuth2 compliant NodeJS Coinbase API library
- Implements all the v1 API methods
- Every API method allows callbacks or returns promises
- Automatically refreshes expired tokens (and fires an event)
npm install ncoinbase
var Coinbase = require('ncoinbase').Coinbase,
api = new Coinbase({
"id": "api_id",
"secret": "api_secret",
"redirect_uri": "http://localhost:4000/callback",
"scope": "balance request"
});
// Assuming we pulled the users credentials
// from the database and assigned them to 'user'
credentials = new Credentials(user);
consumer = api.createConsumer(credentials);
req.coinbase.account_balance(function (err, resp) {
console.log("My Account balance is: " + resp.amount );
});
req.coinbase.account_balance()
.then(function (resp) {
console.log("Resp", resp);
res.send(resp);
})
.catch(function (err) {
console.log("Err", err);
res.send("API Request failed: " + err.message);
});
Expired tokens will be automatically refreshed when you attempt an API call. You can, and should, listen for this for the 'updatedCredentials' event and save the new credentials.
consumer.on('updatedCredentials', function (credentials) {
user.credentials = credentials;
user.save();
});
// Let's assume the users credentials are expired
// and we need to use the refresh token.
consumer.account_balance(function (err, resp) {
// This will still work, but before the 'account_balance'
// request is made, the credentials will be refreshed,
// and the 'updatedCredentials' event will be fired.
console.log("I have " + resp.amount + " many bitcoins!!!");
});
git clone https://github.com/mdp/NCoinbase.git
cd NCoinbase
npm install
node example/server.js
Now visit http://localhost:4000 to authenticate
After authenticating you should see your account details on the index page. You can also try http://localhost:4000/my_balance for your account balance.
The example app show several library features
- Watching for expired/refreshed credentials and updating the session example/server.js#L27
- Using promises with an error catch example/server.js#L49
- Passing in a callback function example/server.js#L61
git clone https://github.com/mdp/NCoinbase.git
cd NCoinbase
npm install
npm test