0

I am looking for the Node.js of the following PHP Script:

$SMA_APICall = "https://www.alphavantage.co/query?function=SMA&symbol=".$symbolValue."&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS";
          $SMAresponse = file_get_contents($SMA_APICall);
          $jsonSMA = json_encode( $SMAresponse);

Here, I am trying to make a call to an API. The API call returns a json object. I want to repeat the same thing using Node js

3
  • Give JSON.parse a try. Commented Oct 30, 2017 at 0:21
  • How do I make call to the API. I used this : var jsonFile = "link to api" . This gives me jsonFile variable holding a string of the link. Instead I want to get the data from the link. Commented Oct 30, 2017 at 0:37
  • errr .... you are already getting json from the http call, so no need to encode it (in php above). What you probably want is json_decode (in php) which is typically a JSON.parse in js. Also, dont post publicly your api keys for services you subscribe. Commented Oct 30, 2017 at 2:49

2 Answers 2

1

I believe what you're trying to do is making a request to an API and get the JSON data. Here's how you can do it with native Node.js module https

 const https = require('https');

 https.get(`https://www.alphavantage.co/query?function=SMA&symbol=${symbolValue}&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS`, (resp) => {
  let data = '';

  resp.on('data', (chunk) => {
    data += chunk;
  });

  resp.on('end', () => {
    console.log(JSON.parse(data)); // JSON Data Here
  });

}).on("error", (err) => {
  console.log("Error: " + err.message);
});

There're several other ways you can do this with other simpler packages. I highly recommend axios because it's cleaner and easier.

The full examples please refer to this article

Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at the request library: https://github.com/request/request

var request = require('request');
var url = "https://www.alphavantage.co/query?function=SMA&symbol=" + symbolValue + "&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS";
request(url, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var jsonSMA = JSON.parse(body);
    // Continue code here
  }
});

4 Comments

It says cannot find module 'request'. What to do about this?
You need to install this module. Run npm install --save request on your command line. --save will add it as a dependency in your package.json file.
So now, all the data I am getting is being stored in jsonSMA. The issue which I am facing right now is , how do I access the data in jsonSMA globally. I have to make such API Calls to many different APIs. So how to i make a global variable jsonSMA. I tried creating var jsonSMA outside of the request call. It doesn't work/
Node.js is asynchronous unlike PHP. This has many advantages for web applications but it takes some getting used to. See docs.nodejitsu.com/articles/getting-started/control-flow/…. If you need to make a number of API requests, I would recommend looking into the async module - github.com/caolan/async.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.