Get Videos
Learn to retrieve information regarding videos on Twitch.
We'll cover the following
Fetch video details
Streamers can choose to save their past live streams as videos on their profiles so viewers can rewatch the videos or catch up on any missed streams. There are thousands of videos across Twitch, spanning multiple creators and categories. We can send a GET request to the videos endpoint of the Twitch API and retrieve the information of any video we want.
The URL for this endpoint is as follows:
https://api.twitch.tv/helix/videos
All requests to this endpoint must be authenticated either with an app or a user access token. We'll use an app access token in this lesson.
Request parameters
There are three required query parameters and several other optional query parameters. A few combinations of these parameters are invalid. The table below gives an overview of each parameter and the accepted combinations:
Parameter | Type | Category | Description |
| String | Required | This is the ID of the video. If we provide this parameter, the endpoint doesn’t need any additional parameter, including |
| String | Required | This is the ID of the user whose videos we want to fetch. We can only provide one user ID. |
| String | Required | This is the ID of the game whose videos we want to fetch. We can only provide one game ID. |
| String | Optional | This is a cursor value for forward pagination. Each response from this 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 this endpoint that has multiple pages returns a cursor value. We can provide this value in the |
| Integer | Optional | This is the number of results to be returned per page, with a maximum of 100 results per page. Its default value is |
| String | Optional | This is an ISO-639-1 two-letter code of the language the video is in. |
| String | Optional | This is the period within which the video was created. Its accepted values are |
| String | Optional | This is the order in which the returned videos are to be sorted. Its accepted values are |
| String | Optional | This is the type of video. A video can be a reupload of a stream, a highlight of a stream, and so on. Its accepted values are |
Although all three ID parameters are listed as required parameters, we only need to provide one of them to make a successful request. In other words, we can choose to provide either the video ID, the user ID, or the game ID, but not more than one. We don’t need to provide any other optional parameters if we choose to use the video ID—the id
parameter.
Let's make a couple of sample calls to this endpoint using different combinations of the parameters. We can change the values of the parameters below or add other parameters to the request.
Note: The code below extracts a video ID to be used later, so don't forget to save it. If your access 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/videos");const headerParameters = {Authorization: "Bearer {{APP_ACCESS_TOKEN}}","Client-Id": "{{CLIENT_ID}}",};// Providing parameters to retrieve videos sorted by number of viewsconst queryParameters = new URLSearchParams({user_id: "12826", // Official Twitch channelsort: "views",});const options = {method: "GET",headers: headerParameters,};async function getVideos() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}getVideos();
In the code above, we perform the following:
Lines 11–14: We define the query parameters for the request using the
user_id
andsort
parameters.Lines 16–19: We set the options for the API call, specifying the HTTP request type as GET and providing the request headers.
Lines 21–29: We define a function called
getVideos
to make a call to the videos endpoint.Lines 22–26: We make a call to the endpoint within a
try
block and print the response from the API.Lines 26–28: We catch any errors or exceptions within a
catch
block and print them to the console.
Line 31: We invoke the
getVideos
function.
The API responds with a list of videos the official Twitch channel has added to its profile. This list is sorted by the number of views.
Let's also make a request using a video ID. Here, we won't use any of the other optional parameters.
import fetch from "node-fetch";const endpointUrl = new URL("https://api.twitch.tv/helix/videos");const headerParameters = {Authorization: "Bearer {{APP_ACCESS_TOKEN}}","Client-Id": "{{CLIENT_ID}}",};// Providing only the id parameterconst queryParameters = new URLSearchParams({id: "{{VIDEO_ID}}",});const options = {method: "GET",headers: headerParameters,};async function getVideoDetails() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}getVideoDetails();
In the code above, we perform the following:
Lines 11–13: We define the query parameters for the request using the
id
parameter.Lines 15–18: We set the options for the API call, specifying the HTTP request type as GET and providing the request headers.
Lines 20–28: We define a function called
getVideoDetails
to make a call to the videos endpoint.Lines 21–25: We make a call to the endpoint within a
try
block and print the response from the API.Lines 25–27: We catch any errors or exceptions within a
catch
block and print them to the console.
Line 30: We invoke the
getVideoDetails
function.
Since IDs are unique, we only get the details of the singular video we passed as a parameter.
Response fields
The JSON response has two top-level properties—data
and pagination
. The search results are contained in the data
property in the form of an array of video objects. The table below discusses the properties of these objects:
Property | Type | Description |
| String | This is the ID of the video. |
| String | This is the ID of the stream, if any, from which the video is taken. It has a value only if the |
| String | This is the ID of the user to whom the video belongs. |
| String | This is the login name of the user to whom the video belongs. |
| String | This is the display name of the user to whom the video belongs. |
| String | This is the title of the video as it is displayed on the user's profile. |
| String | This is the description of the video. |
| String | This is an RFC3339 timestamp for when the video was created. |
| String | This is an RFC3339 timestamp for when the video was published. |
| String | This is the URL where the video can be accessed/viewed. |
| String | This is the static URL for the thumbnail of the video. |
| String | This is the viewing setting of the video—either |
| Integer | This is the number of views the video has accumulated. |
| String | This is an ISO-639-1 code for the language the video is in. |
| String | This is the type of video that indicates whether the video is a reupload of a past stream, a new upload, and so on. This property can take any of these values— |
| String | This is the running time of the video. |
| Object | This indicates which segments of the video, if any, are muted. This is an object containing two values, |