Skip to main content

Recipe

This recipe shows how to extract data directly from API responses by intercepting network requests. Use Playwright’s response listener with withNetworkSettledWait (TypeScript) or wait_for_network_settled (Python).
TypeScript
import { BrowserContext, Page, Response } from "playwright-core";
import { withNetworkSettledWait } from "@intuned/browser";

let apiData: any[] = [];

async function handleResponse(response: Response): Promise<void> {
  if (response.url().includes("/rest/v1/consultations")) {
    try {
      apiData = await response.json();
    } catch (e) {
      // Response might not be JSON
    }
  }
}

export default async function handler(
  params: any,
  page: Page,
  context: BrowserContext
) {
  apiData = [];

  // Listen for responses matching the pattern
  page.on("response", handleResponse);

  // Wait until the page is fully loaded and in the idle state
  await withNetworkSettledWait(
    async (page) => {
      await page.goto('https://sandbox.intuned.dev/consultations/list');
    },
    {
      page,
      timeoutInMs: 20000,
    }
  );
  console.log(`Captured ${apiData.length} consultations from API`);
  return apiData;
}

How it works

  1. Set up response listener — Use page.on("response", handler) to listen for network responses.
  2. Filter by URL — Check if the response URL matches your target API endpoint.
  3. Capture the data — Parse the JSON response and store it.
  4. Navigate to the page — The listener captures API calls triggered during page load.