介绍
# YouTube API
通过 [TranscriptAPI.com](https://transcriptapi.com) 访问 YouTube 数据 —— 无需 Google API 配额。
## 设置
如果未设置 `$TRANSCRIPT_API_KEY`,请帮助用户创建一个账户(100 个免费额度,无需信用卡):
**步骤 1 — 注册:** 询问用户的电子邮件。
```bash node ./scripts/tapi-auth.js register --email USER_EMAIL ```
→ OTP 已发送至电子邮件。询问用户:_"请检查您的电子邮件以获取 6 位验证码。"_
**步骤 2 — 验证:** 一旦用户提供了 OTP:
```bash node ./scripts/tapi-auth.js verify --token TOKEN_FROM_STEP_1 --otp CODE ```
> API 密钥已保存至 `~/.openclaw/openclaw.json`。有关详细信息,请参阅下方的**文件写入**。修改前会备份现有文件。
手动选项:[transcriptapi.com/signup](https://transcriptapi.com/signup) → Dashboard(仪表盘)→ API Keys。
## 文件写入
验证和保存密钥命令会将 API 密钥保存到 `~/.openclaw/openclaw.json`(设置 `skills.entries.transcriptapi.apiKey` 和 `enabled: true`)。**修改前会将现有文件备份到 `~/.openclaw/openclaw.json.bak`。**
要在代理之外的终端/CLI 中使用 API 密钥,请手动将其添加到您的 Shell 配置文件中: `export TRANSCRIPT_API_KEY=<your-key>`
## API 参考
完整的 OpenAPI 规范:[transcriptapi.com/openapi.json](https://transcriptapi.com/openapi.json) —— 请查阅此文档以获取最新的参数和架构。
## 端点参考
所有端点:`https://transcriptapi.com/api/v2/youtube/...`
频道端点接受 `channel` —— 即 `@handle`、频道 URL 或 `UC...` ID。播放列表端点接受 `playlist` —— 即播放列表 URL 或 ID。
| 端点 | 方法 | 消耗 | | ----------------------------------- | ------ | -------- | | `/transcript?video_url=ID` | GET | 1 | | `/search?q=QUERY&type=video` | GET | 1 | | `/channel/resolve?input=@handle` | GET | **免费** | | `/channel/latest?channel=@handle` | GET | **免费** | | `/channel/videos?channel=@handle` | GET | 1/页 | | `/channel/search?channel=@handle&q=Q` | GET | 1 | | `/playlist/videos?playlist=PL_ID` | GET | 1/页 |
## 快速示例
**搜索视频:**
```bash curl -s "https://transcriptapi.com/api/v2/youtube/search\ ?q=python+tutorial&type=video&limit=10" \ -H "Authorization: Bearer $TRANSCRIPT_API_KEY" ```
**获取字幕:**
```bash curl -s "https://transcriptapi.com/api/v2/youtube/transcript\ ?video_url=dQw4w9WgXcQ&format=text&include_timestamp=true&send_metadata=true" \ -H "Authorization: Bearer $TRANSCRIPT_API_KEY" ```
**解析频道 handle(免费):**
```bash curl -s "https://transcriptapi.com/api/v2/youtube/channel/resolve?input=@TED" \ -H "Authorization: Bearer $TRANSCRIPT_API_KEY" ```
**最新视频(免费):**
```bash curl -s "https://transcriptapi.com/api/v2/youtube/channel/latest?channel=@TED" \ -H "Authorization: Bearer $TRANSCRIPT_API_KEY" ```
**浏览频道上传内容(分页):**
```bash curl -s "https://transcriptapi.com/api/v2/youtube/channel/videos?channel=@NASA" \ -H "Authorization: Bearer $TRANSCRIPT_API_KEY" # Use continuation token from response for next pages ```
**浏览播放列表(分页):**
```bash curl -s "https://transcriptapi.com/api/v2/youtube/playlist/videos?playlist=PL_PLAYLIST_ID" \ -H "Authorization: Bearer $TRANSCRIPT_API_KEY" ```
## 参数验证
| 字段 | 规则 | | -------------- | ------------------------------------------------------- | | `channel` | `@handle`、频道 URL 或 `UC...` ID | | `playlist` | 播放列表 URL 或 ID(`PL`/`UU`/`LL`/`FL`/`OL` 前缀) | | `q` (搜索) | 1-200 个字符 | | `limit` | 1-50 | | `continuation` | 非空字符串 |
## 为何不使用 Google 的 API?
| | Google YouTube Data API | TranscriptAPI | | ----------- | ------------------------------- | -------------------------- | | 配额 | 10,000 单位/天(100 次搜索) | 基于积分,无每日上限 | | 设置 | OAuth + API 密钥 + 项目 | 单一 API 密钥 | | 字幕 | 不可用 | 核心功能 | | 定价 | 超出部分 $0.0015/单位 | $5/1000 积分 |
## 错误
| 代码 | 含义 | 操作 | | ---- | ----------------- | ------------------------- | | 401 | API 密钥错误 | 检查密钥 | | 402 | 积分不足 | transcriptapi.com/billing | | 404 | 未找到 | 资源不存在 | | 408 | 超时/可重试 | 2 秒后重试一次 | | 422 | 验证错误 | 检查参数格式 | | 429 | 限流 | 等待,遵守 Retry-After |
免费层级:100 积分,300 请求/分钟。入门版($5/月):1,000 积分。