# Threads 熱門趨勢 取得 Threads 熱門討論,掌握正在發生的事。 實際可用功能依 API key 已開通權限為準;安裝 MCP 或串接所有功能,不代表取得全部使用權限。 所需 scope:`trends`。 存取熱門排行、排行批次與對應文章,將趨勢資料接入你的監控或分析流程。 對應 MCP 工具:get_trends。 ## 連線與權限驗證 REST API 每次請求帶入 `x-api-key`;MCP 使用 `TPC_API_KEY` 環境變數。金鑰只存於後端或本機設定,不要放入前端、Git 或輸出紀錄。 先呼叫 `GET /api/public/schema`,確認 `success: true` 與 `scopes` 包含需要的功能。這一步不消耗每日額度。 ### macOS / Linux 適用 Bash / Zsh。使用 export 設定環境變數,反斜線 \ 用來接續下一行。 ```bash export TPC_API_KEY="pk_YOUR_API_KEY" curl --fail-with-body \ "https://pandora.thepocket.company/api/public/schema" \ -H "x-api-key: $TPC_API_KEY" ``` ### PowerShell 適用 Windows PowerShell 5.1 / PowerShell 7。使用原生 Invoke-RestMethod,避免 curl 別名問題;請求參數集中在 $request,不需換行接續符號。 ```powershell $env:TPC_API_KEY = "pk_YOUR_API_KEY" $request = @{ Uri = "https://pandora.thepocket.company/api/public/schema" Method = "Get" Headers = @{ "x-api-key" = $env:TPC_API_KEY } } Invoke-RestMethod @request | ConvertTo-Json -Depth 10 ``` ### Windows CMD 適用 Windows 命令提示字元,需要可執行的 curl.exe。使用 set 與 %變數名稱%,^ 用來接續下一行;若找不到 curl.exe,可改用 PowerShell 範例。 ```bat set "TPC_API_KEY=pk_YOUR_API_KEY" curl.exe --fail-with-body ^ "https://pandora.thepocket.company/api/public/schema" ^ -H "x-api-key: %TPC_API_KEY%" ``` ## MCP 接入 建議使用 **Node.js 22**。將以下區塊併入 Claude Desktop / Cursor 的既有設定,替換占位金鑰後重新啟動。修改前先備份。新連線可命名為 `tpc`;已有連線時沿用原名稱,不要重複新增,也不要覆蓋其他 MCP 連線。Codex / Claude Code 請見[接入總覽](https://pandora.thepocket.company/api-docs#quickstart)。 此設定適用於 **0.12.0**。從 0.11.0 升級只要把設定裡的版本號改成 0.12.0;從更早的版本升級時,還要將原有的金鑰環境變數名稱改成 `TPC_API_KEY`,金鑰值保持不變。改完請重新啟動客戶端。 ### macOS / Linux ```json { "mcpServers": { "tpc": { "command": "npx", "args": [ "-y", "@accucrazy/pandora-mcp@0.12.0" ], "env": { "TPC_API_KEY": "pk_YOUR_API_KEY" } } } } ``` ### Windows ```json { "mcpServers": { "tpc": { "command": "cmd", "args": [ "/c", "npx", "-y", "@accucrazy/pandora-mcp@0.12.0" ], "env": { "TPC_API_KEY": "pk_YOUR_API_KEY" } } } } ``` TPC 的套件名稱仍為 `@accucrazy/pandora-mcp`,API 網址仍為 `https://pandora.thepocket.company`,請勿自行改名。Windows 使用 `cmd /c npx` 啟動;CLI 範例請在 PowerShell 或 CMD 執行。Claude Desktop 請將金鑰放在該連線的 `env.TPC_API_KEY`,不要只在終端機設定環境變數。 ### 連線失敗時 `Server disconnected` 只表示本機 MCP 行程已中斷,無法單憑這行判斷原因。先開啟 Claude Desktop「設定 → 開發者 → 檢視記錄」: - 指令找不到:確認 Node.js 22 與 npm 已安裝、`node --version` 與 `npx --version` 可執行;Windows 設定需使用上方版本。 - `TPC_API_KEY is required`:補上該連線的金鑰環境變數,保留原金鑰值。 - `401`:向管理員確認金鑰是否正確、有效。 - npm 下載、網路或逾時:依記錄檢查對應連線;不要直接將首次失敗視為正常。 修正後完全結束再重開客戶端,以 schema 與 MCP 工具清單確認連線;不要以付費查詢或產製工作測試。提供記錄給他人前,請先遮掉金鑰與 Token。 API 與 MCP 共用金鑰權限與額度。未授權功能回傳 `403 forbidden`,請由管理員調整權限。 ## API 參數與使用範例 以下保留現行規格章節編號。跨章節參照可見[官方完整來源](https://pandora.thepocket.company/api-docs-trends.md)。 本文件說明如何呼叫 TPC(ThePocketCompany) 對外提供的「Threads 趨勢查詢」API。資料來源是我方持續快照的 **Threads 官方趨勢頁**:每次快照(一個 run)包含當下的趨勢排行條目(entries)與每個趨勢下的熱門貼文(posts)。 > 此 API 需要你的 API key 具有 **`trends` 調閱權限**。用 `GET /api/public/schema` 可以確認你的 key 有哪些權限(回應的 `scopes` 欄位);若呼叫時回 `403 forbidden`,請聯絡我方開通。 > 貼文全文搜尋(跨平台聲量資料)是另一支 API,見「貼文查詢 Posts」文件。 --- ## 1. 概觀 - **Base URL**:`https://pandora.thepocket.company` - **Endpoint**:`POST /api/public/query` 之外新增一支 **`POST /api/public/trends`**,同樣帶 HTTP header `x-api-key`。 - **四種查詢模式**(用 `type` 參數選擇): - `top`:**跨時間窗熱門趨勢聚合(最常用)**——「過去 N 天聲量前 K 名的趨勢議題+各自的去重文章」一次呼叫全部拿到。 - `runs`:快照批次清單——每隔約一小時快照一次 Threads 趨勢頁,一個 run 就是一次快照。 - `entries`:某個 run 的**趨勢排行條目**——排名、趨勢標題、一句話摘要、討論篇數。 - `posts`:某個 run 抓到的**趨勢貼文**——貼文內容與互動數(讚/留言/收藏/分享),並帶著所屬趨勢條目的脈絡。 - **配額**:與貼文查詢 API **共用同一個每日配額計數器**(每呼叫一次 `POST /api/public/trends` 計 1 次)。 - 趨勢資料量小,一般查詢在數秒內回應(不像貼文全文搜尋需要 13~15 秒)。 ### 1.1 五分鐘快速上手 1. **一次性下載**「過去 7 天聲量前 10 名趨勢+各自文章(已去重)」:`type: "top"`(見第 3.4 節)——天數與名次都可調,一次呼叫搞定。 2. 想看**此刻**的趨勢排行:`type: "entries"` + `runId: "latest"`。 3. 想看某個趨勢下的實際貼文:從上一步回應拿 `entryId`,用 `type: "posts"` + 同一個 `runId` + `entryId` 查。 4. 想做逐時段分析:先用 `type: "runs"` 列出快照批次(可給日期區間),再用特定 `runId` 查 entries/posts。 --- ## 2. 請求欄位表(`POST /api/public/trends`) | 欄位 | 型別 | 必填 | 說明 | |---|---|---|---| | `type` | string | 必填 | `"top"`(熱門趨勢聚合)/ `"runs"`(快照清單)/ `"entries"`(趨勢條目)/ `"posts"`(趨勢貼文) | | `runId` | string | entries/posts 必填 | 指定快照批次;填 `"latest"` 代表最新一次快照(伺服器自動解析)。`runs`/`top` 不可帶此欄位 | | `entryId` | string | 選填 | 只有 `type:"posts"` 可用:只回某個趨勢條目底下的貼文 | | `startDate` / `endDate` | string | 選填 | `runs`/`top` 可用:篩快照時間(台北時區日曆日),格式 `YYYY-MM-DD`、需成對、間距 ≤366 天 | | `days` | integer | 選填 | 只有 `type:"top"` 可用:最近 N 個台北日曆日(**含今天**),1~90;與 `startDate/endDate` 二選一,都沒給預設 7 | | `topN` | integer | 選填 | 只有 `type:"top"` 可用:取聲量前幾名趨勢,預設 10、上限 50 | | `postsPerTrend` | integer | 選填 | 只有 `type:"top"` 可用:每個趨勢最多帶幾篇文章,預設 100、上限 500;**填 0 = 只要排行榜不要文章** | | `limit` | integer | 選填 | `runs`/`entries`/`posts` 用。各 type 預設/上限:runs 30/200、entries 200/1000、posts 1000/5000(超出自動夾住);`top` 不收此欄位 | | `format` | string | 選填 | `"json"`(預設)或 `"jsonl"` | ### 2.1 空白範本(直接複製填空) 最常用的「過去 N 天前 K 名熱門趨勢+文章」(把數字換成你要的即可): ```json { "type": "top", "days": 7, "topN": 10, "postsPerTrend": 100 } ``` 「最新一次快照的趨勢排行」: ```json { "type": "entries", "runId": "latest" } ``` ```bash curl -X POST https://pandora.thepocket.company/api/public/trends \ -H "x-api-key: YOUR_API_KEY" \ -H "Content-Type: application/json" \ --data-binary @request.json ``` --- ## 3. 三種 type 的用法與回應 所有成功回應共用外層結構: ```json { "success": true, "meta": { "type": "entries", "runId": "threads_trends_2026_07_10_21_55_13_674_oonekx", "rowCount": 20, "limit": 200, "quota": { "limit": 200, "used": 3, "remaining": 197 } }, "data": [ ... ] } ``` `meta.runId` 一律是**實際的** run id——就算你送的是 `"latest"`,這裡也會告訴你解析成了哪一個 run(方便你之後精確重查同一批)。 ### 3.1 `type: "runs"` — 快照批次清單 ```json { "type": "runs", "startDate": "2026-07-01", "endDate": "2026-07-10", "limit": 30 } ``` `data[]` 欄位: | 欄位 | 中文名稱 | 型別 | 說明 | |---|---|---|---| | `runId` | 快照批次 ID | string | 用於後續查 entries / posts | | `status` | 狀態 | string | 快照批次的處理狀態(如 `completed`) | | `startedAt` / `finishedAt` | 開始/完成時間 | string | ISO 時間戳 | | `discoveredEntries` | 發現條目數 | number | 這次快照發現的趨勢條目數 | | `processedEntries` | 已處理條目數 | number | 實際處理(抓貼文)的條目數 | | `totalPosts` | 貼文總數 | number | 這次快照抓到的貼文總數 | ### 3.2 `type: "entries"` — 趨勢排行條目 ```json { "type": "entries", "runId": "latest" } ``` `data[]` 欄位(實測回應改編): ```json { "runId": "threads_trends_2026_07_10_21_55_13_674_oonekx", "entryId": "trend_3_巴威颱風_a7dc5b91084f", "entryType": "trend", "rank": 3, "label": "巴威颱風停班停課", "query": "巴威颱風", "description": "因應巴威颱風來襲,除台東縣外,全台各縣市及離島均宣布7月11日停止上班上課", "summary": "", "displayText": "", "postCountText": "", "postCount": 20, "serpType": "trends", "href": "https://...", "updatedAt": "2026-07-10T21:58:00.000Z" } ``` | 欄位 | 中文名稱 | 說明 | |---|---|---| | `rank` | 排名 | 趨勢頁上的名次(1 = 最熱) | | `label` | 趨勢標題 | 趨勢頁顯示的標題 | | `query` | 趨勢關鍵字 | 該趨勢對應的搜尋詞 | | `description` | 趨勢摘要 | 一句話說明這個趨勢在紅什麼 | | `postCount` | 討論篇數 | 此趨勢下我方抓到的貼文數 | | `entryType` / `serpType` | 條目類型 | 條目來源類型(如 `trend`) | | `href` | 趨勢連結 | Threads 趨勢頁上的連結 | ### 3.3 `type: "posts"` — 趨勢貼文 ```json { "type": "posts", "runId": "latest", "entryId": "trend_3_巴威颱風_a7dc5b91084f", "limit": 100 } ``` `data[]` 欄位: | 欄位 | 中文名稱 | 說明 | |---|---|---| | `postId` | 貼文 ID | 貼文唯一識別碼 | | `url` | 貼文網址 | 原始連結 | | `source` | 來源平台 | 一律小寫(趨勢貼文目前皆為 `threads`) | | `author` | 作者 | 發文帳號 | | `title` / `content` / `excerpt` | 標題/內文/摘要 | 貼文文字內容 | | `likes` / `comments` / `collections` / `shares` | 按讚/留言/收藏/分享數 | 互動數(快照當下的值) | | `pageCreatedAt` | 發文時間 | ISO 時間戳 | | `snapshottedAt` | 快照時間 | 我方抓到這篇貼文的時間 | | `entryId` / `entryRank` / `entryLabel` / `query` / `serpType` | 所屬趨勢脈絡 | 這篇貼文是在哪個趨勢條目下被抓到的 | > 同一篇貼文可能在多個 run 出現(趨勢延燒好幾小時);以 `postId` 去重即可。互動數是**快照當下**的值,不是即時值。 ### 3.4 `type: "top"` — 跨時間窗熱門趨勢聚合(一次性下載情境) **典型情境**:「我要過去 7 天(或昨天/14 天/30 天)依聲量排序的前 10 熱門趨勢議題,並下載這些趨勢底下的文章(去重)」——一次呼叫、計 1 次配額。 ```json { "type": "top", "days": 7, "topN": 10, "postsPerTrend": 100 } ``` 只要「昨天」:改用精確日期窗(`days` 與日期二選一): ```json { "type": "top", "startDate": "2026-07-09", "endDate": "2026-07-09", "topN": 10, "postsPerTrend": 100 } ``` **伺服器端做了什麼**:時間窗內以**趨勢關鍵字(`query`)**合併同名趨勢(同一議題延燒多個快照會自動歸戶)→ 文章先依 URL **去重**(同文多次入榜只留最新快照)→ 以**聲量**排序取前 `topN` 名 → 每名帶回聲量最高的前 `postsPerTrend` 篇文章。聲量公式與我方產品一致:`10 + 按讚數×3 + 留言數×5` 的全趨勢加總。 **回應形狀**(`data[]` 一個元素 = 一個趨勢,文章巢狀在 `posts[]`): ```json { "success": true, "meta": { "type": "top", "window": { "days": 7 }, "topN": 10, "postsPerTrend": 100, "rowCount": 10, "quota": { "limit": 200, "used": 4, "remaining": 196 } }, "data": [ { "rank": 1, "query": "巴威颱風", "label": "西北颱巴威逼近明日發警報", "postCount": 1189, "totalVolume": 6420743, "posts": [ { "postRank": 1, "postId": "...", "url": "https://...", "source": "threads", "author": "...", "title": "...", "content": "...", "likes": 163701, "comments": 892, "collections": 120, "shares": 4501, "volume": 495573, "pageCreatedAt": "2026-07-09T...", "snapshottedAt": "2026-07-10T..." } ] } ] } ``` | `data[]` 欄位 | 中文名稱 | 說明 | |---|---|---| | `rank` | 名次 | 依聲量排序(1 = 最熱) | | `query` | 趨勢關鍵字 | 跨快照歸戶的鍵——同關鍵字的多次入榜合併為一個議題 | | `label` | 趨勢標題 | 該議題的代表標題 | | `postCount` | 文章數 | 時間窗內此趨勢的**去重後**文章總數(可能大於實際帶回的 `posts` 數) | | `totalVolume` | 總聲量 | 去重文章的聲量加總 | | `posts[]` | 文章清單 | 每篇含 `postRank`(趨勢內聲量名次)、`volume`(單篇聲量)與貼文欄位(同 3.3 節) | > `postsPerTrend: 0` 可以只拿排行榜(`posts` 為空陣列),先看榜單再決定要抓哪個趨勢的完整文章。 > `format: "jsonl"` 時每行 = 一個趨勢(含巢狀 `posts`)。 --- ## 4. 限制與配額 | 項目 | 數值 | |---|---| | 每日配額 | **與貼文查詢 API 共用**同一個計數器(依 client 設定,預設 200 次/天,台北時間午夜重置) | | `limit` 上限 | runs 200 / entries 1000 / posts 5000 | | `top` 參數上限 | `days` ≤90、`topN` ≤50、`postsPerTrend` ≤500 | | `runs`/`top` 日期區間 | ≤366 天 | | 單次掃描上限 | 20GB(趨勢資料量小,正常使用不會觸及) | --- ## 5. 錯誤碼表 回應格式與貼文查詢 API 一致:`{ "success": false, "code": "...", "error": "..." }`。 | HTTP | code | 意義 | |---|---|---| | 401 | `unauthorized` | key 無效或已停用 | | 403 | `forbidden` | key 有效,但**沒有 `trends` 調閱權限**——請聯絡我方開通 | | 400 | `invalid_request` | 參數錯誤(`error` 有明細,例如 entries/posts 缺 `runId`) | | 404 | `not_found` | `runId: "latest"` 但目前沒有任何快照批次(極罕見) | | 429 | `quota_exceeded` | 當日配額用罄(與貼文查詢共用),台北時間午夜重置 | | 400 | `query_too_expensive` | 單次掃描超過 20GB(正常使用不會發生) | | 500 | `internal_error` | 我方內部錯誤,若持續發生請聯絡我方 | --- ## 6. 程式語言範例(Node.js/Python/PowerShell) 編碼注意事項與貼文查詢 API 完全同款(見 Posts 文件第 9/10 節),只是換 endpoint 與 body。以下用最常見的 `type:"top"` 當範例,其他 type 換 body 即可。 ### 6.1 Node.js(18+,內建 fetch) ```js const res = await fetch('https://pandora.thepocket.company/api/public/trends', { method: 'POST', headers: { 'x-api-key': process.env.TPC_API_KEY, 'Content-Type': 'application/json' }, body: JSON.stringify({ type: 'top', days: 7, topN: 10, postsPerTrend: 100 }), signal: AbortSignal.timeout(60_000), }); const data = await res.json(); if (!data.success) throw new Error(`${data.code}: ${data.error}`); for (const t of data.data) { console.log(`#${t.rank} ${t.query}(${t.label})文章 ${t.postCount} 篇、總聲量 ${t.totalVolume}`); } ``` ### 6.2 Python(requests) ```python import os, requests resp = requests.post( "https://pandora.thepocket.company/api/public/trends", headers={"x-api-key": os.environ["TPC_API_KEY"]}, json={"type": "top", "days": 7, "topN": 10, "postsPerTrend": 100}, timeout=60, ) data = resp.json() for t in data["data"]: print(f'#{t["rank"]} {t["query"]}({t["label"]})文章 {t["postCount"]} 篇、總聲量 {t["totalVolume"]}') ``` ### 6.3 Windows/PowerShell 終端機快速測試 Windows 內建 PowerShell 5.1 有中文編碼坑(原因見 Posts 文件第 10 節),直接複製下面這段即可正確收發: **先貼一次(設定 key +小幫手函式):** ```powershell $KEY = "pk_你的KEY" $URL = "https://pandora.thepocket.company/api/public/trends" function Get-Tpc($body) { $resp = Invoke-WebRequest -Uri $URL -Method Post -Headers @{ "x-api-key" = $KEY } ` -ContentType "application/json; charset=utf-8" ` -Body ([System.Text.Encoding]::UTF8.GetBytes(($body | ConvertTo-Json))) -UseBasicParsing [System.Text.Encoding]::UTF8.GetString($resp.RawContentStream.ToArray()) | ConvertFrom-Json } ``` **情境一:過去 7 天前 10 熱門趨勢+各帶 100 篇去重文章** ```powershell $top = Get-Tpc @{ type = "top"; days = 7; topN = 10; postsPerTrend = 100 } $top.data | Select-Object rank, query, label, postCount, totalVolume | Format-Table $top.data[0].posts | Select-Object postRank, author, likes, shares | Format-Table # 第 1 名趨勢的熱文 ``` **情境二:只要「昨天」的排行榜(不下載文章)** ```powershell (Get-Tpc @{ type = "top"; startDate = "2026-07-09"; endDate = "2026-07-09"; topN = 10; postsPerTrend = 0 }).data | Format-Table rank, query, totalVolume ``` **情境三:最新一次快照的趨勢排行** ```powershell (Get-Tpc @{ type = "entries"; runId = "latest" }).data | Select-Object rank, label, query, postCount | Format-Table ``` --- ## 7. 已知行為(請務必注意) 1. **`runId: "latest"` 的解析結果就在 `meta.runId`**——同一時間點連打兩次可能落在不同 run(剛好跨到新快照),要精確重查同一批請改用實際 runId。 2. **配額先扣後查、失敗不退還**(與貼文查詢 API 相同)。 3. **快照頻率約每小時一次**,實際以 `runs` 回應為準;每次快照的條目數/貼文數依 Threads 趨勢頁當下內容而變。 4. 互動數(likes 等)是**快照當下**的值;同一貼文跨 run 重複出現時,各 run 各自有當時的數字。 --- 如有串接問題、需要開通 `trends` 權限或調整配額,請聯絡我方管理窗口。