YouTube

FREEMIUM
Por Dataverse | Atualizado 7 days ago | Data
Popularidade

9.6 / 10

Latência

136ms

Nível de serviço

100%

Health Check

100%

Voltar para todos os tutoriais (7)

Making a GET Request to the YouTube Search API

This tutorial shows how to make a GET request to the YouTube Search API using JavaScript and the fetch function.

Steps

  1. Define the URL for the API endpoint:
const url = 'https://youtube342.p.rapidapi.com/search?part=snippet&q=surfing';

Make sure to replace surfing with your desired search query.

  1. Set the request options, including the method and headers:
const options = {
  method: 'GET',
  headers: {
    'X-RapidAPI-Key': rapidAPIKey,
    'X-RapidAPI-Host': 'youtube342.p.rapidapi.com'
  }
};

Replace rapidAPIKey with your actual RapidAPI key.

  1. Make the API request using 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.