# Transcripts end to end

> Fetch a show's transcripts, request the missing ones, and collect them as they arrive.

Source: https://www.podcastlookup.com/documentation/guides/transcripts

This guide builds a small script that, for one podcast:

1. lists its recent episodes,
2. downloads every transcript that exists,
3. requests the missing ones,
4. and, on later runs, picks up the ones that became available.

### Set up

```bash
mkdir transcripts && cd transcripts
export PODCAST_LOOKUP_API_KEY="pl_…"
```

A tiny client that sends the key and raises readable errors:

```ts title="lookup.ts"
const BASE = "https://www.podcastlookup.com/api/v1"

export async function lookup<T>(path: string, init: RequestInit = {}): Promise<T> {
  const response = await fetch(`${BASE}${path}`, {
    ...init,
    headers: { Authorization: `Bearer ${process.env.PODCAST_LOOKUP_API_KEY}` },
  })
  const body = await response.json()
  if (!response.ok) throw Object.assign(new Error(body.error.message), { code: body.error.code })
  return body
}
```

### List recent episodes

```ts
const { episodes } = await lookup<EpisodeList>(`/podcasts/${podcastId}/episodes?page_size=20`)
```

Each episode's `transcript` field says whether a transcript exists, so you don't spend a credit on episodes that have none.

### Fetch or request

```ts title="sync.ts"
import { existsSync, writeFileSync } from "node:fs"
import { lookup } from "./lookup"

const podcastId = 8017674

const { episodes } = await lookup<any>(`/podcasts/${podcastId}/episodes?page_size=20`)

for (const episode of episodes) {
  const file = `${episode.id}.json`
  if (existsSync(file)) continue // already saved: transcripts never change

  if (episode.transcript) {
    const transcript = await lookup(`/podcasts/${podcastId}/episodes/${episode.id}/transcript`)
    writeFileSync(file, JSON.stringify(transcript, null, 2))
    console.log("saved", episode.title)
  } else if (episode.audio_url) {
    const { request_count } = await lookup<any>(
      `/podcasts/${podcastId}/episodes/${episode.id}/transcription-request`,
      { method: "POST" },
    )
    console.log("requested", episode.title, `(${request_count} requests)`)
  }
}
```

### Run it daily

Run it once a day (cron, a scheduled CI job…). Each run lists episodes (1 credit), downloads new transcripts (1 credit each) and re-requests the missing ones, which moves them up the queue.

```bash
0 7 * * * cd ~/transcripts && npx tsx sync.ts >> sync.log 2>&1
```

## Cost

For 20 episodes with 15 transcribed: 1 (list) + 15 (transcripts) + 5 (requests) = **21 credits** the first day, then **1 credit plus one per new transcript or request** each day after.

> **Note: Publisher transcripts**
>
> When `transcript.source` is `publisher`, the transcript comes from the show's own feed: same segments, but speakers may be unnamed (`Speaker 1`, or `null`) and, when `timed` is `false`, timestamps are `null`. Handle both if you print `[hh:mm:ss] Speaker:` lines.