2

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.

Saad Hassan
  • 313
  • 2
  • 10
  • Can you console log obj? – Algo7 Apr 21 '20 at 01:11
  • when I try to log just the obj, it runs but it also returns: total amount: null – Saad Hassan Apr 21 '20 at 02:07
  • That's why `obj.data.amount` is giving you an error. Are you sure the parameters you're passing in to the `getBuyPrice` function is correct? – Algo7 Apr 21 '20 at 02:09
  • Try run the getBuyPrice function without exporting and wrapping it into an async and see what it does. – Algo7 Apr 21 '20 at 02:14
  • It still returns null. Could it be because my API key is still disabled? It said on my coin base account that the API key enables after 48 hours when created. – Saad Hassan Apr 21 '20 at 02:36
  • That could be the case. But usually that will give you a 401 unauthorized. Maybe check back 48hrs later to see if it works. – Algo7 Apr 21 '20 at 02:36

2 Answers2

5

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.

0

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...