Stream Property not available... or I'm just blind

I’m attempting to determine if a user is online using the API. I can find the user and return all the information associated with them, i.e. display_name, followers, game, status… but to determine if someone is online, looking at previous posts in the forum, I see that people have used the below code

if(data.channels[0].stream === null){
        $('#stream').html('Not Online')
      } else if (data.channels[0].stream === undefined) {
        $('#stream').html('OffLine')
      } else {
        $('#stream').html('Online')

But I don’t see the stream property. I see status, and I assume I could use that, but everywhere I look, I see ‘stream’…

My code prints out ‘Offline’ to my html, but I’m testing a user I can actively see online right now playing a Zeldathon.

I’m using javascript by the way.

Can you show us your code in full so we can see what endpoint you are calling and maybe see where you’re going wrong.

From the code snippet you provided, it appears like you’re hitting the /channels/<channel-id> endpoint. To check whether or not someone is online, use the /streams/<channel-id> endpoint. If a stream is offline, the stream object in the response will be null.

Ok, below is my code. I’m not using the nameARRAY variable, I can see everything output into the console. Six, it does seem like I should be using the /streams/ endpoint, but, will I still be able to pull all the same information from the /channels/ endpoint or will I have to make a separate API call for it?

var searchTERM = $('#searchTERM');
var nameARRAY =["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", 
"habathcx", "RobotCaleb", "noobs2ninjas"];

$('#searchBTN').click(function() {
  console.log(searchTERM.val());
  $.ajax({
    type: 'GET',
    url: 'https://api.twitch.tv/kraken/search/channels/?query=' + searchTERM.val(),
    headers: {
      'Client-ID' : ''
    },
    success: function(data){
      console.log(data);
      $('#content').html(data.channels[0].display_name + ', ' + data.channels[0].views + ', ' + 
          data.channels[0].status + ', ' + data.channels[0].url);
      if(data.channels[0].stream === null){
        $('#stream').html('Not Online')
      } else if (data.channels[0].stream === undefined) {
        $('#stream').html('OffLine')
      } else {
        $('#stream').html('Online')
      }
    }
  });
});

Alright, I believe I figured it out. I added a second API call to the /streams/ that I’ve tested successfully with known offline and online users… or channels?

either way, thanks for the help.

Sorry for the late reply, but you should only need to make one API call since the channel object is embedded in the stream response. Although, there have been known issues where caches can be updated at different times giving slightly out of date results. That being said that should be a non-issue for the most part and just hitting streams endpoint should be fine.

Yeah, this confusion is why they were merged into one object in Helix :stuck_out_tongue:

Per-chances, can you guide me in displaying a channel’s logo?

nevermind, if you’re reading this I figured it out… I’ll just pass the logo url into an html element.

Yeah I just saw the above messages. Glad you were able to figure it out though! :smiley:

eh, it’s throwing a 404 error in the console…

You are missing a header specifying if you want v3 or v5 of the TwitchAPI

It’ll 404 if you request by ChannelID and not specifying a TwitchAPI version (as it defaults to 3 which is by name)

ok, so below is what my code looks like calling the image

headers: {
  'Client-ID' : 'xxxxxxx'
},

so, do I add it under the headers property? I’m going to assume yes and guess that it’s something similar to

headers: {
    'Client-ID': 'xxxxxxx',
    'header' : 'v5'
}

Ok I changed by header property to

headers: {
  'Client-ID' : 'ocau0ahlou2nmmvsfobg2ru18l2m59',
  'accept' : 'application/vnd.twitchtv.v5+json'
},

but now I’m just getting a 400 error…

I’ve had mixed success only using headers when specifying v3 or v5. Try specifying the version as a query parameter in the request itself.

So, it’d look something like below? Remove the version ‘Accept’ parameter from the headers property?

$.ajax({
    type: 'GET',
    url: 'https://api.twitch.tv/kraken/streams/' + searchTERM.val(),
    Accept : 'application/vnd.twitchtv.v5+json'
    headers: {
      'Client-ID' : 'xxxxx'
    },
    success: function(results){
      console.log(results);
    }

ah, I got it… again, it was how I was passing it into my html… I had my quotations all askew… Thanks again.

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