This quickstart demonstrates how to query the YouTube Data API using Cloud Functions for Firebase with an HTTPS trigger.
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.
- Create a Firebase Project on the
Firebase Console if you don't already have a project you want to use.
- Upgrade your Firebase project to the Blaze "pay as you go" plan
- Enable the Youtube API by visiting the
API console,
selecting your Firebase project, and clicking "ENABLE".
- Once the API is enabled, visit the credentials tab and click "CREATE CREDENTIALS" to create a YouTube API key.
- Install the Firebase CLI and log in:
npm install --global firebase-tools firebase login
- Clone or download this repo and open the
youtube
directory. cd
into thefunctions
directory and install dependencies withnpm install
- Set up your Firebase project by running
firebase use --add
with the Firebase CLI, select your Project ID and follow the instructions. - Set the YouTube API key as an environment variable:
firebase functions:config:set youtube.key="THE API KEY"
- Set up the Firebase emulators with your config (docs):
cd functions firebase functions:config:get > .runtimeconfig.json
- Run the following command to start the emulator:
firebase emulators:start --only functions
- Check the emulator output to find the URL of the
getChannelInfo
function. It will looks something likehttp://localhost:5001/my-project-id/us-central1/getChannelInfo
- 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. - You should get a JSON response with information about the YouTube channel!
Deploy to Firebase using the following command:
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.
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!