Active Streams
Learn to retrieve a list of all ongoing streams on Twitch.
We'll cover the following
Most users visit Twitch to watch live streams. In fact, the first page we see upon visiting Twitch contains a number of live streams. We can start watching them with just a click.
The Twitch API can also be used to get a similar list of streams. We can send a GET request to the streams endpoint of the Twitch API to get a list of all current live streams.
It returns a list sorted in descending order of current viewers, spread over multiple pages of results. Since we can fetch only one page at a time, subsequent requests for further pages might show duplicate or missing streams as viewers join and leave streams. This changes the order in which the streams are sorted.
The URL for this endpoint is as follows:
https://api.twitch.tv/helix/streams
All calls to this endpoint must be authorized with either an app or a user access token. We'll use an app access token in this lesson.
Request parameters
It doesn't require any parameters. However, we can provide several optional query parameters to filter the returned list of streams. These parameters are shown in the table below:
Parameter | Type | Category | Description |
| String | Optional | This is a cursor value for forward pagination. Each response from the streams endpoint that has multiple pages returns a cursor value. We can provide this value in the |
| String | Optional | This is a cursor value for backward pagination. Each response from the streams endpoint that has multiple pages returns a cursor value. We can provide this value in the |
| Integer | Optional | This is the number of streams to be returned per page, with a maximum of 100 results per page. Its default value is |
| String | Optional | This parameter filters the returned streams based on the game specified by it. We can provide a maximum of 100 game IDs. |
| String | Optional | This is an ISO-639-1 code to filter results based on the stream language. We can provide a maximum of 100 language codes. |
| String | Optional | This parameter filters the returned streams based on the user ID specified by it. We can provide a maximum of 100 user IDs. |
| String | Optional | This parameter is similar to the |
Let's authorize ourselves with an app access token and make a request to this endpoint. We can then view the response we get from the API.
Note: If your token has expired, return to this lesson and follow the steps to generate a new one.
import fetch from "node-fetch";const endpointUrl = new URL("https://api.twitch.tv/helix/streams");const headerParameters = {Authorization: "Bearer {{APP_ACCESS_TOKEN}}","Client-Id": "{{CLIENT_ID}}",};const options = {method: "GET",headers: headerParameters,};async function fetchActiveStreams() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}fetchActiveStreams();
In the code above, we perform the following:
Lines 10–13: We set the options for the API call, specifying the HTTP request type as GET and providing the request headers.
Lines 15–22: We define a function called
fetchActiveStreams
to make a call to the streams endpoint.Lines 16–19: We make a call to the endpoint within a
try
block and print the response from the API.Lines 19–21: We catch any errors or exceptions within a
catch
block and print them to the console.
Line 24: We invoke the
fetchActiveStreams
function.
Response fields
A successful response to a call to this endpoint is a JSON object with two top-level properties—data
and pagination
. The data
property is a list of stream objects, and the pagination
property contains a cursor value for pagination.
The table below gives an overview of the properties present in a single stream object contained within the data
array:
Property | Type | Description |
| String | This is the ID of the stream. |
| String | This is the ID of the user broadcasting the stream. |
| String | This is the login name of the user broadcasting the stream. |
| String | This is the display name of the user broadcasting the stream. |
| String | This is the ID of the game being played on the stream. |
| String | This is the name of the game being played on the stream. |
| String | This is the type of stream. Since we're retrieving ongoing streams, its value is |
| String | This is the title of the stream. |
| Integer | This is the number of viewers currently watching the stream. |
| String | This is the UTC timestamp of when the stream was started. |
| String | This is an ISO-639-1 code for the stream language. |
| String | This is the URL of the stream thumbnail. We can retrieve different thumbnail sizes by replacing the |
| Array[String] | This is a list of IDs of all tags that apply to the stream. |
| Boolean | This indicates whether the stream is intended for mature audiences. |