In this example we will show how to retrieve videos from a specific category using the YouTube Data API. We will use the GET /search
endpoint to search for videos by category. Our example will search for videos in the “Gaming” category. You can find the full list of categories in the Categories page.
Request examples
const options = {
method: 'GET',
headers: {
Authorization: 'Bearer poix-xxx',
},
};
fetch(
'https://api.poix.io/search?videoCategoryId=20&type=video&maxResults=1',
options
)
.then((response) => response.json())
.then((response) => console.log(response))
.catch((err) => console.error(err));
In this request we are searching for videos in the Gaming 20
category. We are also setting the maxResults
parameter to 1 to get only one video. You can change the videoCategoryId
parameter to search for videos in a different category. Setting the type
parameter to video
required for the request when using videoCategoryId
parameter.
Response example
{
"regionCode": "US",
"totalResults": 1000000,
"resultsPerPage": 1,
"nextPageToken": "CAEQAA",
"data": [
{
"kind": "youtube#video",
"videoId": "FHDYk9HqomU",
"publishedAt": "2014-05-23T13:44:06Z",
"channelId": "UCKk076mm-7JjLxJcFSXIPJA",
"title": "Show of the Week: Watch Dogs and the 5 Least Secure Security Systems in Games",
"description": "Watch Dogs leads Show of the Week, with its smartphone hacking and omnipresent rising bollards, followed by the least secure ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/FHDYk9HqomU/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/FHDYk9HqomU/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/FHDYk9HqomU/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "outsidexbox",
"liveBroadcastContent": "none",
"publishTime": "2014-05-23T13:44:06Z"
}
]
}
The result is similar to what you would see on the YouTube website when searching for videos. Get more details about this API endpoint in the API Search documentation.