This tutorial shows how to make a GET request to the YouTube Search API using JavaScript and the fetch
function.
const url = 'https://youtube342.p.rapidapi.com/search?part=snippet&q=surfing';
Make sure to replace surfing
with your desired search query.
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': rapidAPIKey,
'X-RapidAPI-Host': 'youtube342.p.rapidapi.com'
}
};
Replace rapidAPIKey
with your actual RapidAPI key.
fetch
:try {
const response = await fetch(url, options);
const result = await response.text();
console.log(result);
} catch (error) {
console.error(error);
}
The fetch
function sends the GET request to the specified URL with the provided options. The response is then parsed as text using response.text()
, and the result is logged to the console.
If an error occurs during the request, it will be caught in the catch
block and logged to the console.
That’s it! You have now made a GET request to the YouTube Search API and can process the response as needed in your application.