Verify youtube api v3 video exists JS

1.9k views Asked by At

I used the v3 API to check if a youtube video exists, however, I am not able to fetch the return data. I assume I need the

"pageInfo": { "totalResults": 1, "resultsPerPage": 1 },

to check it. But how to get this information using the following code?

var data = $.get('https://www.googleapis.com/youtube/v3/videos?id=V2VmcuOEqEg&key={API-KEY-HERE}&part=status');
1

There are 1 answers

2
theduck On BEST ANSWER

You can use something like this to determine if a video exists:

var url = "https://www.googleapis.com/youtube/v3/videos";
var videoId = "id={YOUR-VIDEO-ID}";
var apiKey = "key={YOUR-API-KEY}";
var part = "part=snippet";

$.get(url + "?" + apiKey + "&" + videoId + "&" + part, function(response) {
    alert(response.pageInfo.totalResults);
});

response.pageInfo.totalResults will equal 0 if the specified video id does not exist and 1 if it does.