browser-to-api
browserbase/skills
透過分析觀察到的 HTTP 流量、建立 URL 範本,以及從請求/回應範例推斷 JSON 模式,從瀏覽器追蹤擷取資料中產生 OpenAPI 3.1 規範。
...展開全部瀏覽器到 API
基於重播的 API 探索。讀取瀏覽器追蹤記錄,配對其中的 CDP 請求/回應事件,將觀察到的 URL 套用範本,從樣本中推斷 JSON 模式,並輸出OpenAPI 3.1文件以及一份人類可讀的覆蓋率報告。
此技能不會擷取流量。它純粹是基於瀏覽器追蹤(browser-trace)的 cdp/network/*.jsonl儲存桶所進行的離線後處理。這兩項技能可組合使用:
browser-trace → .o11y//cdp/network/{requests,responses}.jsonl
browser-to-api → .o11y//api-spec/index.html + openapi.yaml + client.mjs
何時使用
- 使用者希望為第三方或未文件化的網站 API 取得 OpenAPI 文件。
- 使用者已執行
瀏覽器追蹤,並希望從中提取端點與資料結構。 - 使用者正在針對未公開規格的網站建置客戶端/SDK。
- 使用者希望取得一份覆蓋率報告,顯示哪些流程能擴展該規格。
若使用者希望擷取流量,請先引導其使用瀏覽器追蹤功能。
兩步驟工作流程
1. 使用browser-trace擷取流量(可選:透過啟用「瀏覽網路」功能擷取請求內容)
# 針對現有可除錯 Chrome 目標的本地範例
TARGET=9222
node ../browser-trace/scripts/start-capture.mjs "$TARGET" my-site
browse open about:blank --cdp "$TARGET"
browse network on # 擷取請求/回應正文
browse open https://example.com
# ...執行您希望涵蓋的任何流量...
# 在關閉擷取功能之前,先對內容主目錄進行快照(臨時目錄是
# 按工作階段共享的,因此若跳過此步驟,後續執行的 `browse network on`
# 會將您的內容與未來擷取所寫入的資料混雜在一起)。
cp -r "$(browse network path | jq -r .path)" .o11y/my-site/cdp/network/bodies/
browse network off
node ../browser-trace/scripts/stop-capture.mjs my-site
node ../browser-trace/scripts/bisect-cdp.mjs my-site
啟用「瀏覽網路」 屬選用但強烈建議— 若未啟用,規格中將缺乏回應正文的結構(「瀏覽 CDP」所使用的 CDP 資料流並未嵌入正文)。啟用後,請求正文(已由 CDP 擷取)與回應正文將透過CDPrequestId 合併至追蹤紀錄中。
2. 產生規格文件
node scripts/discover.mjs --run .o11y/my-site
# → .o11y/my-site/api-spec/index.html ← 開啟此檔案
# .o11y/my-site/api-spec/client.mjs
# .o11y/my-site/api-spec/openapi.yaml
# .o11y/my-site/api-spec/openapi.json
# .o11y/my-site/api-spec/report.md
# .o11y/my-site/api-spec/confidence.json
# .o11y/my-site/api-spec/samples/*.json
# .o11y/my-site/api-spec/intermediate/*.jsonl
discover.mjs會自動偵測若要使用來自其他位置的請求正文擷取檔(例如:未進行快照,但希望取得即時瀏覽網路目錄),請明確傳入--bodies
3. 開啟 HTML 報告
在discover.mjs執行完畢後,請務必開啟生成的 HTML 報告:
開啟 .o11y/my-site/api-spec/index.html
該報告是一個獨立的 HTML 檔案(無需伺服器),會將每個已發現的操作以可展開的卡片形式呈現,內容包含變數、客戶端使用方式、請求/回應範例,以及底部生成的client.mjs程式碼片段。這是主要交付成果 — 務必為使用者開啟此檔案。
CLI 參數
| 參數 | 必填 | 含義 |
|---|---|---|
--run |
是 | 瀏覽器追蹤執行目錄的路徑 |
--out |
否 | 輸出目錄;預設為 |
--bodies |
無 | 瀏覽網路擷取目錄以將其併入追蹤記錄(若存在,則自動從偵測) |
--include |
否 | 僅包含符合正規表達式的 URL(可重複指定) |
--exclude |
否 | 排除符合正規表達式的 URL(可重複指定;除預設值外) |
--origins |
否 | 以逗號分隔的來源白名單(例如:api.example.com,example.com) |
--format |
無 | 輸出格式。預設為兩者皆有 |
--title |
無 | OpenAPI 的info.title。預設值取自主要來源 |
--redact |
否 | 需遮蔽的額外標頭名稱/JSON 鍵值(以逗號分隔) |
--min-samples |
否 | 每個端點至少包含的樣本數。預設值為1 |
--stage |
無 | 僅執行一個階段:載入、過濾、正規化、推斷、輸出 |
輸出佈局
/api-spec/
├── index.html 視覺化報告 — 開啟此檔案(自包含,無需伺服器)
├── client.mjs 零依賴的 fetch 客戶端,每項操作皆具備類型化函式
├── openapi.yaml 機器可讀的規格
├── openapi.json 鏡像檔
├── report.md Markdown 摘要 + curl 範例
├── confidence.json 各端點的信心度 + 正規化標誌
├── samples/ 經隱去敏感資訊的請求/回應範例
│ └──__.json
└── intermediate/ 處理流程的副產品(配對/過濾/端點的 jsonl 檔案)
透過「瀏覽 CDP」和「瀏覽網路」可獲得的內容
兩個互補的擷取來源:
| 來源 | 提供 | 限制 |
|---|---|---|
瀏覽 CDP(由browser-trace 使用) |
請求方法/URL/標頭/POST 資料、回應狀態/標頭/MIME 類型、完整事件時間戳記 |
不包含回應正文。必須透過Network.getResponseBody 擷取正文,而 Firehose 並不會執行此操作。 |
瀏覽網路(獨立指令) |
將請求內容與回應內容儲存至磁碟,並以 CDPrequestId作為鍵值 |
擷取目錄在每個瀏覽工作階段中是共用的;若在執行下一次「browse network on」之前進行快照,則會覆寫該目錄。 |
若您傳入--bodies 參數,discover.mjs會從「瀏覽網路」目錄中提取正文( ),或將其儲存至 目錄下(該目錄會被自動偵測)。匹配是根據requestId進行的——「瀏覽網路」會將其寫入每個request.json檔案的id 欄位,我們會直接進行關聯。
當包含正文時,以下內容會有所變更:
- ✅ 路徑模板、查詢參數架構、狀態碼、內容類型 — 無論是否存在請求主體,均保持不變。
- ✅ 請求正文模式 — 僅需 CDP 提供的
postData即可;若非postData情況,正文目錄僅為附加功能。 - ✅回應正文模式— 完全從真實樣本推斷而出。若無正文,則會獲得
{ description, content:這樣的骨架。}
該報告會標記所有沒有回應正文範例的端點。
自動雜訊過濾
正規化階段會自動分類並過濾基礎架構雜訊:
- 追蹤/分析— 包含
/track、/pixel、/beacon、/impression、/pageview、/dag/v*的路徑 - 防機器人— Akamai (
/akam/)、指紋載荷 (sensor_data)、經混淆處理的多段路徑 - 會話基礎架構—
/session、/authenticate/start、Cookie 同意、A/B 測試端點 - HTML 頁面渲染— 傳回
text/html 的GET請求(指渲染後的頁面,而非 API)
這通常會過濾掉 60-80% 的擷取流量。可使用--include旗標來挽救誤判的情況。
GraphQL / 多工端點分解
當單一端點(例如/dapi/fe/gql)被呼叫時,若傳入不同的operationName值,該功能會自動將其拆分為獨立的邏輯操作。每個操作皆擁有:
- OpenAPI 路徑條目(例如
/dapi/fe/gql [自動完成]) - 僅根據該操作的樣本推斷出的請求/回應架構
- 報告中的 Curl 範例與變數表
偵測機制會針對請求正文欄位(operationName、method、action)以及查詢參數(opname、op)進行分析。此機制涵蓋 GraphQL(APQ 與內嵌式)、JSON-RPC 以及類似的分派模式。
限制
- 涵蓋範圍受限於擷取到的流量。追蹤中未執行的端點將不會出現。此功能無法保證完整性。
- 架構屬歸納式,而非契約式。即使每個樣本都包含某個欄位,該欄位在伺服器端仍可能為可選項。
- 身份驗證僅被觀察到,而非明確指定。此技能會將符合身份驗證格式的標頭記錄在
x-observed-auth擴充欄位中,但不會聲稱其採用特定安全性方案。 - 路徑模板化採用啟發式方法。數值/UUID/十六進位/slug 模式會針對每個區段進行偵測。模稜兩可的 URL 會在
confidence.json中標記。 - 資料遮蔽採用「盡力而為」原則。預設遮蔽範圍涵蓋常見憑證,但應用程式專屬的機密資訊可能未能被遮蔽;若需處理已知的自訂標頭或金鑰,請使用
--redact 參數。
最佳實務
- 引導您希望記錄的流量。瀏覽器追蹤資料越豐富,規格文件就越詳盡。
- 針對資訊雜亂的網站,請使用
--origins 參數。行銷頁面可能會呼叫數十個分析伺服器;請將範圍限制在您關心的 API 來源上。 - 請先檢視
report.md 檔案。其中包含適用於 curl 的範例,以及針對每個已發現操作的回應樣本。 - 若希望最終文件中僅包含形狀可信的端點,請將
--min-samples調整為 2 以上— 剔除長尾資料。 - 當回應正文結構至關重要時,請搭配
啟用「瀏覽網路」功能。僅使用 CDP 資料流時,雖有請求正文,但沒有回應正文。
有關管線內部運作及檔案格式參考,請參閱 REFERENCE.md。
---
name: browser-to-api
description: Generate an OpenAPI 3.1 specification from a browser-trace capture by analyzing observed HTTP traffic, templating URLs, and inferring JSON schemas from request/response samples.
license: MIT
---
# Browser to API
Replay-driven API discovery. Consume a `browser-trace` capture, pair its CDP request / response events, templatize observed URLs, infer JSON schemas from samples, and emit an **OpenAPI 3.1** document plus a human-readable coverage report.
This skill **does not capture traffic**. It is purely offline post-processing on top of `browser-trace`'s `cdp/network/*.jsonl` buckets. The two skills compose:
```
browser-trace → .o11y/<run>/cdp/network/{requests,responses}.jsonl
browser-to-api → .o11y/<run>/api-spec/index.html + openapi.yaml + client.mjs
```
## When to use
- The user wants an OpenAPI document for a third-party or undocumented website API.
- The user has a `browser-trace` run and wants endpoints + schemas extracted from it.
- The user is building a client/SDK against a site that doesn't publish a spec.
- The user wants a coverage report showing which flows would broaden the spec.
If the user wants to **capture** traffic, send them to `browser-trace` first.
## Two-step workflow
### 1. Capture with `browser-trace` (and optionally bodies via `browse network on`)
```bash
# Local example against an existing debuggable Chrome target
TARGET=9222
node ../browser-trace/scripts/start-capture.mjs "$TARGET" my-site
browse open about:blank --cdp "$TARGET"
browse network on # capture request/response bodies
browse open https://example.com
# ...drive whatever flows you want covered...
# Snapshot the bodies dir BEFORE turning capture off (the temp dir is shared
# per-session, so subsequent `browse network on` runs would mix your bodies
# with whatever a future capture writes if you skip this step).
cp -r "$(browse network path | jq -r .path)" .o11y/my-site/cdp/network/bodies/
browse network off
node ../browser-trace/scripts/stop-capture.mjs my-site
node ../browser-trace/scripts/bisect-cdp.mjs my-site
```
`browse network on` is **optional but strongly recommended** — without it, the spec has no response-body schemas (the CDP firehose used by `browse cdp` does not embed bodies). With it, both request bodies (already captured by CDP) *and* response bodies are joined into the trace by CDP `requestId`.
### 2. Generate the spec
```bash
node scripts/discover.mjs --run .o11y/my-site
# → .o11y/my-site/api-spec/index.html ← open this
# .o11y/my-site/api-spec/client.mjs
# .o11y/my-site/api-spec/openapi.yaml
# .o11y/my-site/api-spec/openapi.json
# .o11y/my-site/api-spec/report.md
# .o11y/my-site/api-spec/confidence.json
# .o11y/my-site/api-spec/samples/*.json
# .o11y/my-site/api-spec/intermediate/*.jsonl
```
`discover.mjs` auto-detects `<run>/cdp/network/bodies/`. To use a body capture from elsewhere (e.g. didn't snapshot, want the live `browse network` dir), pass `--bodies <path>` explicitly.
### 3. Open the HTML report
After `discover.mjs` finishes, **always open the generated HTML report**:
```bash
open .o11y/my-site/api-spec/index.html
```
The report is a self-contained HTML file (no server needed) that shows each discovered operation as an expandable card with variables, client usage, request/response examples, and a generated `client.mjs` snippet at the bottom. This is the primary deliverable — always open it for the user.
## CLI flags
| Flag | Required | Meaning |
|---|---|---|
| `--run <path>` | yes | Path to a `browser-trace` run directory |
| `--out <path>` | no | Output dir; default `<run>/api-spec/` |
| `--bodies <path>` | no | `browse network` capture dir to join into the trace (auto-detected from `<run>/cdp/network/bodies/` when present) |
| `--include <regex>` | no | Only include URLs matching regex (repeatable) |
| `--exclude <regex>` | no | Exclude URLs matching regex (repeatable; in addition to defaults) |
| `--origins <list>` | no | Comma-separated origin allow-list (e.g. `api.example.com,example.com`) |
| `--format <yaml\|json\|both>` | no | Output format. Default `both` |
| `--title <string>` | no | OpenAPI `info.title`. Default derived from primary origin |
| `--redact <list>` | no | Extra header names / JSON keys to redact (comma-separated) |
| `--min-samples <n>` | no | Minimum samples per endpoint to include. Default `1` |
| `--stage <name>` | no | Run only one stage: `load`, `filter`, `normalize`, `infer`, `emit` |
## Output layout
```
<run>/api-spec/
├── index.html visual report — open this (self-contained, no server)
├── client.mjs zero-dep fetch client with typed functions per operation
├── openapi.yaml machine-readable spec
├── openapi.json mirror
├── report.md markdown summary + curl examples
├── confidence.json per-endpoint confidence + normalization flags
├── samples/ redacted request/response examples
│ └── <method>__<path-hash>.json
└── intermediate/ pipeline byproducts (paired/filtered/endpoints jsonl)
```
## What you get from `browse cdp` and `browse network`
Two complementary capture sources:
| Source | Provides | Limitation |
|---|---|---|
| `browse cdp` (used by `browser-trace`) | request method/URL/headers/`postData`, response status/headers/mimeType, full event timing | **Does not embed response bodies.** Bodies must be pulled with `Network.getResponseBody`, which the firehose doesn't do. |
| `browse network on` (separate command) | request bodies AND response bodies on disk, keyed by CDP `requestId` | Capture dir is shared per `browse` session; snapshot before another `browse network on` overwrites it. |
`discover.mjs` will pull bodies from a `browse network` dir if you pass `--bodies <path>` (or stash them under `<run>/cdp/network/bodies/`, which is auto-detected). The matching is by `requestId` — `browse network` writes that into each `request.json` as `id`, and we join directly.
What changes when bodies are present:
- ✅ Path templating, query-param schemas, status codes, content-types — same either way.
- ✅ Request-body schemas — `postData` from CDP is enough; bodies dir is a nice-to-have for non-`postData` cases.
- ✅ **Response-body schemas** — fully inferred from real samples. Without bodies you get `{ description, content: <mimeType> }` skeletons.
The report flags every endpoint that has no response-body sample.
## Automatic noise filtering
The normalize stage automatically classifies and drops infrastructure noise:
- **Tracking / analytics** — paths containing `/track`, `/pixel`, `/beacon`, `/impression`, `/pageview`, `/dag/v*`
- **Bot defense** — Akamai (`/akam/`), fingerprint payloads (`sensor_data`), obfuscated multi-segment paths
- **Session plumbing** — `/session`, `/authenticate/start`, cookie consent, A/B experiment endpoints
- **HTML page renders** — `GET` requests returning `text/html` (the rendered page, not the API)
This typically drops 60-80% of captured traffic. The `--include` flag can rescue a false positive.
## GraphQL / multiplexed endpoint decomposition
When a single endpoint (like `/dapi/fe/gql`) is called with different `operationName` values, the skill automatically splits it into separate logical operations. Each gets its own:
- OpenAPI path entry (e.g. `/dapi/fe/gql [Autocomplete]`)
- Request/response schema inferred from only that operation's samples
- Curl example and variables table in the report
Detection works on body fields (`operationName`, `method`, `action`) and query params (`opname`, `op`). This covers GraphQL (APQ and inline), JSON-RPC, and similar dispatch patterns.
## Limitations
- **Coverage is bounded by the captured flow.** Endpoints not exercised in the trace will not appear. The skill cannot prove completeness.
- **Schemas are inductive, not contractual.** A field might be optional on the server even if every sample contained it.
- **Auth is observed, not specified.** The skill records auth-shaped headers in an `x-observed-auth` extension but won't claim a security scheme.
- **Path templating is heuristic.** Numeric / UUID / hex / slug patterns are detected per segment. Ambiguous URLs are flagged in `confidence.json`.
- **Redaction is best-effort.** Default redactions cover common credentials, but app-specific secrets may slip through; use `--redact` for known custom headers/keys.
## Best practices
1. **Drive the flows you want documented.** The richer the browser-trace, the richer the spec.
2. **Use `--origins` for noisy sites.** A marketing page hits dozens of analytics hosts; restrict to the API origin you care about.
3. **Inspect `report.md` first.** It has curl-ready examples and response samples for every discovered operation.
4. **Bump `--min-samples` to 2+** when you want only confidently-shaped endpoints in the final doc — drop the long tail.
5. **Pair with `browse network on`** when response-body schemas matter. The CDP firehose alone has request bodies but not response bodies.
For pipeline internals and the file format reference, see [REFERENCE.md](REFERENCE.md).





首頁
