SKILL.md
SKILL.mdBrowse 4 files
2,640 tokens
10,336 bytes
Token encoding: o200k_base
Snapshot 24fd22b
1---2name: har-derived-api-client3description: Record a site's XHR into a HAR, derive an HTTP client.4version: 0.1.05author: Hermes Agent6license: MIT7platforms: [linux, macos, windows]8metadata:9 hermes:10 tags: [Browser, HAR, API, Reverse-Engineering, Playwright]11 category: web-development12---13 14# HAR-Derived API Client15 16Drive a website once with a real browser while recording its network traffic17to a HAR file, then distill that HAR into the site's private JSON API so you18can call it directly with plain HTTP — far cheaper and faster than19browser-controlling the page on every request. Credit: trick by Jared Longster,20popularized by Dax (thdxr). This captures and replays; it does NOT bypass21auth, solve CAPTCHAs, or defeat bot-detection — if the site needs a logged-in22session, you carry its headers/cookies forward, you don't forge them.23 24The scripts are stdlib-plus-Playwright: capture needs Playwright, derivation25is pure stdlib, replay needs only `requests`/`httpx` (or `curl`).26 27Covers **every Hermes browser pathway**: the default local `browser_navigate`28backend, plus the cloud/remote backends (Browserbase, Browser-Use, Firecrawl)29and any `/browser connect` CDP endpoint. There are two capture scripts — one30for a browser you launch, one for a browser you attach to over CDP — because31HAR recording works differently in each case (see How to Run).32 33## When to Use34 35- "Build a CLI/client for <website>" — derive its API instead of scripting clicks.36- "This site has no public API but the page clearly fetches JSON."37- You're about to loop `browser_navigate` for the same query repeatedly — stop and derive the endpoint once.38- Reverse-engineering an autocomplete, search, feed, or checkout XHR.39- You captured a session on a cloud backend (Browserbase / Browser-Use / Firecrawl) or via `/browser connect` and want the API without re-renting the browser.40 41## Prerequisites42 43- Playwright + a browser binary (capture step only):44 - `pip install playwright` then `playwright install chromium`45 - (If a system Playwright already has browsers under `~/.cache/ms-playwright`, reuse it.)46- `requests` or `httpx` for the replay step (stdlib `urllib` also works).47- No API keys. Any keys/tokens the client needs are the ones the HAR captured.48- For the CDP path (`har_capture_cdp.py`): a reachable CDP endpoint. On Hermes,49 run `/browser connect` to print the active endpoint, or read `BROWSER_CDP_URL`50 / `browser.cdp_url` in config. Cloud backends expose it as `cdpUrl`/`connectUrl`.51 52## How to Run53 54Scripts under this skill's `scripts/`, invoked through the `terminal` tool.55**Pick the capturer by pathway** — this is the part that trips people up:56 57| Browser pathway | How Hermes reaches it | Capturer |58|---|---|---|59| Local `browser_navigate` (default, agent-browser/Playwright) | launched locally | `har_capture.py` |60| Camofox (`CAMOFOX_URL` set) | local REST/CDP | `har_capture_cdp.py` if it exposes CDP, else drive it yourself |61| Browserbase / Browser-Use / Firecrawl (cloud) | **CDP** (`cdpUrl`) | `har_capture_cdp.py` |62| `/browser connect <url>` / `BROWSER_CDP_URL` | **CDP** | `har_capture_cdp.py` |63 64Rule of thumb: **if Hermes *launched* the browser, use `har_capture.py`; if it65*connected to* one over CDP, use `har_capture_cdp.py`.** `har_capture.py` uses66Playwright's `record_har_path`, which only works on a locally-owned context.67`har_capture_cdp.py` attaches with `connect_over_cdp()` and assembles the HAR68from `page.on("request"/"response")` events, because `record_har_path` is69unavailable on a connected browser.70 71Then, for either path:72 73- `har_to_client.py` — filters the HAR to XHR/fetch/JSON, groups by endpoint, and prints params, headers, bodies, and replay hints (User-Agent / cookie / auth).74 75Resolve paths against this skill's directory. Canonical loop:76 77```bash78# 1a. Capture, LOCAL browser (Hermes launched it)79python3 scripts/har_capture.py "https://SITE/" out.har \80 --action "fill:input[name=search]:my query" --action "sleep:3" --wait 281 82# 1b. Capture, CDP browser (cloud backend or /browser connect)83# get the endpoint from /browser connect or BROWSER_CDP_URL84python3 scripts/har_capture_cdp.py "ws://HOST/devtools/browser/..." out.har \85 --goto "https://SITE/" --action "fill:input[name=search]:my query" \86 --action "sleep:3" --wait 287 88# 2. Derive — read the endpoints out of the HAR89python3 scripts/har_to_client.py out.har --host SITE --max-body 40090 91# 3. Replay — write a tiny client from the printed endpoint (see Procedure)92```93 94## Quick Reference95 96```97har_capture.py <url> <out.har> [--wait S] [--headed] [--action SPEC ...]98 action SPEC: fill:SELECTOR:TEXT | press:SELECTOR:KEY | click:SELECTOR99 goto:URL | sleep:SECONDS (run in order after page load)100 use when Hermes LAUNCHED the browser (local browser_navigate default)101 102har_capture_cdp.py <cdp_url> <out.har> [--goto URL] [--wait S] [--action SPEC ...]103 same action SPEC; attaches to an existing CDP browser and does NOT close it104 use for cloud backends (Browserbase/Browser-Use/Firecrawl) & /browser connect105 106har_to_client.py <in.har> [--host SUBSTR] [--include-static] [--max-body N]107 default: keeps only XHR/fetch/JSON; --host narrows to one domain108 prints per endpoint: query params, non-boring req headers, req body sample,109 response status/content-type + body sample110 prints "### Replay hints": the browser User-Agent, cookie/auth presence111```112 113## Procedure114 1150. **Pick the capturer by pathway** (see How to Run table). Launched-locally → `har_capture.py`; reached over CDP → `har_capture_cdp.py`. On Hermes, `/browser connect` tells you the CDP endpoint when a cloud/remote backend is active.1161. **Find the interaction.** Open the site with `browser_navigate` (or `--headed` capture) to see which selector to type into / click, and confirm a JSON XHR fires in devtools/network.1172. **Capture the HAR** via the `terminal` tool. Order `--action` to reach the request: `fill` the box, then `sleep` long enough for the debounced XHR, and always leave `--wait` at the end so late responses flush. Both capturers embed response bodies, so the derived client sees real payload shapes.1183. **Derive** with `har_to_client.py --host <domain>`. Read off: the method, the URL/path template (numeric/UUID segments collapse to `{id}`), query params, request-body JSON, and the `### Replay hints` block.1194. **Write the client.** Recreate the request exactly — same method, path, query params, body. Send the headers the site actually needs: at minimum copy the **User-Agent** from the replay hints. If hints report cookies or an auth/token header, resend those too.1205. **Test browserless.** Run the client with the `terminal` tool and confirm it returns the same data the browser saw. This is the payoff: no browser in the loop.1216. **(Optional) Wrap as a CLI** — a small `argparse` script over the derived call, e.g. `search.py "frank herbert"`.122 123Worked example (Wikipedia search-title, derived + replayed live):124 125```python126import requests127r = requests.get(128 "https://en.wikipedia.org/w/rest.php/v1/search/title",129 params={"q": "frank herbert", "limit": 5},130 headers={"accept": "application/json",131 "User-Agent": "Mozilla/5.0 ... Chrome/131 Safari/537.36"}, # from HAR132 timeout=15,133)134for p in r.json()["pages"]:135 print(p["title"], "-", p.get("description"))136```137 138## Pitfalls139 140- **Default library User-Agent gets 403.** Many sites (Wikipedia, Cloudflare-fronted APIs) reject `python-requests/x.y`. Always send the browser UA from the replay hints. This is the #1 reason a derived client fails when the browser succeeded.141- **A failed `--action` aborts before the HAR flushes** — you get no file. If capture errors on a selector, the run produced nothing; fix the selector (use `--headed` to watch) and rerun. Don't debug a missing HAR.142- **Server-rendered pages have no XHR** to derive — `har_to_client.py` prints "No API-looking entries". The data came in the HTML; scrape it or find the interaction that does fetch JSON.143- **Debounced/typeahead XHRs need a real pause.** Add `--action "sleep:3"` after `fill`; typing alone won't have fired the request when the HAR closes.144- **Auth/session endpoints** need the captured `Cookie`/`Authorization` header, and those expire. The derived client is only as durable as the credential; re-capture when it 401s. HARs contain live secrets — treat `out.har` as sensitive and delete it after deriving.145- **`record_har_content="embed"` makes big HARs.** Use `--max-body` to cap what's printed; the file itself can be large for media-heavy pages.146- **Endpoints shift.** Sites change private APIs without notice. Re-run the capture→derive loop when a client breaks rather than patching URLs by hand.147- **Wrong capturer = empty/no HAR.** `har_capture.py` on a cloud/CDP backend records nothing (it launches its own local browser instead of the one you meant). `har_capture_cdp.py` needs the endpoint; on Hermes get it from `/browser connect` or `BROWSER_CDP_URL`. Match the capturer to the pathway (How to Run table).148- **Headless-Chrome UA is a weak tell.** Local/agent-browser capture yields a `HeadlessChrome/...` User-Agent; some sites sniff the "Headless" token. Cloud backends (Browserbase/Browser-Use) send a real desktop-Chrome UA, so a client derived from a cloud capture replays more reliably. If a headless-derived client 403s where the browser didn't, swap the "Headless" UA for a normal Chrome UA string before assuming the endpoint changed.149- **CDP capture doesn't close the browser.** `har_capture_cdp.py` attaches to a browser it doesn't own and leaves it running — correct for cloud/remote sessions Hermes manages. Don't add a close; let the owning backend tear it down.150 151## Verification152 153End-to-end proof against a live site with no API key:154 155```bash156python3 scripts/har_capture.py "https://en.wikipedia.org/wiki/Main_Page" /tmp/wiki.har \157 --action "fill:input[name=search]:dune messiah" --action "sleep:3" --wait 2158python3 scripts/har_to_client.py /tmp/wiki.har --host wikipedia.org --max-body 200159```160 161Expect the derivation to print `GET https://en.wikipedia.org/w/rest.php/v1/search/title`162with `q` and `limit` params and a JSON `pages` response — then replay it with the163Procedure snippet and confirm matching titles come back over plain HTTP.164 Discovery context
Discovered by repository scan. No exact path reference found in the snapshot’s root AGENTS.md.