Trouble Getting "Viewers" From API

Hello,

I’m having issues nesting my Twtich.api request to go specifically to the user that will be using my app. Currently, I have the following lines:
Twitch.api({method: ‘streams’}, function(error, streams) {
console.log("View count is: " + streams.viewers);
});

This however, returns an array of all active streams, it shows all their view counts. How do I go about nesting my request to be specific to my user only? I have the user logged in, i find their display name, stream key, _id, and follower counts with ease (as these do not require nesting). I have thought about doing a for loop and looping through all of the active streams until I find one that matches the _id of my user but that seems rather tedious and lenghty. How do I go about narrowing down my search :smiley:

Cheers,
Hyden

I may be misunderstanding the question, but are you looking for the /streams/:channel endpoint? Are you trying to get the viewer count for only your user or is there other data that you are trying to gather?

1 Like

I am trying to just get the current view count of only my user correct

Ok, so, the /streams/:channel endpoint will return data if online, but not offline. So, if you want to get the active viewer count, yes, use /streams/:channel (i.e. /streams/illusionaryone). Now, if you want to get total views ever on the channel I would recommend the /channels/:channel (i.e. /channels/illusionaryone) endpoint, which will return the views data member off the primary object if the user is offline or online.

I hope that helps, if not, let me know!

1 Like

I thought I understood what you’re saying. I tried this:

Twitch.api({method: ‘streams/:channel’}, function(error, streams) {
console.log("View count is: " + streams.viewers);
});

However, in my console it says that :channels does no exist :confused:

In your case, let us say your user name is IllusionaryOne (me). You would access /streams/illusionaryone :channel is just a placeholder to say put the channel/user name you want here. So, put the name of the user that you are trying to get in the place of :channel

1 Like

Ah ok, that was pretty helpful it solved my first problem :smiley:

I can now narrow my search down to the user specifically, and I can see their view count but when I try to console.log their view count I’m unsure what to write to get it. Not sure if that makes sense, currently I have

console.log("View count is: " + channel.viewers);

But that just returns undefined so clearly my +channels.viewers is not correct

The total views of the channel is on the views data member:
“views”: 49144894,

1 Like

Sorry let me refresh, I can see that

I have found this information about my user. It shows that there is currently 1 Viewer watching.
How do I access this number specifically and put it into a variable or for now a console.log statement that would just say
“THE STREAMER HAS 1 VIEWER” kinda thing

Nvm I figured it out! :smiley:

console.log("View count is: "+ streams.stream.viewers);

Thanks for all the help!

1 Like

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