Skip to main content
ScrapingRabbit

Blog · · 5 min read

Get YouTube Video Tags and Comments With an API

Read a video's tags and its top comments as JSON. Includes working code, how to page through more comments, and how to turn counts like 294K into numbers.

Two small pieces of YouTube data answer a lot of questions. The tags show which phrases a creator wants the video to be found for. The comments show what viewers actually think. This guide shows how to read both with plain GET requests, and how to handle the awkward parts of the answers.

Read a video's tags

The video endpoint returns the title, the counts and the tags in one answer. The tags are in a list called keywords. Every call needs an API key. New accounts get 100 free credits and a first key is created automatically on sign-up (it is shown once on the dashboard). Sign in at scrapingrabbit.com/login, then send the key in the x-api-key header (a Bearer token also works). The code below reads it from the SCRAPINGRABBIT_API_KEY environment variable, so no key is ever written into your code. The video is passed as its full URL in the url parameter, URL-encoded. A successful call costs 1 credit.

curl -H "x-api-key: $SCRAPINGRABBIT_API_KEY" "https://api.scrapingrabbit.com/v1/youtube/video?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ"

A shortened answer looks like this:

{
  "success": true,
  "data": {
    "video_id": "dQw4w9WgXcQ",
    "title": "Rick Astley - Never Gonna Give You Up (Official Video) (4K Remaster)",
    "views": "1841551413",
    "likes": "19M",
    "upload_date": "Oct 24, 2009",
    "duration": 213,
    "duration_formatted": "3:33",
    "keywords": ["rick astley", "Never Gonna Give You Up", "nggyu"]
  }
}

That video has 27 tags. Reading them in JavaScript takes a few lines:

const headers = { "x-api-key": process.env.SCRAPINGRABBIT_API_KEY };
const video = encodeURIComponent("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
const res = await fetch(`https://api.scrapingrabbit.com/v1/youtube/video?url=${video}`, { headers });
const { data } = await res.json();
console.log((data.keywords ?? []).join(", "));

Use ?? [] because a video without tags has nothing to read. Treat that as a normal result.

Read the top comments

The comments endpoint takes the same video URL in url. It also costs 1 credit per successful call.

curl -H "x-api-key: $SCRAPINGRABBIT_API_KEY" "https://api.scrapingrabbit.com/v1/youtube/comments?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ"

Each comment has these fields:

FieldMeaning
textThe comment itself
likesLike count, as text such as "294K"
repliesNumber of replies, as a number
published_dateRelative time such as "7 years ago"
channelThe author's handle and a link to their channel
comment_id and linkAn ID and a direct link to the comment

The answer also has total_comments, which is the number of comments on this page, and a pagination object.

Get more than one page

One request returns one page. In our test that was 20 comments. The answer includes pagination.next_page_token. To get the next page, send the same request again with that token in the optional next_page_token parameter. Pass the token back unchanged (URL-encode it, which encodeURIComponent does):

const headers = { "x-api-key": process.env.SCRAPINGRABBIT_API_KEY };
const video = encodeURIComponent("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
const base = `https://api.scrapingrabbit.com/v1/youtube/comments?url=${video}`;
const first = (await (await fetch(base, { headers })).json()).data;
const token = first.pagination?.next_page_token;

if (token) {
  const url = `${base}&next_page_token=${encodeURIComponent(token)}`;
  const second = (await (await fetch(url, { headers })).json()).data;
  console.log(second.comments.length, "more comments");
}

We ran exactly this through the keyed API. The second page held 20 different comments, with no overlap with the first, and it came with a new token, so you can keep going. Stop when there is no token or when you have enough. Each page is one call, so each page costs 1 credit.

The token is the only extra parameter this endpoint accepts. Any other extra parameter, or a token with characters a real token never has, returns INVALID_PARAMETERS and is not charged. A made-up token that merely looks valid is not refused: it returns a successful page with no comments, and that still costs 1 credit. So always pass back exactly the token the previous answer gave you.

Turn counts like 294K into numbers

Likes arrive as text, which is awkward for sorting. This helper converts the common forms:

function parseCount(text) {
  const match = /^([\d.,]+)\s*([KMB])?$/i.exec(String(text).trim());
  if (!match) return null;
  const n = parseFloat(match[1].replace(/,/g, ""));
  const multiplier = { K: 1e3, M: 1e6, B: 1e9 }[(match[2] ?? "").toUpperCase()] ?? 1;
  return Math.round(n * multiplier);
}

parseCount("294K"); // 294000
parseCount("1.2M"); // 1200000
parseCount("340");  // 340

It returns null for anything it does not recognize, so check for that before you sort. Remember that "294K" is rounded by YouTube, so the number you get is close, not exact.

With that helper you can find the comment with the most likes on a page:

const headers = { "x-api-key": process.env.SCRAPINGRABBIT_API_KEY };
const video = encodeURIComponent("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
const res = await fetch(`https://api.scrapingrabbit.com/v1/youtube/comments?url=${video}`, { headers });
const { data } = await res.json();

const top = [...data.comments].sort((a, b) => (parseCount(b.likes) ?? 0) - (parseCount(a.likes) ?? 0))[0];
console.log(top.likes, top.text);

In our test the most liked comment on the first page was not the first one in the list, so sorting is worth the extra step.

Things to plan for

  • Dates are relative. "7 years ago" is not a date you can store. If you need exact times, record when you fetched the comment.
  • Rate limits and credits apply. Each key is limited per minute: 10 requests on the free plan, 60 on Starter, 120 on Pro and 300 on Scale. Paging through many pages uses that quickly and costs 1 credit per page, so add a short pause between pages and cache what you fetch.
  • Check success first. A failed request returns success: false, a code and a message, and is not charged. Read data only after you have checked.
  • Comments may be off. If the creator turned comments off, expect no comments to read.

Try it without code

The free tag extractor works in the browser with no key. It shows a video's tags and copies them in one click. The free comment extractor shows the first page of comments with likes and replies. Both use the endpoints described here.

Try it yourself

Frequently asked questions

Does the comments endpoint return every comment on a video?

No. Each request returns one page, 20 comments in our test. The answer includes a next_page_token. Pass it back in the optional next_page_token parameter to get the next page. Each page costs 1 credit.

Why are likes a string like 294K and not a number?

YouTube shows large counts in a short form, and the API passes them on as text. Convert them yourself, the helper in this guide shows how.

Why does a video return no tags?

The creator did not add any, or the video is private or unavailable. An empty list is a normal answer, not an error.

More guides