self-hosted Webtor 的 /api/v1 API 参考:用 Bearer 密钥管理媒体库并获取签名下载链接
self-hosted Webtor 的 /api/v1 API 参考:用 Bearer 密钥管理媒体库并获取签名下载链接
self-hosted Webtor 是 webtor.io 的一体化 Docker 自托管版本,本文带你用 Bearer API 密钥调用它的 /api/v1 接口,保存媒体库资源、列出文件,并获取签名下载链接与流媒体地址。适合刚入门的自托管用户。
为什么是 /api/v1?🔐
早期的 rest-api 服务如今不再对外暴露——nginx 已经不再代理 /rest-api/,任何人都不能绕过 Web 界面直接读写媒体库。现在唯一的入口是 web-ui 自家的 /api/v1,它要求每个请求都携带一把 API 密钥:
curl -H "Authorization: Bearer <your-api-key>" http://localhost:8080/api/v1/resource/<id>
这个设计很关键:
/rest-api/之所以关闭,是因为它本身没有鉴权,一旦暴露公网就允许"存一个 torrent、拿到内容签名链接"的越权行为。换成需要密钥的/api/v1之后才安全。
/api/v1 的路径与旧的 /rest-api/ 一一对应,返回的 JSON 结构也完全一致——你只需要记住:主机路径前缀变了,并必须带认证头。
一键生成你的 Bearer API 密钥
获取密钥很简单,无需改任何代码:
- 用浏览器登录你的实例(如果设置了
ADMIN_PASSWORD/ONLY_AUTHORIZED,需要先登录)。 - 打开个人主页(profile page),在 API 凭据区域生成一把 API credential。
- 记下这把密钥,之后所有请求都用
Authorization: Bearer <your-api-key>携带它。
如果你忘了怎么改密码或重置访问,可用容器内自带的 admin 子命令(详见 README.md 的 Administrator Password 一节)。
核心端点速查 📋
| 方法 | 路径 | 作用 |
|---|---|---|
POST |
/api/v1/resource |
保存一个 torrent(.torrent 文件或 magnet 链接)到媒体库,返回资源 id |
GET |
/api/v1/resource/<id> |
读取该资源信息 |
GET |
/api/v1/resource/<id>/list?path=/ |
列出资源内的文件 / 目录 |
GET |
/api/v1/resource/<id>/export/<content_id> |
获取某个文件的签名下载 / 流媒体链接 |
GET |
/api/v1/library |
列出媒体库里的全部条目 |
POST |
/api/v1/vault/pledges |
将 torrent 内容永久保存到内置 S3(Vault) |
GET |
/api/v1/docs/index.html |
在线 Swagger 文档(无需密钥,可直接打开) |
保存媒体库资源:POST /resource
把 .torrent 文件或 magnet 链接提交进来,就会得到它的资源 id:
curl -H "Authorization: Bearer <your-api-key>" \
-H "Content-Type: application/x-bittorrent" \
--data-binary @my.torrent \
http://localhost:8080/api/v1/resource
返回体形如 { "id": "<infohash>", "multi_file": true, "size": 12345, "files_count": 3, ... },这个 id(即 infohash)是后续所有操作的凭据。
⚠️ 一个容易踩的坑:
POST /resource必须声明非表单的Content-Type(如application/x-bittorrent或application/json)。如果让 curl 默认成application/x-www-form-urlencoded,web-ui 的全局 CSRF 中间件会先把请求体当表单解析掉,结果就是400 "the body must be a .torrent file or a magnet uri"。
保存动作是幂等的:同一个 torrent 重复提交会返回同一个 id,不会产生重复条目。
列出文件并获取签名下载 / 流媒体链接 📥
拿到 id 后,先列出资源内的内容(path=/ 会返回该目录树下的文件):
curl -H "Authorization: Bearer <your-api-key>" \
"http://localhost:8080/api/v1/resource/<id>/list?path=/"
返回是 { "items": [ { "id": "...", "name": "video.mp4", "type": "file", ... } ] } 结构。从中挑出某个文件的 content_id,再去换它的签名链接:
curl -H "Authorization: Bearer <your-api-key>" \
"http://localhost:8080/api/v1/resource/<id>/export/<content_id>"
返回体在 exports 字段下提供两类地址:
exports.download.url—— 直接下载(DDL),单个文件可直接下载;对目录则会打包成 ZIP(保留目录结构)。exports.stream.url—— 流媒体地址(HLS 的index.m3u8或字幕的 WebVTT),可边下边播,无需先下完整个 torrent。
这些链接都带有 ?api-key=...&token=... 的签名查询串,由 torrent-http-proxy 签发,直接交给 curl 或播放器即可取流:
curl -o video.mp4 "<exports.download.url>"
管理媒体库列表:GET /library
想看整个媒体库(电影、剧集都会被自动识别归类),调用:
curl -H "Authorization: Bearer <your-api-key>" \
"http://localhost:8080/api/v1/library"
默认返回 { "type": "all", "items": [ ... ] },type 字段会原样回显查询参数(默认 all)。这是把媒体库接入自己脚本最直接的读接口。
永久保存:保存到 Vault 💾
如果担心 torrent 的 swarm 变冷后无法再取,可以把内容永久存入实例内置的 S3 存储(Vault):
# 1) 发起保存(JSON 体,resource_id 即 POST /resource 返回的 id)
curl -H "Authorization: Bearer <your-api-key>" \
-H "Content-Type: application/json" \
-d '{"resource_id":"<id>"}' \
http://localhost:8080/api/v1/vault/pledges
# 2) 轮询保存状态,直到 vaulted 变为 true
curl -H "Authorization: Bearer <your-api-key>" \
"http://localhost:8080/api/v1/vault/pledges/<id>"
保存是异步的:vault 会经 torrent-http-proxy 拉取内容、校验每一个 piece、上传到 /storage/vault,再通知 web-ui。自托管版本没有订阅档位,Vault 点数不限量,真正的上限只是磁盘空间。
在线查阅完整 API 文档 📚
/api/v1 自带可交互的 Swagger 文档,无需任何密钥即可打开:
- 文档页面:
http://localhost:8080/api/v1/docs/index.html - 原始规范:
http://localhost:8080/api/v1/swagger.json(可直接curl下来看paths)
这是核对参数与返回结构的权威来源,建议先收藏。
常见错误与排查 🔧
/api/v1 的鉴权失败会返回统一的 {"error": {"code": ..., "message": ...}} 信封:
| 状态码 | 含义 | 典型 error.code |
|---|---|---|
401 |
完全没有携带密钥(Authorization 头缺失) |
unauthorized |
403 |
密钥格式正确但从未签发 / 无权限 | forbidden(message 常为 "this key is not allowed to use the API") |
几个高频排错点:
- 401 → 检查是否漏写了
Authorization: Bearer <key>,或密钥是从旧容器复制来的(重建容器后密钥可能变化)。 - 403 → 密钥形如 UUID 但不是本实例签发的;去 profile 重新生成一把。
- 400 "the body must be a .torrent file or a magnet uri" →
POST /resource的Content-Type被默认成了表单编码,按上文改成非表单类型即可。 - 签名链接打不开 / 429 → 这些链接是路径无关的签名,换
~vod、~hls、~vtt等"matryoshka 链路段"时要保留原查询串;跨实例直连时可能触发会话 IP 校验而返回429。
相关文件资料
想了解这些行为的实现与验证,可参考仓库内的以下资料:
- API 鉴权契约(401/403/成功)的端到端验证:tests/scenarios/90-api.sh
- 共享的
apiv1请求辅助函数(Bearer 头 +Content-Type说明):tests/lib.sh - 保存 → 列表 → 签名下载全链路(DDL):tests/scenarios/10-ddl.sh
- 目录打包 ZIP 下载(torrent-archiver):tests/scenarios/20-archive.sh
- HLS 流媒体导出:tests/scenarios/30-hls.sh
- 保存到 Vault 的完整流程:tests/scenarios/70-vault.sh
- 用官方 webtor-cli 调用
/api/v1:tests/scenarios/92-cli.sh - 路由与
/rest-api/关闭的回归守卫:etc/nginx/conf/nginx.template.conf - 环境变量(
ADMIN_PASSWORD、ONLY_AUTHORIZED等)总览:README.md
提示:这些场景脚本也是现成的"活文档"——照着它们的
curl顺序,就能把/api/v1的每个端点跑通一遍。