YouTubeAnswer

YouTube comments API: get all comments from a video

Google's API returns up to 100 comment threads a call for 1 unit, with partial replies. Ours returns 20 top-level comments a call, saved to CSV.

6 min read

Short answer

  • Google's YouTube Data API pages through a video's comments at 1 unit a call and up to 100 threads a page, inside a default 10,000 units a day.
  • A thread carries only some of its replies, and the rest need a second call per thread with comments.list and a parent ID.
  • ToolzerHub's comments route returns top-level comments, about 20 a call at 1 credit, with a reply count and no reply text. Newest order reaches the end.
On this page

Google's YouTube Data API pages through a video's comments at 1 quota unit a call and up to 100 threads a page, but a thread carries only some of its replies. Our API returns top-level comments, about 20 a call, with a reply count and no reply text. Set sort_order=newest and the script below pages a video to the end into a CSV.

What Google's API returns

Google's commentThreads.list takes a videoId and returns comment threads. You need a Google account, a project and an API key. A thread's replies field is partial, so you call comments.list with parentId for the rest. Use Google's API when you need replies, comment search, moderation or a clear error for disabled comments. The costs:

  • commentThreads.list: 1 unit a call, 1 to 100 threads a page.
  • comments.list with parentId: 1 unit a call, 1 to 100 replies a page.
  • Default quota: 10,000 units a day; more needs a compliance audit.
  • Every extra page, and every invalid request, costs again.

Comments with ToolzerHub

We make ToolzerHub. Our YouTube API returns a public video's comments as JSON with one API key and no Google project. GET /v2/youtube/comments takes video_reference (an ID or a watch, youtu.be, Shorts or embed URL), sort_order (top or newest) and page_cursor, and costs 1 credit a call.

Each comment has commentId, textDisplay, likesCount, replyCount, publishedAt and the author's channel ID and handle. The first page also carries commentsCount, YouTube's total for the video, replies included. Likes from 1,000 up come back as rounded text such as 1.8K; the script turns it into a number (1800), so large counts are approximate.

Export a video's comments to CSV

Save this as youtube_comments.py. It writes one row per comment ID, without the author, and stops at the end of the video or at your page cap.

python
import csv
import os
import sys
import time

import requests

URL = "https://api.toolzerhub.com/v2/youtube/comments"
HEADERS = {"x-api-key": os.environ.get("TOOLZERHUB_API_KEY", "")}
FIELDS = ["video", "comment_id", "published", "likes", "replies", "text"]
calls = 0
FAILED = object()  # marks a 400 or a 503 that outlasted the retries, so the run can tell it from "empty"


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 num(value):
    # Counts of 1,000 and up can come back as text such as "1.8K".
    text = str(value or "0").upper().replace(",", "")
    try:
        return int(float(text[:-1]) * 1000) if text.endswith("K") else int(text)
    except ValueError:
        return 0


def get(params, tries=4):
    # 429 and 503 are retryable; the wait comes from the error body or the Retry-After header.
    global calls
    for attempt in range(tries):
        r = requests.get(URL, headers=HEADERS, params=params, timeout=90)
        calls += 1
        if r.status_code == 400:  # malformed video id: skip this video, keep the rest of the run
            print("bad request:", r.json()["error"]["message"].splitlines()[0])
            return FAILED
        if r.status_code not in (429, 503) or attempt == tries - 1:
            if r.status_code in (429, 503):  # still failing after the retries: give up on this video
                return FAILED
            if r.status_code >= 400:
                sys.exit(f"HTTP {r.status_code}: {r.text[:200]}")
            return r.json().get("data")  # None when there is nothing to return
        try:
            wait = r.json()["error"]["details"]["retryAfter"]
        except (ValueError, KeyError, TypeError):
            wait = None
        try:
            wait = int(wait or r.headers.get("Retry-After") or 5 * (attempt + 1))
        except ValueError:
            wait = 5 * (attempt + 1)
        time.sleep(min(wait, 60))


def export_video(writer, video, max_pages, sort):
    params = {"video_reference": video, "sort_order": sort}
    seen, pages, more, failed, total, replies = set(), 0, False, False, None, 0
    while pages < max_pages:
        data = get(params)
        if data is FAILED:
            failed = True
            break
        if not data or not data.get("data"):
            break  # null data, comments turned off, no comments, or an unknown id
        pages += 1
        total = total or data.get("commentsCount")
        for c in data["data"]:
            if c["commentId"] in seen:  # keep one row per comment id
                continue
            seen.add(c["commentId"])
            replies += num(c.get("replyCount"))
            writer.writerow({
                "video": video, "comment_id": c["commentId"],
                "published": (c.get("publishedAt") or "")[:10],
                "likes": num(c.get("likesCount")), "replies": num(c.get("replyCount")),
                "text": cell((c.get("textDisplay") or "").replace("\n", " ")),
            })
        more = bool(data.get("page_cursor"))
        if not more:
            break
        params["page_cursor"] = data["page_cursor"]
        time.sleep(0.4)  # the free plan allows 3 requests a second
    if failed:
        status = " (gave up: bad request or repeated 429/503)"
    elif not pages:
        status = " (nothing returned: comments off, none yet, or unknown id)"
    elif more:
        status = " (stopped at the page cap, more available)"
    elif sort == "top":
        status = " (top order ended early; use newest for the full list)"
    else:
        status = f" (reached the end; {replies} replies not fetched; YouTube shows {total} in all)"
    print(f"{video}: {len(seen)} comments, {pages} pages{status}")
    return len(seen)


def main(videos, max_pages="50", sort="newest", out="comments.csv"):
    total = 0
    with open(out, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=FIELDS)
        writer.writeheader()
        for video in [v.strip() for v in videos.split(",") if v.strip()]:
            total += export_video(writer, video, int(max_pages), sort)
    print(f"wrote {total} comments to {out} in {calls} calls")


if __name__ == "__main__":
    if len(sys.argv) < 2 or (len(sys.argv) > 3 and sys.argv[3] not in ("newest", "top")):
        sys.exit("usage: python youtube_comments.py VIDEO_ID_OR_URL[,...] [max_pages] [newest|top] [file.csv]")
    if not HEADERS["x-api-key"]:
        sys.exit("set TOOLZERHUB_API_KEY first")
    main(*sys.argv[1:])
bash
export TOOLZERHUB_API_KEY=your_key
python youtube_comments.py "GUqsH5y1j1M,https://www.youtube.com/watch?v=1C_zuHf6lP4" 100 newest comments.csv

Both videos ran to the end:

text
GUqsH5y1j1M: 1288 comments, 65 pages (reached the end; 877 replies not fetched; YouTube shows 2165 in all)
https://www.youtube.com/watch?v=1C_zuHf6lP4: 1649 comments, 83 pages (reached the end; 710 replies not fetched; YouTube shows 2359 in all)
wrote 2937 comments to comments.csv

How deep newest and top go

We paged four public videos once per sort order. "Total" is commentsCount from the first page, and "Replies" is the sum of replyCount over the newest rows.

Video Total Newest Replies Top
GUqsH5y1j1M 2,165 1,288 877 991
1C_zuHf6lP4 2,359 1,649 710 962
rzmd7RouGrM 3,336 2,010 1,325 985
UF8uR6Z6KLc 29,253 3,000+ 1,212

On the first three videos newest ended on its own, and rows plus reply counts match YouTube's total within 1: 1,288 + 877 = 2,165 and 1,649 + 710 = 2,359. The replies are the missing part, 877 of 2,165 on the first video. On the 29,253-comment video we stopped newest at 150 pages, 3,000 comments with more available.

The default top order ends after 49 to 61 pages on every video, so send sort_order=newest for a complete list. Dates look derived from text such as 5 years ago, so older comments have an approximate day.

What it costs

A call returns about 20 comments for 1 credit, so 1,000 comments cost about 50 credits.

Job Google ToolzerHub
1,000 top-level comments 10 units about 50 credits
Video with 1,288 threads 13 units 65 credits
Allowance 10,000 units a day 100 credits a month

You get 100 free credits a month, about 2,000 comments; $19 buys 10,000 credits that never expire, and plans for higher volume cover more videos. Set your key and run the script on your first video.

For other YouTube limits see the YouTube search API quota, how to get a video's transcript and a channel's community posts.

We are not affiliated with Google or YouTube.

Get this data from your own code

The YouTube API returns it as JSON with one x-api-key header. 100 free credits monthly.