Full Sample Code

Taking what we learned from the previous module, here is some sample Javascript code that uses the API correctly to return information about SKT Faker's (or the unkillable Demon King) twitch channel. Note that I did not include my own API key and you will need to insert your own key in order for this code to work.

                     var req = new XMLHttpRequest();
                     var APIkey = "INSERT_YOUR_API_KEY_HERE";
                     var curQuery = "channels/faker";
                     req.open("GET", "https://api.twitch.tv/kraken/" + curQuery, true);
                     req.setRequestHeader("Client-ID", APIkey);
                     req.addEventListener("load", function(){
                         if(req.status >= 200 && req.status < 400){
                         var response = JSON.parse(req.responseText);
                         console.log(response);}
                         else{
                         console.log("Error in network request: " + req.statusText);
                     }});
                     req.send(null);
                     event.preventDefault();

All we needed to do was set curQuery equal to "channels/faker" which was then added to the base URL to get "https://api.twitch.tv/kraken/channels/faker". If you look at the Twitch.tv API documentation and go to the /channel endpoint, you will see "GET/channels/:channel/". Note that there is a colon after "channels/". The colon means you should add your specific request parameter to the endpoint. In this particular case :channel became faker.

Previous Next