Skip to main content
ScrapingRabbit

Blog · · 4 min read

How to Get a YouTube Transcript With an API

Get the full transcript of any YouTube video as JSON with one GET request. Working curl, JavaScript and Python examples, plus what to do when a video has no captions.

A YouTube transcript is the text of what is said in a video, split into short lines with a start time. If you want to search a lecture, draft an article from a talk, or feed spoken content into your own tool, you need that text as data. This guide shows how to get it with one request.

What you get back

You send the video URL and receive JSON. Each line of the transcript has three fields:

FieldMeaning
startSeconds from the start of the video when the line begins
durationHow many seconds the line stays on screen
textThe words of that line

Next to the lines, the answer also includes the videoId, the language of the captions, and isAutoGenerated, which is false when a person wrote the captions and true when YouTube generated them.

Make the request

Pass the full video link in the url parameter, URL-encoded. For example, https://www.youtube.com/watch?v=dQw4w9WgXcQ becomes https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ. 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. A successful call costs 1 credit.

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

The same request in JavaScript:

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

And in Python:

import os
import requests

res = requests.get(
    "https://api.scrapingrabbit.com/v1/youtube/transcript",
    params={"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"},
    headers={"x-api-key": os.environ["SCRAPINGRABBIT_API_KEY"]},
    timeout=60,
)
data = res.json()["data"]
print(data["language"], data["isAutoGenerated"])

A real answer

This is a shortened answer for the video above. We cut the list to three lines:

{
  "success": true,
  "data": {
    "videoId": "dQw4w9WgXcQ",
    "transcript": [
      { "start": 1.36, "duration": 1.68, "text": "[♪♪♪]" },
      { "start": 18.64, "duration": 3.24, "text": "♪ We're no strangers to love ♪" },
      { "start": 22.64, "duration": 4.32, "text": "♪ You know the rules and so do I ♪" }
    ],
    "language": "en",
    "isAutoGenerated": false
  }
}

Turn the lines into plain text

Most uses need one block of text, not a list of lines. Join the text fields:

const text = data.transcript.map((line) => line.text).join(" ");

If you want timestamps in the output, format start yourself. Divide by 60 for minutes and keep the remainder for seconds.

Handle videos that cannot be read

Not every video has a transcript, and not every link is valid. When something goes wrong the answer has success set to false, a code and a message you can log or show. For example, asking for a video that does not exist returns a message saying the video is no longer available or has been removed. Failed calls are not charged.

Always check success before you read data. Read the response body once into a variable, check it, and only then take data from it. A response body can only be read once, so use this in place of the destructuring line in the first example, not after it:

const body = await res.json();
if (!body.success) {
  console.warn("No transcript:", body.code, body.message);
  process.exit(1);
}
const { data } = body;

Treat "no transcript" as a normal outcome, not a crash. Plan for it in your interface, for example by showing a note that the video has no captions.

Keep requests polite

Each key is limited per minute: 10 requests on the free plan, 60 on Starter, 120 on Pro and 300 on Scale. If you go over, you get a 429 status with the code RATE_LIMITED and a Retry-After header that says how many seconds to wait. Two habits help, and both also save credits:

  • Cache results. A transcript for a published video rarely changes, so store it and reuse it instead of paying for it again.
  • Add a short pause between requests when you process a list of videos, sized to your plan's per-minute limit.

Try it without writing code

If you only need one transcript, the free tool does the same job in the browser with no key. Paste a link, copy the text with or without timestamps, and you are done. To call it from your own code, sign in for an API key: new accounts get 100 free credits, and the pricing page lists the plans.

Try it yourself

Frequently asked questions

Do I need a YouTube account or an API key from Google?

No. The endpoint reads the public caption track of a video, so you do not need a Google account or a YouTube Data API key.

Why does the same video sometimes return no transcript?

A transcript only exists when the video has a caption track. Creators can turn captions off, and very new or music-only videos may have none.

Is the transcript text exact?

Not always. When captions are auto-generated, names and technical terms can be wrong. The isAutoGenerated field tells you which kind you got.

More guides