优先级顺序:
.env 文件(默认)CLAUDE.md 文件bash
# .env 文件格式
QB_URL="http://192.168.31.88:8080"
QB_USER="admin"
QB_PASS="123123"
注意: 执行任何 API 操作前,先检查
.env文件是否存在并包含配置。如果缺少配置,询问用户。
qBittorrent 使用 cookie-based 认证:
bash
# 配置(从 .env 读取)
source .env 2>/dev/null || true
COOKIE_FILE="/tmp/qb_cookies.txt"
# 登录并保存 cookie
curl -c "$COOKIE_FILE" -b "$COOKIE_FILE" \
-H "Referer: $QB_URL" \
-d "username=$QB_USER&password=$QB_PASS" \
"$QB_URL/api/v2/auth/login"
# 登出
curl -c "$COOKIE_FILE" -b "$COOKIE_FILE" \
"$QB_URL/api/v2/auth/logout"
重要: 登录请求需要 Referer header。
/api/v2/auth#操作端点方法参数登录/loginPOSTusername, password登出/logoutPOST-
/api/v2/torrents#操作端点方法参数种子列表/infoGETfilter, hashes, category, tag种子数量/countGETfilter种子详情/propertiesGEThash添加种子/addPOSTurls, savepath, category, tags删除种子/deletePOSThashes, deleteFiles开始/startPOSThashes停止/stopPOSThashes重新检查/recheckPOSThashes重新宣告/reannouncePOSThashes重命名/renamePOSThash, name设置注释/setCommentPOSThashes, comment
/api/v2/torrents#操作端点方法参数文件列表/filesGEThash文件优先级/filePrioPOSThash, id, priority重命名文件/renameFilePOSThash, oldPath, newPath重命名文件夹/renameFolderPOSThash, oldPath, newPath
/api/v2/torrents#操作端点方法参数获取下载限速/downloadLimitGEThashes获取上传限速/uploadLimitGEThashes设置下载限速/setDownloadLimitPOSThashes, limit设置上传限速/setUploadLimitPOSThashes, limit设置分享限制/setShareLimitsPOSThashes, ratioLimit, seedingTimeLimit
/api/v2/torrents#操作端点方法参数提高优先级/increasePrioPOSThashes降低优先级/decreasePrioPOSThashes最高优先级/topPrioPOSThashes最低优先级/bottomPrioPOSThashes
/api/v2/torrents#操作端点方法参数设置保存路径/setLocationPOSThashes, location设置保存目录/setSavePathPOSThashes, path设置下载目录/setDownloadPathPOSThashes, path
/api/v2/torrents#操作端点方法参数获取分类/categoriesGET-创建分类/createCategoryPOSTcategory, savePath编辑分类/editCategoryPOSTcategory, savePath删除分类/removeCategoriesPOSTcategories设置分类/setCategoryPOSThashes, category
/api/v2/torrents#操作端点方法参数获取标签/tagsGET-创建标签/createTagsPOSTtags删除标签/deleteTagsPOSTtags添加标签/addTagsPOSThashes, tags设置标签/setTagsPOSThashes, tags移除标签/removeTagsPOSThashes, tags
/api/v2/torrents#操作端点方法参数获取 Trackers/trackersGEThash添加 Trackers/addTrackersPOSThash, urls编辑 Tracker/editTrackerPOSThash, origUrl, newUrl删除 Trackers/removeTrackersPOSThash, urls
/api/v2/torrents#操作端点方法参数获取 Web Seeds/webseedsGEThash添加 Web Seeds/addWebSeedsPOSThash, urls编辑 Web Seed/editWebSeedPOSThash, origUrl, newUrl删除 Web Seeds/removeWebSeedsPOSThash, urls
/api/v2/torrents#操作端点方法参数添加 Peers/addPeersPOSThashes, peers
/api/v2/torrents#操作端点方法参数自动管理/setAutoManagementPOSThashes, enable超级做种/setSuperSeedingPOSThashes, value强制开始/setForceStartPOSThashes, value顺序下载/toggleSequentialDownloadPOSThashes首尾块优先/toggleFirstLastPiecePrioPOSThashes
/api/v2/transfer#操作端点方法参数传输信息/infoGET-下载限速/downloadLimitGET-上传限速/uploadLimitGET-设置下载限速/setDownloadLimitPOSTlimit设置上传限速/setUploadLimitPOSTlimit切换备用限速/toggleSpeedLimitsModePOST-
/api/v2/app#操作端点方法参数版本/versionGET-API 版本/webapiVersionGET-构建信息/buildInfoGET-关闭/shutdownPOST-获取设置/preferencesGET-设置偏好/setPreferencesPOSTjson默认保存路径/defaultSavePathGET-
/api/v2/log#操作端点方法参数日志/mainGETnormal, info, warning, criticalPeer 日志/peersGETlast_known_id
/api/v2/sync#操作端点方法参数主数据/maindataGETrid种子 Peers/torrentPeersGEThash, rid
/api/v2/rss#操作端点方法参数添加文件夹/addFolderPOSTpath添加订阅/addFeedPOSTurl, path移除/removeItemPOSTpath移动/moveItemPOSTitemPath, destPath获取文章/itemsGETwithData标记已读/markAsReadPOSTitemPath, articleId刷新/refreshItemPOSTitemPath设置自动下载规则/setRulePOSTruleName, ruleDef删除规则/removeRulePOSTruleName获取规则/rulesGET-匹配规则/matchingArticlesGETruleName
/api/v2/search#操作端点方法参数启动搜索/startPOSTpattern, plugins, category停止搜索/stopPOSTid搜索状态/statusGETid搜索结果/resultsGETid, limit, offset删除搜索/deletePOSTid获取插件/pluginsGET-安装插件/installPluginPOSTsources卸载插件/uninstallPluginPOSTnames启用插件/enablePluginPOSTnames, enable更新插件/updatePluginsPOST-
参数单位换算limit (限速)bytes/second1 MB/s = 1048576sizebytes-progress0-1 浮点数0.5 = 50%priority整数0=不下载, 1=正常, 6=高, 7=最大
状态含义error错误missingFiles文件丢失uploading做种中pausedUP暂停做种queuedUP排队做种stalledUP做种停滞checkingUP检查中forcedUP强制做种allocating分配空间downloading下载中metaDL下载元数据pausedDL暂停下载queuedDL排队下载stalledDL下载停滞checkingDL检查中forcedDL强制下载checkingResumeData检查恢复数据moving移动中unknown未知
torrents/info 支持的过滤器:
all - 全部downloading - 下载中seeding - 做种中completed - 已完成paused - 已暂停active - 活跃inactive - 不活跃resumed - 已恢复stalled - 停滞stalled_uploading - 上传停滞stalled_downloading - 下载停滞errored - 错误重要: 添加种子前必须先查询已有保存路径,然后询问用户保存位置。
bash
# 获取已有保存路径(按使用频率排序,显示前 10 个)
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/torrents/info" | \
jq -r '.[].save_path' | sort | uniq -c | sort -rn | head -10
# 获取默认保存路径
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/app/defaultSavePath"
使用 AskUserQuestion 工具,展示已有路径供用户选择:
text
问题: 下载文件保存到哪个目录?
选项:
- /m2/ani/番剧 (最常用)
- /m2/movies
- /downloads (默认)
- 其他路径 (用户自定义)
用户选择路径后,执行添加操作:
bash
# Magnet link(用户选择或指定的路径)
curl -c cookies.txt -b cookies.txt \
-d "urls=magnet:?xt=urn:btih:xxxx" \
-d "savepath=$USER_SELECTED_PATH" \
"$QB_URL/api/v2/torrents/add"
# 带分类和标签
curl -c cookies.txt -b cookies.txt \
-d "urls=magnet:?xt=urn:btih:xxxx" \
-d "savepath=$USER_SELECTED_PATH" \
-d "category=movies" \
-d "tags=hd,2024" \
"$QB_URL/api/v2/torrents/add"
# Torrent 文件
curl -c cookies.txt -b cookies.txt \
-F "torrents=@file.torrent" \
-d "savepath=$USER_SELECTED_PATH" \
"$QB_URL/api/v2/torrents/add"
bash
# 全部
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/torrents/info"
# 按状态过滤
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/torrents/info?filter=downloading"
# 按分类过滤
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/torrents/info?category=movies"
# 按标签过滤
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/torrents/info?tag=hd"
# 指定种子
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/torrents/info?hashes=abc123,def456"
bash
# 开始
curl -c cookies.txt -b cookies.txt \
-d "hashes=abc123" \
"$QB_URL/api/v2/torrents/start"
# 停止
curl -c cookies.txt -b cookies.txt \
-d "hashes=abc123" \
"$QB_URL/api/v2/torrents/stop"
### 删除种子
> **重要**: 删除种子时的 `deleteFiles` 参数规则:
>
> | 场景 | deleteFiles | 说明 |
> |------|-------------|------|
> | **默认行为** | `false` | 只删除任务,保留已下载的文件 |
> | 用户明确要求删除文件 | `true` | 用户指定要删除文件时 |
> | 未下载完成的种子 | `true` | progress < 100% 时,删除不完整的文件 |
>
> **安全原则**: 除非用户明确指定或种子未完成,否则默认不删除文件。
```bash
# 删除种子(保留文件)- 默认行为
curl -c cookies.txt -b cookies.txt \
-d "hashes=abc123&deleteFiles=false" \
"$QB_URL/api/v2/torrents/delete"
# 删除种子(同时删除文件)- 用户明确要求时
curl -c cookies.txt -b cookies.txt \
-d "hashes=abc123&deleteFiles=true" \
"$QB_URL/api/v2/torrents/delete"
# 删除多个种子(用 | 分隔)
curl -c cookies.txt -b cookies.txt \
-d "hashes=abc123|def456|ghi789&deleteFiles=false" \
"$QB_URL/api/v2/torrents/delete"
# 删除全部种子
curl -c cookies.txt -b cookies.txt \
-d "hashes=all&deleteFiles=false" \
"$QB_URL/api/v2/torrents/delete"
删除前建议检查:
bash
# 检查种子是否完成 (progress=1 表示 100%)
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/torrents/info?hashes=abc123" | jq '.[0].progress'
参数类型必填说明hashesstring✅种子 hash,多个用 | 分隔, all 删除全部deleteFilesbool❌是否删除文件,默认 false
bash
# 设置种子下载限速 1 MB/s
curl -c cookies.txt -b cookies.txt \
-d "hashes=abc123&limit=1048576" \
"$QB_URL/api/v2/torrents/setDownloadLimit"
# 设置种子上传限速 512 KB/s
curl -c cookies.txt -b cookies.txt \
-d "hashes=abc123&limit=524288" \
"$QB_URL/api/v2/torrents/setUploadLimit"
# 全局限速
curl -c cookies.txt -b cookies.txt \
-d "limit=1048576" \
"$QB_URL/api/v2/transfer/setDownloadLimit"
bash
# 创建分类
curl -c cookies.txt -b cookies.txt \
-d "category=movies&savePath=/downloads/movies" \
"$QB_URL/api/v2/torrents/createCategory"
# 设置分类
curl -c cookies.txt -b cookies.txt \
-d "hashes=abc123&category=movies" \
"$QB_URL/api/v2/torrents/setCategory"
# 创建标签
curl -c cookies.txt -b cookies.txt \
-d "tags=hd,4k" \
"$QB_URL/api/v2/torrents/createTags"
# 添加标签
curl -c cookies.txt -b cookies.txt \
-d "hashes=abc123&tags=hd" \
"$QB_URL/api/v2/torrents/addTags"
bash
# 设置文件优先级
# priority: 0=不下载, 1=正常, 6=高, 7=最大
curl -c cookies.txt -b cookies.txt \
-d "hash=abc123&id=0&priority=0" \
"$QB_URL/api/v2/torrents/filePrio"
bash
# 获取 trackers
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/torrents/trackers?hash=abc123"
# 添加 tracker
curl -c cookies.txt -b cookies.txt \
-d "hash=abc123&urls=udp://tracker.example.com:1337/announce" \
"$QB_URL/api/v2/torrents/addTrackers"
bash
curl -c cookies.txt -b cookies.txt \
"$QB_URL/api/v2/transfer/info"
返回示例:
json
{
"dl_info_speed": 1048576,
"dl_info_data": 10737418240,
"up_info_speed": 524288,
"up_info_data": 5368709120,
"dl_rate_limit": 0,
"up_rate_limit": 0,
"dht_nodes": 1234,
"connection_status": "connected"
}
bash
#!/bin/bash
# 配置 - 从 CLAUDE.md 获取或使用环境变量
QB_URL="${QB_URL:-http://192.168.31.88:8080}"
QB_USER="${QB_USER:-admin}"
QB_PASS="${QB_PASS:-123123}"
COOKIE_FILE="/tmp/qb_cookies.txt"
# 登录
qb_login() {
curl -s -c "$COOKIE_FILE" -b "$COOKIE_FILE" \
-H "Referer: $QB_URL" \
-d "username=$QB_USER&password=$QB_PASS" \
"$QB_URL/api/v2/auth/login"
}
# 登出
qb_logout() {
curl -s -c "$COOKIE_FILE" -b "$COOKIE_FILE" \
"$QB_URL/api/v2/auth/logout"
rm -f "$COOKIE_FILE"
}
# GET 请求
qb_get() {
local endpoint="$1"
curl -s -c "$COOKIE_FILE" -b "$COOKIE_FILE" \
"$QB_URL/api/v2/$endpoint"
}
# POST 请求
qb_post() {
local endpoint="$1"
local data="$2"
curl -s -c "$COOKIE_FILE" -b "$COOKIE_FILE" \
-d "$data" \
"$QB_URL/api/v2/$endpoint"
}
# 使用示例
qb_login
# 获取下载中的种子
qb_get "torrents/info?filter=downloading" | jq '.'
# 添加种子
qb_post "torrents/add" "urls=magnet:?xt=urn:btih:xxxx&savepath=/downloads"
qb_logout
问题解决方案登录失败检查 Referer headerCookie 不生效确保同时使用 -c 和 -b限速不生效单位是 bytes/s,不是 KB/s找不到种子用 torrents/info 查询 hash中文乱码确保 URL 编码403 Forbidden检查白名单设置或 CSRF 保护