|
| 1 | +# TranscriptFetch Python SDK |
| 2 | + |
| 3 | +Official, typed Python client for the [TranscriptFetch](https://transcriptfetch.com) API: fetch YouTube transcripts, channels, playlists, and search results as clean, structured data. Sync + async, fully type-hinted. |
| 4 | + |
| 5 | +```bash |
| 6 | +pip install transcriptfetch-sdk |
| 7 | +``` |
| 8 | + |
| 9 | +## Quickstart |
| 10 | + |
| 11 | +```python |
| 12 | +from transcriptfetch import TranscriptFetch |
| 13 | + |
| 14 | +# api_key falls back to the TRANSCRIPTFETCH_API_KEY env var |
| 15 | +tf = TranscriptFetch(api_key="tf_live_...") |
| 16 | + |
| 17 | +t = tf.transcripts.video("https://youtu.be/aircAruvnKk") |
| 18 | +print(t.title) |
| 19 | +print(t.text) |
| 20 | +for seg in t.segments: |
| 21 | + print(f"[{seg.start:.1f}] {seg.text}") |
| 22 | + |
| 23 | +print("credits left:", t.usage.balance) |
| 24 | +``` |
| 25 | + |
| 26 | +Get an API key (100 free credits) at <https://transcriptfetch.com/app>. One credit per successful fetch; failed/blocked/no-transcript requests are free. |
| 27 | + |
| 28 | +## Endpoints |
| 29 | + |
| 30 | +```python |
| 31 | +tf.transcripts.video(video) # single transcript (text + segments) |
| 32 | +tf.transcripts.channel(channel, limit=, cursor=) # a channel's videos (metadata) |
| 33 | +tf.transcripts.playlist(playlist, limit=, cursor=) # a playlist's videos |
| 34 | +tf.transcripts.search(query, limit=, cursor=) # search YouTube |
| 35 | +tf.transcripts.batch(video_ids) # up to 50 transcripts in one call |
| 36 | +tf.health() # unauthenticated liveness probe |
| 37 | +``` |
| 38 | + |
| 39 | +`video`/`channel`/`playlist` accept URLs or raw IDs (normalized automatically). |
| 40 | + |
| 41 | +## Pagination |
| 42 | + |
| 43 | +List endpoints are cursor-paginated. Iterate every result without managing cursors: |
| 44 | + |
| 45 | +```python |
| 46 | +for video in tf.transcripts.iter_channel("@lexfridman", limit=10): |
| 47 | + print(video.video_id, video.title) |
| 48 | +``` |
| 49 | + |
| 50 | +Or page manually via `page.next_cursor` and the `cursor=` argument. |
| 51 | + |
| 52 | +## Async |
| 53 | + |
| 54 | +```python |
| 55 | +import asyncio |
| 56 | +from transcriptfetch import AsyncTranscriptFetch |
| 57 | + |
| 58 | +async def main(): |
| 59 | + async with AsyncTranscriptFetch() as tf: |
| 60 | + t = await tf.transcripts.video("aircAruvnKk") |
| 61 | + print(t.text) |
| 62 | + async for v in tf.transcripts.iter_search("how transformers work", limit=10): |
| 63 | + print(v.title) |
| 64 | + |
| 65 | +asyncio.run(main()) |
| 66 | +``` |
| 67 | + |
| 68 | +## Errors |
| 69 | + |
| 70 | +All errors subclass `TranscriptFetchError`. API errors carry `.status`, `.code`, `.message`, and `.request_id`: |
| 71 | + |
| 72 | +```python |
| 73 | +from transcriptfetch import ( |
| 74 | + AuthenticationError, InsufficientCreditsError, InvalidRequestError, |
| 75 | + RateLimitError, IdempotencyConflictError, UpstreamUnavailableError, |
| 76 | + InternalServerError, APIError, APIConnectionError, APITimeoutError, |
| 77 | +) |
| 78 | + |
| 79 | +try: |
| 80 | + tf.transcripts.video("bad") |
| 81 | +except InsufficientCreditsError: |
| 82 | + ... # 402: top up at /pricing |
| 83 | +except RateLimitError as e: |
| 84 | + print(e.retry_after) # 429 |
| 85 | +except APIError as e: |
| 86 | + print(e.status, e.code, e.request_id) |
| 87 | +``` |
| 88 | + |
| 89 | +## Reliability |
| 90 | + |
| 91 | +- **Automatic retries** on `429` (honoring `Retry-After`) and `5xx`, with exponential backoff + jitter (`max_retries=2` by default). |
| 92 | +- **Idempotency**: every write auto-sends an `Idempotency-Key` so a retried request is never double-charged. Override per call with `idempotency_key=...`. |
| 93 | +- **Configurable:** `TranscriptFetch(api_key=..., base_url=..., timeout=30, max_retries=2)`. Both clients are context managers and accept a custom `http_client=` (httpx). |
| 94 | + |
| 95 | +## Development |
| 96 | + |
| 97 | +```bash |
| 98 | +pip install -e ".[dev]" |
| 99 | +ruff check . && mypy src && pytest |
| 100 | +``` |
| 101 | + |
| 102 | +Tests are fully mocked (no network). MIT licensed. |
0 commit comments