Retrieving JSON from the Twitch API

Hello!

I’m having a bit of a blank moment at the moment. I’m looking to retrieve some JSON from the Twitch API. I have created my Client ID, stored a bunch of channels into an array. However how would I go about sending these channels into a query string via the array?

Code:

const channels = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
const url = "https://api.twitch.tv/kraken/streams/" + channels[] + "?client_id=t300383ams5iuxlej34gzwk11qjepn&callback=?";

function callback(data) {
  console.log(data);
}

$.getJSON(url, callback);

Thanks,
James.

The newest API changed to channel IDS. When I get home I can explain my working code showing you how to go from channel name to channel ID and then using that ID to get stream info.

http://codepen.io/adam4813/pen/RRqyow has the code at the bottom of the JS section.

Hi Adam, I’ve resulted to just wrapping everything in a for loop. However I’m not sure if this is bad practice as I will have to keep making AJAX requests to Twitch, thus resulting in poor performance? Let me know what you think:

const channels = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
for(let i = 0; i < channels.length; i++) {
  let url = "https://api.twitch.tv/kraken/streams/" + channels[i] + "?client_id=t300383ams5iuxlej34gzwk11qjepn&callback=?";

  function callback(data) {
    console.log(data);
  }

  $.getJSON(url, callback);

}

That’s about what i’m doing take a look at my more finalized version to see what I mean.

In an ideal world, Twitch’s API would allow for bulk searches. As it stands, we need to make one HTTP transaction per request. It’s suboptimal, but that’s the way Twitch has designed it, and it’s up to them to change it.

2 Likes

I see, thanks @PortableStick!