Get a YouTube transcript with Python
This tutorial saves one transcript response to a JSON file using Python’s standard library. You need a FetchFox project key and a video URL you are permitted to process.
Prepare your environment
Create an account, reveal your project key in the dashboard, and set FETCHFOX_API_KEY in your server or terminal environment. Set VIDEO_URL to a complete YouTube video URL. Do not commit either a real key or a transcript containing sensitive material to a public repository.
Request and save the transcript
The code reads the key from your environment and checks both HTTP and application success. It writes the original response so you retain the timestamped segments as well as the full text.
import json, os
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from urllib.error import HTTPError
query = urlencode({"url": os.environ["VIDEO_URL"]})
request = Request("https://api.fetchfox.net/youtube/transcript?" + query,
headers={"x-access-key": os.environ["FETCHFOX_API_KEY"]})
try:
with urlopen(request, timeout=130) as response:
result = json.load(response)
except HTTPError as error:
raise SystemExit(f"HTTP {error.code}; inspect limits before retrying")
if not result.get("success"):
raise SystemExit("Transcript request was unsuccessful")
with open("transcript.json", "w", encoding="utf-8") as output:
json.dump(result, output, ensure_ascii=False, indent=2)
print(result["data"]["transcript"])Use the output and handle errors
Iterate over data.transcriptSegments when you need timed cues. Pass the saved JSON to the free formatter for CSV, plain text, SRT or VTT. A successful request costs one credit. There is no separate monthly request-attempt allowance.
HTTP 429 means stop and respect Retry-After. Authentication or validation failures require correcting the request. Missing captions are not solved by unbounded retries; retain the failed source URL for later review.