Get YouTube transcripts from AWS Lambda
A Lambda function can request transcript text from a hosted API over HTTPS. If an extraction script works on your laptop and fails after deployment, check the returned error before changing the function’s infrastructure. A refused request, a missing caption language and an unavailable video need different responses.
Start with the application contract
Keep video ids in the input event, store your VidWords token in your function’s environment or secret-management setup, and give the invocation enough time to receive a response. Do not log credentials. For larger workloads, split inputs into batches and keep successful results so you can resume.
Python Lambda handler
Set VIDWORDS_TOKEN in the function configuration and invoke it with an event such as {"video_ids":["dQw4w9WgXcQ"]}. The handler uses only Python’s standard library.
import json, os, urllib.request
def get_transcripts(video_ids, language=None):
payload = {"ids": video_ids}
if language:
payload["lang"] = language
request = urllib.request.Request(
"https://vidwords.com/api/transcripts",
data=json.dumps(payload).encode(),
headers={"Authorization": "Basic " + os.environ["VIDWORDS_TOKEN"],
"Content-Type": "application/json"},
)
with urllib.request.urlopen(request, timeout=120) as response:
return json.load(response)["results"]
def handler(event, context):
results = get_transcripts(event["video_ids"], event.get("lang"))
return {
"transcripts": [r for r in results if "error" not in r],
"failed": [r for r in results if "error" in r],
}Handle deployment failures clearly
- A connection or invocation timeout is different from a per-video error returned by the API.
- Validate input before sending a request; do not retry invalid ids.
- Retain failed ids and their messages, and retry temporary failures with a bounded policy.
- Use your application’s existing queue or job system for work that should outlive one invocation.
Limits and failed lookups
The REST API accepts up to 50 video ids per call. A delivered caption transcript uses one Cloud Request. A free account includes 100 one-time Developer Requests; see current plans and top-ups for continued use. Audio transcription requires separate AI Units and explicit consent through the API’s allowAsr option.
Read each result’s error field: one unavailable video does not make the rest of a batch fail. A lookup that returns no transcript is not charged. Hosted retrieval can still be refused by YouTube; it does not promise access to private, restricted or unavailable videos. See the API reference for response fields and limits.