DEV Community

slurpsyrup
slurpsyrup

Posted on Originally published at fetchproof.com

How to export YouTube comments to CSV without an API key (Python)

YouTube has no export button for comments. The official route is the YouTube Data API's commentThreads.list, which needs a Google Cloud project, an API key and a share of a daily quota. If you just want a video's comments as rows in a spreadsheet, one GET returns the first page as JSON:

export FETCHPROOF_API_KEY=fp_live_xxx

curl -s "https://api.fetchproof.com/v1/youtube/video/comments?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ&order=top" \
  --header "x-api-key: $FETCHPROOF_API_KEY"
Enter fullscreen mode Exit fullscreen mode

url is the video's watch URL and the only required parameter. order is top or newest. The response is a page of top-level comments plus a continuationToken for the next page, and each comment carries its own token for its replies.

Why not the official API

The Data API works, and for an app that has to stay inside Google's terms it is the right choice. The costs show up when the job grows:

  • Setup. You need a Google Cloud project, the API enabled and a key before the first request.
  • Quota. commentThreads.list costs 1 unit per call against a default of 10,000 units a day shared by most methods. At most 100 threads come back per call.
  • Replies. A thread carries only some replies. Google's guide sends you to comments.list for the full set, one paged call per thread.

Scraping a watch page directly works on a laptop, but on a cloud server YouTube is much less patient with datacenter IPs, and long runs tend to end in a bot check or HTTP 429.

The comments endpoint reads the public comment section and returns structured JSON. No Google project, no quota, no proxy pool. The credential is the Fetchproof key in x-api-key.

What comes back

{
  "success": true,
  "credits_remaining": 99,
  "credits_charged": 1,
  "comments": [
    {
      "id": "Ugz_xxx",
      "content": "Example comment text",
      "publishedTimeText": "3 months ago",
      "publishedTime": "2026-09-03T12:00:00.000Z",
      "replyLevel": 0,
      "author": {
        "name": "@example_viewer",
        "channelId": "UC_xxx",
        "isVerified": false,
        "isCreator": false,
        "avatarUrl": "https://yt3.ggpht.com/example",
        "channelUrl": "https://www.youtube.com/@example_viewer"
      },
      "engagement": { "likes": 9300, "replies": 66 },
      "repliesContinuationToken": "Eg0S_xxx"
    }
  ],
  "continuationToken": "Eg0S_yyy"
}
Enter fullscreen mode Exit fullscreen mode

The fields that matter for an export:

  • engagement.likes and engagement.replies are integers, so "9.3K" already arrives as a sortable number.
  • author.channelId is the commenter's stable ID. author.name is the handle, which can change. isCreator marks the video owner's comments.
  • publishedTime is an estimate converted from relative text like "3 months ago". Keep publishedTimeText next to it.
  • repliesContinuationToken is the key to a comment's replies. It is null when there are none.
  • continuationToken at the top level is the next page. It is null on the last page.

The endpoint's ceiling is about 1,000 comments in top order and about 7,000 in newest order.

Python: every comment and reply, to CSV

The standard library is enough. Keep the key in the environment.

import csv
import json
import os
import urllib.parse
import urllib.request

VIDEO = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
HEADERS = {"x-api-key": os.environ["FETCHPROOF_API_KEY"]}


def get(url):
    request = urllib.request.Request(url, headers=HEADERS)
    with urllib.request.urlopen(request, timeout=60) as response:
        return json.load(response)


def comment_page(token=None):
    url = "https://api.fetchproof.com/v1/youtube/video/comments?url=" + urllib.parse.quote(VIDEO, safe="") + "&order=newest"
    if token:
        url += "&continuationToken=" + urllib.parse.quote(token, safe="")
    return get(url)


def replies(token):
    while token:
        body = get("https://api.fetchproof.com/v1/youtube/video/comment/replies?continuationToken=" + urllib.parse.quote(token, safe=""))
        yield from body["comments"]
        token = body.get("continuationToken")


def row(comment, parent_id=""):
    return {
        "id": comment["id"],
        "parent_id": parent_id,
        "author": comment["author"]["name"],
        "channel_id": comment["author"]["channelId"],
        "likes": comment["engagement"]["likes"],
        "published": comment["publishedTime"],
        "published_text": comment["publishedTimeText"],
        "text": comment["content"],
    }


rows, token = [], None
while True:
    page = comment_page(token)
    for comment in page["comments"]:
        rows.append(row(comment))
        for reply in replies(comment.get("repliesContinuationToken")):
            rows.append(row(reply, parent_id=comment["id"]))
    token = page.get("continuationToken")
    if not token:
        break

with open("comments.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=list(rows[0]) if rows else ["id"])
    writer.writeheader()
    writer.writerows(rows)
print(len(rows), "comments and replies written to comments.csv")
Enter fullscreen mode Exit fullscreen mode

Each page of comments is one call, and so is each page of replies. Pass a continuationToken back unchanged and stop when it is null. A video with comments turned off returns an empty list and a null token, so the script writes a header-only file. Open comments.csv as UTF-8 because comments are full of emoji and non-Latin scripts.

To skip replies and keep the export cheap, delete the inner for reply loop.

Node: top comments for a quick look

const video = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
const response = await fetch(
  `https://api.fetchproof.com/v1/youtube/video/comments?url=${encodeURIComponent(video)}&order=top`,
  { headers: { "x-api-key": process.env.FETCHPROOF_API_KEY } },
);
if (!response.ok) {
  throw new Error(`YouTube comments failed: HTTP ${response.status}`);
}
const { comments, continuationToken } = await response.json();

for (const c of comments.toSorted((a, b) => b.engagement.likes - a.engagement.likes).slice(0, 10)) {
  console.log(c.engagement.likes, c.author.name, c.content.slice(0, 80));
}
console.log(`next page: ${continuationToken ? "yes" : "none"}`);
Enter fullscreen mode Exit fullscreen mode

toSorted needs Node 20. On Node 18, use [...comments].sort(...).

From n8n or an MCP client

In n8n, use the HTTP Request node with the same x-api-key header. Point it at /v1/youtube/video/comments, pass url and order as query parameters, and use pagination to send continuationToken back until it comes back empty. From an MCP client the tools are youtube_video_comments and youtube_video_comment_replies.

What a call costs

Each page of comments costs 1 credit, and so does each page of replies. Five pages of comments plus three threads with one page of replies each comes to 8 credits. Errors, blocked or timed-out requests, invalid requests and cache hits cost nothing. New accounts get 100 free credits, no card.

FAQ

Can I get YouTube comments without an API key? Not from Google's API. This endpoint needs only a Fetchproof key and reads the same public comments a logged-out visitor sees.

Does it include replies? Yes, in a second call per thread, using repliesContinuationToken.

How many comments per video? About 1,000 in top order and about 7,000 in newest order.

Shorts and live replays? Yes. Pass the video's URL as url.

Originally published at fetchproof.com.

Top comments (0)