Saad Hassan
Saad Hassan

Reputation: 323

How to use coinbase API for node.js

Is there anyone experience with using coinbase API for node.js

I am following the npm coinbase documentation but It is not working. I am simply trying to get the price of the bitcoin. I did first installed coinbase between, so that is not the problem.

const config = require('../configuration')
const coinbase = require('coinbase')

const apiKey = config.get('COINBASE_API_KEY')
const apiSecret = config.get('COINBASE_API_SECRET')

const client = new coinbase.Client({apiKey, apiSecret})

module.exports = {
  start: async () => {
    client.getBuyPrice({'currencyPair': 'BTC-USD'}, function(err, obj) {
      console.log('total amount: ' + obj.data.amount);
    });
  }
}

When I run it, it gives me the following error:

C:\Users\msaad\VisualStudioProjects\CryptoTrader-master\session-1>node index.js C:\Users\msaad\VisualStudioProjects\CryptoTrader-master\session-1\src\app\index.js:12 console.log('total amount: ' + obj.data.amount);

TypeError: Cannot read property 'data' of null

It always returns NULL when I run it.

Upvotes: 2

Views: 3670

Answers (2)

Aasmund Berge Endresen
Aasmund Berge Endresen

Reputation: 347

Am I the only one getting a bunch of warnings about severe vulnerabilities after installing the 'coinbase' npm package and running npm audit? I can't find anything about it when Googling, and all guides on fetching data from Coinbase with node.js seems to focus on using that package...

Upvotes: 0

user7896971
user7896971

Reputation:

You can disable SSL for your client.

const { apiKey, apiSecret } = require('./config.js');
const Client = require('coinbase').Client;

const myClient = new Client({ 'apiKey':apiKey, 'apiSecret':apiSecret, 
strictSSL:false });

myClient.getBuyPrice({'currencyPair': 'BTC-USD'}, function(err, obj){
    if(err) console.log("Error: ", err);
    console.log("Total Amount: ", obj.data.amount);
});

Alternatively you can get and use SSL certificates as explained in the answer to this question.

Upvotes: 5

Related Questions