skip to Main Content

I am working on a YouTube clone from YouTube API v3, whereas I need to related videos list based on the current playing video’s videoId.

async function getApiData(videoId) {
    const res = await apiRequest('/search', {
        params: {
            part: 'snippet',
            videoCategoryId: videoId, // relatedToVideoId : videoId
            maximumResult: 10,
            type: 'video'   
        }
    })
    console.log("res", res)
    return res;
}

In the params, I have passed videoCategoryId/relatedToVideoId, in different requests.
but it sends an error message.

What would be the correct way to get a related video list from the YouTube API based on the videoId?

Thanks for your support

2

Answers


  1. I think to retrieve related videos based on a given videoId using the YouTube API v3, you should use the relatedToVideoId parameter instead of videoCategoryId.

    The videoCategoryId parameter is used for filtering videos by category, not for retrieving related videos.

    I add sample code below:

    async function getRelatedVideos(videoId) {
        const res = await apiRequest('/search', {
            params: {
                part: 'snippet',
                type: 'video',
                relatedToVideoId: videoId,
                maxResults: 10, // It's 'maxResults', not 'maximumResult'
            }
        });
    
        console.log("res", res);
        return res;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search