How to just check if a channel is live?

Hi guys, I’m currently using the older API to show a status bar on my site when our stream is live.

Currently just doing this:"+twitch_key+"&channel="+channel

There I would just check if it has proper data, if it is then we’re online.

I’m looking over the new API and I’m so confused it’s unreal.

All I want is a bar to show if we’re live on our site, how would I do that?

1 Like

I don’t think you quite get what I’m saying.

I’m used to using the old API where we didn’t need to do any kind of auth or anything, I simply fired off the request like this:"+twitch_key+"&channel="+channel

Then I get data back.

I don’t understand how to do it now in something like Javascript, just to display a bar saying we’re live. Do I have to go through some form of auth now and use webhooks and callbacks? The documents seem rather complicated to do such a simple request.

You don’t need to go through some form of auth now, or use webhooks. The main differences are that the client id has to be in the header of the request now, and if you’re looking for data on multiple channels instead of doing &channel=CHANNEL1,CHANNEL2,CHANNEL3 you would need to do &user_login=CHANNEL1&user_login=CHANNEL2&user_login=CHANNEL3 etc…

Helix also has rate limits, which means you can make 30 requests per min if you use just your client-id, but if you use an app token your limit would be 120 a min, but again that’s not a requirement, you can do it entirely without authentication on that endpoint if you choose.

Doesn’t seem to work as simply as that.

Switching to helix, so doing: (where id is the ID from my Client ID in the dev dashboard for my “app”)

Always gives me:

{“error”:“Unauthorized”,“status”:401,“message”:“Must provide a valid Client-ID or OAuth token”}

You have the client id in the querystring. You need to send it as the Client-ID header.

1 Like

Well then, that makes more sense, I’m now able to actually get some data back thanks.

Next issue, we only seem to get the game id now not the actual name, is there a way to grab the game name?

You can use the games endpoint to get the name of the game using the id

Once you get the name, you’ll likely want to cache it so you wont have to make another request each time you need to translate a game ID into its name.

I’m thinking I might just do a PHP cron that curl’s the API, store the result on our server and then update it once a minute.

Then we’re only doing one single API call (well, maybe two to get the game name) and every single visitor is only then querying a local file on our site.

I also need that information for my project and since there was no definitive boolean value that told me this I build that value from the Amount of viewers a channel has:

NULL viewers -> Channel offline
Not NULL viewers (or a number) -> Channel online

@liamdawe hope that helps you

It’s probably not best to to determine the online/offline status of a stream based on the value of the viewers field, because that field, along with the whole stream object itself, wont exist if the stream is offline.

A better way would be to see if a stream object exists for the channel you’re looking up. If it exists the stream is online, else it’s offline.

That’s why it repeats an NULL Value :wink:

Or of course another option is to proof if the object exists. Which has basically the same result.

Not exactly the same.

If you the stream is offline you’ll get a response like:

{ data: [] };

So if you check for viewers:

const stream = data[0];

if(stream.viewers !== null) {
    console.log('Stream Online');
} else {
    console.log('Stream Offline');

This will throw an error when the stream is offline because TypeError: Cannot read property 'viewers' of undefined, as there is no data[0] when the stream is offline (this is just when checking a single stream btw, there may be other stream objects if looking up multiple channels).

Sure you could catch and handle the error, but it’s bad practice when you can much more easily check if the object itself exists, and not error at all.

In case it helps anyone else, I’m just doing this and it works perfectly.

I firstly grab the data from:

Then, I also query for the name of the game if it exists:

Then I save it to a local file (bundling in a game name if it exists), this is done on a cron once a minute so we never even touch the sides of the API limit - it also means actually loading it for users is super fast.

Then, with JS we do a simple check on the file:

	var json_file = "twitchstatus.json";
	$.getJSON(json_file, function(t) 
		if (t["data"].length > 0 && t["data"][0].type == 'live')
			if (t["game_name"].length > 0)
				// we have a game name, use it here
			else if (t["data"][0].title.length > 0)
				// no game name, use the stream title
			// show a "we're live" bar here

Or like this, hope it helps :tea:

Not in my case, I use a Linux Shell Script to make calls towards the API.

Results might diff from the used program language.

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