Get Trending Videos via YouTube API

 import logging
import os
import requests
from fastapi import FastAPI, HTTPException
from fastapi.responses import RedirectResponse
from some_get_request_handler import handle_get_endpoint
from some_post_request_handler import handle_post_endpoint, Data

logger = logging.getLogger(__name__)

app = FastAPI()

@app.get("/", include_in_schema=False)
def root():
    return RedirectResponse(url='/docs')

# Importing KeywordData class from
from youtube_trending_videos import get_trending_videos"/trending/")(get_trending_videos)

if __name__ == "__main__":

About this template

This app is provides fast api endpoint and is designed to fetch trending videos from YouTube based on keywords provided by the user. It utilizes the YouTube Data API v3 to search for videos that are currently trending in the US region. The app requires a valid YouTube API key, which must be set as an environment variable named `YOUTUBE_API_KEY`. The response includes detailed information about each video, including a direct link to the video on YouTube.

Introduction to the Template

Welcome to the "How to get trending videos for a specific keyword via YouTube API" template. This guide will walk you through the process of using this Lazy template to create an application that fetches trending videos from YouTube based on user-provided keywords. The application leverages the YouTube Data API v3 and is designed to be simple to use, even for non-technical builders.

Clicking Start with this Template

To begin, click on the "Start with this Template" button. This will initialize the template in the Lazy Builder interface, pre-populating the code necessary for the application.

Initial Setup: Adding Environment Secrets

Before testing the application, you need to set up an environment secret for the YouTube API key. Here's how to obtain and set up your YouTube API key:

  • Go to the Google Developers Console (
  • Create a new project or select an existing one.
  • Click on "Enable APIs and Services" and search for "YouTube Data API v3".
  • Enable the YouTube Data API v3 for your project.
  • Go to "Credentials" and click on "Create credentials". Select "API key" from the dropdown.
  • Copy the generated API key.
  • In the Lazy Builder interface, navigate to the Environment Secrets tab.
  • Create a new secret with the key `YOUTUBE_API_KEY` and paste the API key you copied as the value.

Test: Pressing the Test Button

Once the environment secret is set, press the "Test" button. This will deploy the application and launch the Lazy CLI. You will be provided with a server link to interact with the API.

Entering Input: Filling in User Input

If the application requires user input, the Lazy CLI will prompt you for it after pressing the test button. For this template, you will be asked to provide keywords for which you want to find trending YouTube videos.

Using the App

After deployment, you can use the provided server link to send requests to the API. Additionally, you will receive a link to the FastAPI documentation (`/docs`) where you can test the API endpoints directly from your browser.

Here's a sample request to the `/trending/` endpoint:

POST /trending/
Content-Type: application/json

  "keywords": "music"

And a sample response might look like this:

    "title": "Trending Music Video",
    "description": "Latest trending music video on YouTube",
    "videoUrl": ""
    // ... other video details
  // ... more videos

Integrating the App

If you wish to integrate this application with an external tool or service, you will need to use the server link provided by Lazy. For instance, you can add the API endpoint to a frontend application to display trending videos based on user input. Ensure that you handle the API key securely and do not expose it in the frontend.

For further integration, such as adding the app's server link in an external tool, follow the specific instructions provided by that tool for adding external API endpoints.

By following these steps, you should now have a fully functional application that can fetch trending YouTube videos based on keywords. Enjoy building with Lazy!

Last published
June 15, 2024

More templates like this

PDF Data Extraction and Excel Transfer

An app for extracting name, phone number, and email data from PDF files and transferring it to Excel.


GPT-4 Exam Generator

This app uses GPT-4 to generate a PDF of a new exam based on an uploaded PDF of a past exam.


Job Search Website

A customisable one-page job search website with a search bar and a search button that displays UI cards of matching jobs. You need to have a (free) account on to get the API key required for this to work.

React JS
Get Trending Videos via YouTube API