Import YouTube Data to Google Sheets
YouTube provides a Data API, a Reporting API, and an Analytics API, all with different but overlapping functionality. In this guide, we’ll be looking at the Data API, which provides video and channel statistics for all public videos and channels. We will walk through how to pull video data from the YouTube Data API directly into Google Sheets using the API Connector add-on for Sheets.
YouTube’s Analytics API, which provides detailed private data about your account (i.e. data you need to be logged in to view) is discussed in a separate article.
- Before You Begin
- Part 1: Connect to the YouTube Data API
- Part 2: Create your API Request URL
- Part 3: Pull YouTube API Data into Sheets
- Part 4: More Example API URLs
- Part 5: Handle Filtering
- Part 6: Handle Pagination
- Appendix: Connect with a YouTube Data API Key
BEFORE YOU BEGIN
Click here to install the API Connector add-on from the Google Marketplace.
PART 1: CONNECT TO THE YOUTUBE DATA API
If you haven’t connected to the YouTube Data API before, you’ll first need to initiate the connection.
- Open up Google Sheets and click Add-ons > API Connector > Manage Connections.
- In the list of available connections, find YouTube Data and click Connect.
- You will see a modal asking you to approve the connection. Click Allow.
- You’ll then be returned to your Google Sheet, and can verify that your YouTube Data connection is active in the Connections screen.
PART 2: CREATE YOUR YOUTUBE API REQUEST URL
- API root: https://www.googleapis.com/youtube/v3
- Endpoint: /search
- Parameters: ?part=snippet&q=cats
Putting this all together, we get the full API Request URL:
PART 3: PULL YOUTUBE API DATA INTO SHEETS
Now let’s enter our URL into API Connector and import YouTube data into Google Sheets.
- Open up Google Sheets and click Add-ons > API Connector > Open.
- In the Create tab, enter the API URL we just created.
- Leave Headers empty as we don’t need them for this request.
- Under Authentication, choose YouTube Data from the dropdown menu.
- Create a new tab and click ‘Set current’ to use that tab as your data destination.
- Name your request and click Run. A moment later you’ll see some YouTube data populate your spreadsheet:
PART 4: MORE EXAMPLE API URLs
You can experiment with endpoints and query strings as described in the documentation to see other types of YouTube data, but if you just want to jump in and get a feel for it, play around with the URLs you enter in the API URL path field. Try the following (one at a time), substituting in actual values where indicated.
- A list of 50 videos matching a search string (default is 5 results):
- A list of videos in a playlist (substitute in your own playlist ID):
You can find your playlist ID by clicking into a YouTube video in your playlist and checking the URL. It will be located inside the
- A list of videos you’ve liked:
- Key statistics (views, likes, dislikes, comments) for a list of videos (substitute in your own video IDs):
- Key statistics (view counts, video counts, and subscriber counts) for a channel (substitute in your own channel ID):
To get the channel ID, hover over or click the channel name listed under any video, and check the text in the URL after /channel/
- Key statistics for multiple channels:
(enter your channel IDs as a comma-separated list)
PART 5: HANDLE FILTERING
You can filter your results using the “fields” parameter as described here: https://developers.google.com/youtube/v3/getting-started#fields. For example, this filtered URL will produce a list of metrics like video titles, view counts, like counts, dislike counts, etc. for all the video IDs listed in the
PART 6: HANDLE PAGINATION
By default YouTube limits the number of results in a single response to 5 rows, which can be increased to 50 by adding
&maxResults=50 to the end of your API request URL.
If you want to retrieve more than 50 results at a time, you’ll need to traverse the paginated results as described here: https://developers.google.com/youtube/v3/guides/implementation/pagination. It says that you need to run your first request URL, then take the value returned in the
nextPageToken field and plug that into a
pageToken query string and run a second request, and then repeat the process for as many pages as you’d like to retrieve. This can be handled automatically by API Connector through the pagination handling feature. If you are using automatic pagination handling, you’d set up your request as follows.
- API URL: enter your request URL as usual, making sure to include maxResults=50 at the end
- Pagination type:
- Next token parameter:
- Next token field:
- Number of pages: enter the number of pages you’d like to fetch
APPENDIX: CONNECT WITH A YOUTUBE DATA API KEY
YouTube requires that all API requests are authenticated. This is the detailed, step-by-step process to authenticate with a YouTube API key:
to any API URL, e.g.
https://www.googleapis.com/youtube/v3/search?part=snippet&q=cats&key=YOUR_API_KEY. Since you’re manually including an API key, leave OAuth2 authentication set to None.
- Navigate to https://console.developers.google.com/. If this is your first time accessing this page, you’ll be prompted to accept Google Cloud Platform’s terms of service. Click agree and continue.
- Now in the top left, click Select a Project, which will open up a modal. Click New Project
- This will open up the New Project modal. You can name your project anything, but let’s call it YouTube API. You’ll also be prompted to browse for a Location (aka folder). For our purposes here, it doesn’t matter which one you pick.
- Once you create your project, click on Credentials in the left-hand menu. Next, click the Create credentials dropdown and click ‘API key’.
- Your YouTube API key is now ready. You’ll see a screen containing your API key; copy this to your clipboard and click Close.
- There’s still one last step: you need to enable the YouTube API in your project. To do this, click Library and search for ‘YouTube Data API’. Alternately, you can navigate there directly with this link: https://console.developers.google.com/apis/library/youtube.googleapis.com. Click Enable as shown:
- Congrats, you’re done! You now have access to the YouTube Data API, and can start pulling YouTube data into Google Sheets.