Skip to main content
Convert browser automations to direct API calls for speed and reliability.
Browser automation is reliable, but it’s not always the fastest approach. When a site’s internal API is accessible from within the browser context, calling those endpoints directly is faster and less fragile than driving the UI. No selectors to maintain, no page rendering to wait for, and no DOM changes to break your script. Libretto captures all network traffic during a browser session, so your agent can inspect exactly which API calls the site makes and convert a UI-based workflow to direct requests.
Not every site is a good candidate for network conversion. Stay with browser automation if:
  • The site has enterprise bot protection (Akamai, PerimeterX, Shape Security) and monkey-patches window.fetch with call-stack inspection
  • The site does API-level monitoring that correlates request timing to UI interactions
  • The data you need is rendered client-side and doesn’t come from a clean API response
In these cases, stick with your existing Playwright automation or use passive interception (page.on('response', ...)) to capture API data without making extra requests.

Steps

1

Prompt your agent

Point the agent at the existing workflow file and ask it to convert to network requests.
Example prompt
Use the Libretto skill. We have a browser script at ./integration.ts that automates going to Hacker News and getting the first 10 posts. Convert it to direct network requests instead.
Include “Use the Libretto skill” so your agent uses Libretto’s network capture tools instead of guessing at API endpoints.
2

Watch the agent analyze the traffic

The agent runs the existing script to capture all network requests and responses, then examines the log to find API endpoints that return the data you need.
# What the agent runs
npx libretto run ./integration.ts
jq . .libretto/sessions/conversion-flow/network.jsonl
If the site requires login, the agent opens a headed browser and you’ll log in as usual. Otherwise you don’t need to do anything during this phase.
3

Review the agent's approach

The agent reports what API endpoints it found and whether the site has bot protection. It recommends one of two approaches:
  • In-browser fetch: call endpoints directly from within the browser’s JavaScript context. The requests share the browser’s cookies and TLS fingerprint, so they look identical to the site’s own requests. Faster, but only safe when the site doesn’t monitor for anomalous call patterns.
  • Passive interception: set up a page.on('response', ...) listener and let the site’s own code make the requests. Zero detection risk because no extra requests are made, but you’re limited to whatever requests the site naturally triggers. Safer, but less flexible. Use this when the site has bot protection.
If the agent’s recommendation looks reasonable, let it proceed. If you’d rather use one approach over the other, say so:
Use passive interception, I don’t want to risk triggering their bot detection.
4

Review the rewritten workflow

The agent rewrites the workflow to use network requests instead of DOM extraction. Things to check:
  • Endpoints: are the URLs and HTTP methods correct?
  • Auth/cookies: does the new version handle authentication the same way as the original?
  • Output shape: the return type should be identical to the browser-based version
  • Error handling: API calls should check response status codes and handle failures gracefully
  • Pagination: if the original script paginated through UI, the new version should paginate through the API
If something’s off:
The original script also grabbed the comment count, but the new version doesn’t fetch that field from the API.
5

Check the validation run

The agent runs the rewritten workflow headless and shows you the output. Compare it to what the browser-based version returned. The data should match.
# What the agent runs
npx libretto run ./integration.ts --headless
If the output is wrong or incomplete, tell the agent what’s off:
The scores are all null. It looks like the API response has a different field name for that.

When to convert to network requests

  • The site exposes a usable JSON API behind the UI
  • You need to paginate deeply (fetching page 50 without clicking “next” 49 times)
  • You want data the UI doesn’t display (hidden fields, metadata, IDs)
  • Speed and reliability matter more than DOM fidelity

One-shot workflow generation

Start from scratch. Give your agent a goal and let it build the workflow.

Debugging workflows

Reproduce failures and fix broken automations interactively.