The YouTube Data API allows 100 search.list calls a day by default. Each call costs 1 unit in its own search bucket, returns up to 50 results, and counts again for every extra page. More quota needs an audit. Our API returns about 20 results a call for 1 credit, and the script below pages a search into a CSV.
What Google's search quota is
Google's search.list reference puts searches in their own bucket. Older guides say each search costs 100 units out of the 10,000; the 10,000 units no longer pay for searches. The numbers:
search.list: 100 calls a day, 1 unit each.- All other endpoints: 10,000 units a day combined.
maxResults: 0 to 50, default 5. Google can return fewer than you ask for, so follownextPageToken.- Each extra page, and each invalid request, costs again.
- More quota: complete an audit with the Audit and Quota Extension Form. Google prints no price.
Filters include type, publishedAfter, publishedBefore, order, regionCode and videoDuration. There is no Shorts parameter, and videoDuration=short means under four minutes, which is not the same set. The relatedToVideoId parameter is gone too; for that list see YouTube related videos. For a channel's latest uploads Google points to playlistItems.list, not search. Use Google's API for uploads, your own channel, captions you own, analytics, anything that needs your users' sign-in, and under 100 searches a day.
Search YouTube with ToolzerHub
We make ToolzerHub. Our YouTube API returns public YouTube search results as JSON with one API key and no Google Cloud project. GET /v2/youtube/search costs 1 credit a call. The script runs several searches, pages each one, and writes the videos to CSV. Pass shorts instead of video to get Shorts only.
import csv
import os
import sys
import time
import requests
URL = "https://api.toolzerhub.com/v2/youtube/search"
HEADERS = {"x-api-key": os.environ.get("TOOLZERHUB_API_KEY", "")}
FIELDS = ["query", "video_id", "title", "channel", "handle", "views", "published", "length", "url"]
calls = 0
def cell(value):
# Spreadsheets run text that starts with = + - or @ as a formula; an apostrophe keeps it text.
if isinstance(value, str) and value[:1] in ("=", "+", "-", "@", "\t", "\r"):
return "'" + value
return value
def get(params, tries=5):
# 429 and 503 are retryable; the error body may say how long to wait.
global calls
for attempt in range(tries):
r = requests.get(URL, headers=HEADERS, params=params, timeout=90)
calls += 1
if r.status_code == 400: # bad parameter: skip this query, keep the rest of the run
print("bad request:", r.json()["error"]["message"].splitlines()[0])
return None
if r.status_code not in (429, 503) or attempt == tries - 1:
r.raise_for_status()
return r.json().get("data") # None when there is nothing to return
try:
wait = r.json()["error"]["details"]["retryAfter"]
except (ValueError, KeyError, TypeError):
wait = 5 * (attempt + 1)
time.sleep(min(wait, 60))
def videos(entries):
# Shelves such as the Shorts row hold their items one level down; ads are skipped.
for e in entries:
if e.get("type") in ("video", "shorts"):
yield e
elif isinstance(e.get("data"), list):
yield from videos(e["data"])
def row(query, v):
return {
"query": query,
"video_id": v["videoId"],
"title": (v.get("title") or "").replace("\n", " "),
"channel": v.get("channelTitle"),
"handle": v.get("channelHandle"),
"views": v.get("viewCount"),
"published": (v.get("publishedAt") or "")[:10],
"length": v.get("lengthText"),
"url": f"https://www.youtube.com/watch?v={v['videoId']}",
}
def main(queries, pages="3", kind="video", out="youtube_search.csv"):
seen = set()
with open(out, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=FIELDS)
writer.writeheader()
for query in [q.strip() for q in queries.split(",") if q.strip()]:
params = {"search_term": query, "result_kind": kind}
for _ in range(int(pages)):
data = get(params)
if not data or not data.get("data"):
break
for v in videos(data["data"]):
if v["videoId"] not in seen: # pages can overlap
seen.add(v["videoId"])
writer.writerow({k: cell(x) for k, x in row(query, v).items()})
if not data.get("page_cursor"):
break
params["page_cursor"] = data["page_cursor"]
print(f"wrote {len(seen)} videos to {out} in {calls} calls")
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit('usage: python youtube_search.py "python tutorial,lofi music" [pages_per_query] [video|shorts] [file.csv]')
if not HEADERS["x-api-key"]:
sys.exit("set TOOLZERHUB_API_KEY first")
main(*sys.argv[1:])
export TOOLZERHUB_API_KEY=your_key
python youtube_search.py "python tutorial,nasa artemis,lofi music,how to tie a tie" 3 video youtube_search.csv
wrote 239 videos to youtube_search.csv in 12 calls
The four searches gave 239 videos from 112 distinct channel handles. One video matched two queries and was written once. The same script with shorts on "python tutorial" wrote 46 Shorts in 2 calls.
How deep a search goes
We ran 10 searches and paged four of them to the end.
- A page holds about 20 videos. The default mix adds playlists, courses, channels, a Shorts row and sometimes an ad;
result_kind=videoreturns videos only. - Paging has an end: a query runs 29 to 31 calls and returns 517 to 593 unique videos.
- Filters that work:
result_kind(video, shorts, channel, playlist),published_within(today, week, month, year),length_filter(under 3 minutes, 3 to 20, over 20),sort_order(relevance, popularity),region_code,language_code. result_kind=shortsreturns only Shorts, all 60 seconds or shorter. For one channel's Shorts, see get a channel's Shorts.result_kind=videonever returns a Shorts item, but short clips still appear as videos, so checklengthTextfor a hard cut.- Results carry
viewCountbut no like count; likes come from/v2/youtube/video, 1 credit a call.publishedAtlooks derived from text such as 2 years ago, so older videos get the right year and an approximate day.
Search inside one channel
Google's search.list keeps results to one channel when you pass channelId, and with type=video such a search stops at 500 videos. It draws on the same 100 searches a day. Our GET /v2/youtube/channel/search takes channel_username or channel_id with a search_term, returns about 30 results a call with view counts and publish dates, and costs 1 credit.
curl "https://api.toolzerhub.com/v2/youtube/channel/search?channel_username=mkbhd&search_term=iphone" \
-H "x-api-key: $TOOLZERHUB_API_KEY"
Follow page_cursor until it is missing. MKBHD's channel searched for iphone ended after 15 pages with 347 results, 70 of them Shorts. NASA's channel searched for artemis ended after 17 pages with 416, including 33 Shorts and 4 playlists.
What it costs
| Google default | ToolzerHub | |
|---|---|---|
| Free allowance | 100 search calls a day | 100 credits a month |
| Results per call | Up to 50 | About 20 |
| Free results | Up to 5,000 a day | About 2,000 a month |
| 1,000 calls a day | Needs an audit | 1,000 credits a day |
A job of 20 queries with 5 pages each is 100 calls. On Google that is the whole day's quota and up to 5,000 results. On ours it is 100 credits and about 2,000 results, and one query paged to its end is about 30 credits. The 100 free credits a month cover that job once; $19 buys 10,000 credits that never expire, and plans for higher volume cover more searches. Set your key and run the script on your first query.
For the suggestions under the search box see the YouTube autocomplete API, and to check whether a channel is live without spending searches, this live check. For transcripts see our YouTube transcript API guide. For another Google product whose API is gone, see does Google Finance have an API.
We are not affiliated with Google or YouTube.