Extracting transcripts from YouTube videos is one of the most common tasks in AI and data engineering. Whether you are building a RAG pipeline, training a language model, or creating a content analysis tool, you need reliable access to video transcripts. In this tutorial, you will learn how to use the YouTubeTranscripts.co API to extract transcripts from any YouTube video using Python and the httpx library.
Prerequisites
Before you start, make sure you have Python 3.8 or later installed. You will also need an API key from YouTubeTranscripts.co. Sign up at youtubetranscripts.co/login to get 150 free requests, no credit card required.
Install Dependencies
We will use httpx, a modern HTTP client for Python. It supports async/await and is faster than the requests library for most use cases.
pip install httpxFetch Your First Transcript
The API is straightforward: make a GET request with your API key and the YouTube video URL. The response includes the video title, channel name, duration, and an array of transcript segments with text, start time, and duration.
import httpx
API_KEY = "YOUR_API_KEY"
VIDEO_URL = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
response = httpx.get(
"https://api.youtubetranscripts.co/v1/transcript",
params={"url": VIDEO_URL},
headers={"x-api-key": API_KEY},
)
response.raise_for_status()
data = response.json()
print(f"Title: {data['title']}")
print(f"Channel: {data['channel']}")
print(f"Duration: {data['duration']} seconds")
print(f"Segments: {len(data['transcript'])}")Get Plain Text Format
If you just need the full transcript as a single string (common for AI and NLP use cases), use the format=text parameter. This returns a text field with the complete transcript instead of individual segments.
response = httpx.get(
"https://api.youtubetranscripts.co/v1/transcript",
params={"url": VIDEO_URL, "format": "text"},
headers={"x-api-key": API_KEY},
)
data = response.json()
full_text = data["text"]
print(full_text[:500])Get SRT Subtitle Format
Need subtitles? Use format=srt to get the transcript in standard SRT subtitle format, ready for video editing software.
response = httpx.get(
"https://api.youtubetranscripts.co/v1/transcript",
params={"url": VIDEO_URL, "format": "srt"},
headers={"x-api-key": API_KEY},
)
data = response.json()
with open("subtitles.srt", "w") as f:
f.write(data["srt"])Error Handling
Always handle errors gracefully. The API returns standard HTTP status codes and descriptive error messages.
try:
response = httpx.get(
"https://api.youtubetranscripts.co/v1/transcript",
params={"url": VIDEO_URL},
headers={"x-api-key": API_KEY},
)
response.raise_for_status()
data = response.json()
except httpx.HTTPStatusError as e:
print(f"API error: {e.response.status_code} - {e.response.json()}")
except httpx.RequestError as e:
print(f"Network error: {e}")Conclusion
You now know how to extract YouTube transcripts in Python using the YouTubeTranscripts.co API. From here, you can build AI chatbots over video content, create transcript search engines, or feed transcripts into your data pipeline. Sign up at youtubetranscripts.co to get your API key and 150 free requests.
Ready to start extracting YouTube transcripts?
Get 150 free API requests. No credit card required.
Get Your Free API Key