All guides

October 2, 2026 · 6 min read

How to Get YouTube Transcripts in Python (with an API)

Getting a YouTube transcript in Python takes one HTTP request. This guide uses the TranscriptYT API with the requests library to pull timestamped segments, save subtitle files, translate captions, and handle the failures you will see in production.

Get an API key

Sign in at transcript-yt.com with Google and create a key in the dashboard. The free tier includes 100 transcripts with no card. Store the key in an environment variable rather than in your source code.

pip install requests
export TRANSCRIPTYT_KEY="ts_live_..."

Fetch a transcript as JSON

Pass a YouTube URL (watch, youtu.be, shorts, or embed) or a bare video ID as the url parameter. The JSON response includes the video title, channel, duration, the delivered language, timestamped segments, and the full text.

The source field tells you whether the text came from the video's captions or from AI transcription of the audio, which TranscriptYT uses automatically when a video has no captions.

import os
import requests

API = "https://transcript-yt.com/v1/transcript"
HEADERS = {"Authorization": f"Bearer {os.environ['TRANSCRIPTYT_KEY']}"}

res = requests.get(API, params={"url": "https://youtu.be/dQw4w9WgXcQ"}, headers=HEADERS, timeout=120)
res.raise_for_status()
data = res.json()

print(data["title"], data["language"], data["source"])
for seg in data["segments"][:3]:
    print(f"{seg['start']:>7.1f}s  {seg['text']}")

Save SRT or VTT subtitle files

Set format to srt, vtt, text, csv, or md and the API returns the file body directly, so you can write it to disk without any conversion code. Every format costs the same single credit.

res = requests.get(
    API,
    params={"url": "dQw4w9WgXcQ", "format": "srt"},
    headers=HEADERS,
    timeout=120,
)
res.raise_for_status()
with open("captions.srt", "w", encoding="utf-8") as f:
    f.write(res.text)

Choose or translate the language

Use language to pick a caption track by BCP-47 code. If the video has no track in that language, the original is machine-translated and translated is true in the response. Use translate_to to translate any track into one of 150+ languages while keeping every segment's timing.

To see which tracks exist before spending a credit, call GET /v1/languages, which is free.

spanish = requests.get(
    API,
    params={"url": "dQw4w9WgXcQ", "translate_to": "es", "format": "vtt"},
    headers=HEADERS,
    timeout=120,
).text

tracks = requests.get(
    "https://transcript-yt.com/v1/languages",
    params={"url": "dQw4w9WgXcQ"},
    headers=HEADERS,
).json()["languages"]

Handle errors and retries

Errors share one envelope, {"error": {"code", "message"}}, so branch on the code rather than the message. BLOCKED, RATE_LIMITED, and UPSTREAM_ERROR are temporary and worth retrying with backoff. VIDEO_UNAVAILABLE, NO_CAPTIONS, and LANGUAGE_NOT_AVAILABLE are final answers for that video. Running out of credits returns a 402 with an upgrade_url.

Failed requests are never billed, so retrying costs nothing until one succeeds.

import time

RETRYABLE = {"BLOCKED", "RATE_LIMITED", "UPSTREAM_ERROR"}

def get_transcript(video, **params):
    for attempt in range(3):
        res = requests.get(API, params={"url": video, **params}, headers=HEADERS, timeout=120)
        if res.ok:
            return res.json()
        if res.status_code == 402:
            raise RuntimeError(f"Out of credits: {res.json()['upgrade_url']}")
        error = res.json()["error"]
        if error["code"] not in RETRYABLE:
            raise RuntimeError(f"{error['code']}: {error['message']}")
        time.sleep(2 ** attempt)
    raise RuntimeError("Gave up after 3 attempts")

API or the youtube-transcript-api library?

The open-source youtube-transcript-api package is a fine choice for scripts and notebooks on your own machine. In production on cloud servers, YouTube blocks most datacenter IPs, so self-hosting it usually means also running a proxy pool. A hosted API handles that, adds SRT/VTT export and AI transcription for videos without captions, and works the same from any language.

Try the TranscriptYT API

Get transcripts as JSON, text, SRT, or VTT. 100 free credits, no card required.

More guides