400 error from streams query with channel ID

I’m getting a list of “programming” channels with a channels query. That’s working fine. Then I’m trying to query the streams with each channel ID returned to check if the channel is currently streaming using the status as I’ve read elsewhere in this forum. For this second query I’m getting a 400 error for each request.

This format for the request seems to match what I see in the documentation, except that the documentation always seems to use numerical channel IDs. What am I doing wrong?

Update: I figured it out. I didn’t realize that there was a separate “_id” field in the response representing the channel ID. I thought the name was the channel ID :blush:

$.ajax({
  url: 'https://api.twitch.tv/kraken/search/channels?query=programming',
  headers: {
    'Accept': 'application/vnd.twitchtv.v5+json',
    'Client-ID': clientId,
  },
}).done(function(data){
  console.log(data);
  twitchData.channels = data.channels;
  
  for (var i = 0; i < twitchData.channels.length; i++) {
    var name = twitchData.channels[i].name;
    $.ajax({
      url: 'https://api.twitch.tv/kraken/streams/' + name,
      headers: {
        'Accept': 'application/vnd.twitchtv.v5+json',
        'Client-ID': clientId,
      },
    }).done(function(data){
      console.log(data);
    });
  }
});

You’re not trying to get the streams with the channel ID, you’re getting them with the channel name.

Try changing var name = twitchData.channels[i].name; to var id = twitchData.channels[i]._id; and then in the request URL change + name to + id

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.