Back to Functions Samples

YouTube: Get information about a YouTube channel (2nd Gen)

Node/youtube/README.md

latest3.0 KB
Original Source

YouTube: Get information about a YouTube channel (2nd Gen)

This quickstart demonstrates how to query the YouTube Data API using Cloud Functions for Firebase (2nd Gen) with an HTTPS trigger.

Introduction

The function getChannelInfo returns information about a Youtube channel. By default it will return information about the Firebase YouTube channel, but you can pass it a channelId URL Query parameter to query any channel you'd like.

Setup

Get a YouTube API Key

  1. Create a Firebase Project on the Firebase Console if you don't already have a project you want to use.
    1. Upgrade your Firebase project to the Blaze "pay as you go" plan
  2. Enable the Youtube API by visiting the API console, selecting your Firebase project, and clicking "ENABLE".
    1. Once the API is enabled, visit the credentials tab and click "CREATE CREDENTIALS" to create a YouTube API key.

Clone and configure the function

  1. Install the Firebase CLI and log in:
    npm install --global firebase-tools
    
    firebase login
    
  2. Clone or download this repo and open the Node/youtube directory.
  3. cd into the functions directory and install dependencies with pnpm install
  4. Set up your Firebase project by running firebase use --add with the Firebase CLI, select your Project ID and follow the instructions.
  5. Set the YouTube API key as a secret:
    bash
    firebase functions:secrets:set YOUTUBE_API_KEY
    
    When prompted, enter the API key you created.

Run your function locally with the Firebase Emulator Suite

  1. Start the emulator:
    bash
    firebase emulators:start --only functions
    
  2. Check the emulator output to find the URL of the getChannelInfo function. It will looks something like http://localhost:5001/my-project-id/us-central1/getChannelInfo
  3. Via CURL or in your browser, visit the URL that the function is running at. Optionally, add a query string ?channelId=SOME_CHANNEL_ID to the end of the URL.
  4. You should get a JSON response with information about the YouTube channel!

Deploy the app to prod

Deploy to Firebase using the following command:

bash
firebase deploy

This deploys and activates the getChannelInfo function.

The first time you call firebase deploy on a new project with Functions will take longer than usual.

Modify it to your needs

Now that you've got this sample working, modify it to work for your use case! Some ideas:

  • Check out the other things you can query with the YouTube Data API
  • Convert getChannelInfo function to a scheduled function, and write the new latest videos for a channel into Firestore or Realtime Database
  • ...anything else you can think of!