-1

How can I read this API and get information from it? to javascript.

https://bitcoinfees.earn.com/api/v1/fees/recommended

this does not seem to work - 
loadJSON("https://bitcoinfees.earn.com/api/v1/fees/recommended", gotData, 'jsonp');

function gotData(data) {
  println(data);
}
Zissouu
  • 924
  • 2
  • 10
  • 17
Jonas
  • 21
  • 7
  • 1
    Possible duplicate of [How to make an AJAX call without jQuery?](https://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery) – Serge K. Jan 08 '18 at 13:26
  • 3
    if you could explain what functions you're using, for instance where does `loadJSON` come from? it is not a native javascript function as far as i know – Flame Jan 08 '18 at 13:26

3 Answers3

1

loadJSON is not a native JS function(it is in library p5). You can use the fetch function like this:

    fetch("https://bitcoinfees.earn.com/api/v1/fees/recommended") // Call the fetch function passing the url of the API as a parameter
    .then((resp) => resp.json()) // Transform the data into json
    .then(function(data) {
        // Your code for handling the data you get from the API
     console.log(data);
        console.log(data.fastestFee); //And since it is just an object you can access any value like this
    })
    .catch(function(err) {
        // This is where you run code if the server returns any errors (optional)
     console.log(err)
    });
mdatsev
  • 3,054
  • 13
  • 28
1

Using javascript only:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
       // Typical action to be performed when the document is ready:
       document.getElementById("demo").innerHTML = xhttp.responseText;
       alert(JSON.stringify(xhttp.responseText));
      //To display fastestFee's value   
      var parseData = JSON.parse(xhttp.responseText);
      alert(parseData.fastestFee);
    }
};
xhttp.open("GET", "https://bitcoinfees.earn.com/api/v1/fees/recommended", true);
xhttp.send();
<div id="demo"> </div> 
Khurshid Ansari
  • 4,638
  • 2
  • 33
  • 52
0

First result on Google. Learn it. Use it.

    var url = "https://bitcoinfees.earn.com/api/v1/fees/recommended";
    
    var req = $.ajax(url, {
      success: function(data) {
        console.log(data);
      },
      error: function() {
        console.log('Error!');  
      }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
ProEvilz
  • 5,310
  • 9
  • 44
  • 74