Cloudflare's Browser Run hosts headless browsers, which are browsers driven by code with no visible window. On 20 November 2026 Cloudflare added a /script endpoint that accepts a JavaScript function over HTTP and runs the function against a live browser. Until now the usual options were a deployed Worker, a remote connection or one of the preset endpoints. An agent backend in any language can now send Playwright code and get JSON back.

We think the main use is giving an agent a browser tool for work that needs several steps. Cloudflare recommends custom scripts for jobs such as form completion and clicking, where a preset endpoint doesn't fit.

How the endpoint works

POST /script starts a browser and runs the submitted function in an isolated Worker connected to the new session. The library field chooses Playwright or Puppeteer. Leaving out library selects the raw Chrome DevTools Protocol (CDP), the low-level command set that both libraries use to control Chrome.

A Playwright function receives page, context, browser and request. A JSON-serialisable return value comes back inside result with status 200, and a typed array keeps its own content type, which suits binary output such as a screenshot.

The response headers cf-browser-session-id and cf-browser-target-id identify the session and the open page. A later call to POST /script/<sessionId> continues on the same page with cookies and navigation history intact.

Build a two-step browser tool

  1. Create a custom API token. The script page asks for the Browser Run - Edit permission. Cloudflare's session management page calls the permission Browser Rendering - Edit, so look for either name.
  2. Start a session using Cloudflare's example. The -i flag prints the headers you need for the next step.
curl -i -X POST "https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-run/script?keep_alive=120000" \
  -H "Authorization: Bearer <apiToken>" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "async ({ page }) => { await page.goto(\"https://example.com\"); return { title: await page.title(), url: page.url() }; }",
    "library": "playwright",
    "timeout": 60000
  }'
  1. Send a follow-up with the two saved IDs. Launch options such as keep_alive and recording are fixed at creation, and this endpoint takes no query parameters.
curl -X POST "https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-run/script/<sessionId>" \
  -H "Authorization: Bearer <apiToken>" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "async ({ page }) => ({ url: page.url(), title: await page.title() })",
    "library": "playwright",
    "timeout": 30000,
    "targetId": "<targetId>"
  }'
  1. Close the session with the documented DELETE call, which returns a closing status.
curl "https://api.cloudflare.com/client/v4/accounts/ACCOUNT_ID/browser-run/devtools/browser/SESSION_ID" \
  --request DELETE \
  --header "Authorization: Bearer {api_token}"

Teams already on Workers can call env.BROWSER.script() through a browser binding, which needs no API token. The method doesn't work in local development, so test with npx wrangler dev --remote. Put env.BROWSER.closeSession(sessionId) in a finally block, as Cloudflare's example does, so a failed script doesn't leave a browser running. For prototyping, Script Studio in the dashboard runs scripts beside a live view of the page.

Set both timeouts

The request's timeout field caps the run at 60 seconds by default and 120 seconds at most, excluding the time taken to acquire a browser. Cloudflare doesn't pass timeout through to Playwright or Puppeteer, so a script has a separate library limit.

Cloudflare says page.goto() and the waitFor* methods time out after 30 seconds in both libraries. Playwright's own reference for page.goto() lists a default of 0, meaning no timeout. We'd set navigation timeouts explicitly. Cloudflare's example for slow pages pairs a 40-second navigation limit with a timeout of 45 seconds or more.

The status code shows which limit fired. A run that hits the request cap returns 408, while a library timeout makes the script throw and returns 422. Compile errors and runtime exceptions also return 422, with the script line and column where Cloudflare can locate the fault. An agent that writes its own scripts could use the line and column to repair the code.

A 404 page won't raise an error in Playwright, because page.goto() doesn't throw on any valid HTTP status. Check response.status() inside the script and return the failure as JSON.

Idle browsers cost money

Once a script has started, neither endpoint closes the session, whether the script succeeds or fails. The browser stays open until you close the session or the idle timeout expires. The idle timeout defaults to 60 seconds and can be set as high as 20 minutes with keep_alive.

Billing follows the session's lifetime, so waiting time counts. Developer Flavio Copes, writing about Puppeteer sessions on Browser Run, gives the same warning about unclosed browsers.

| Item | Workers Free | Workers Paid | |---|---|---| | Plan minimum | None listed on the pages we read | US$5 a month | | Browser time | 10 minutes a day | 10 hours a month, then US$0.09 an hour | | Concurrent browsers | 3 | 10 included, then US$2 each, up to 200 by default | | New browsers | 1 every 20 seconds | 3 per second |

By our arithmetic, ten abandoned sessions at the default idle timeout use a free account's whole daily allowance. Requests over the daily limit fail with 429 until the next UTC day, which begins at 1pm in New Zealand during daylight saving.

Limits to plan around

Cookies persist only in the default browser context, which is Playwright's term for an isolated browser profile. Contexts your script creates close when the run ends, so do logins in the supplied page. Parallel scripts on one page can interfere with each other, so open a separate tab for each parallel run. Long agent runs also need a restart path, because sessions close when Browser Run ships a new release.

Browser Run won't get you past bot checks. Its requests are always identified as bot traffic, and its signature headers can't be removed or overridden.

Adding ?browser=kitesurf runs CDP scripts on Kitesurf, Cloudflare's lightweight browser for agents, which is free during its beta within account limits. Kitesurf runs create no session, so they can't be continued. In a comparison reported by IT Brief, Kitesurf screenshots used 380 ms of CPU against Chromium's 1,173 ms, but took 1,148 ms of wall time against 637 ms.

For New Zealand teams handling customer data, the FAQ says sessions run close to the incoming request and doesn't name regions. Optional recordings are kept for 30 days with input fields masked, although other page text can still appear in the replay. The script page doesn't cover model-written code or restrictions on network access, so we'd keep logged-in sessions away from scripts an agent writes after reading untrusted pages.

When a preset endpoint is enough

A read-only job may not need a script. Since 19 November, the /snapshot endpoint can also return a PDF, links, scraped elements or AI-extracted JSON from one page load. JSON extraction adds Workers AI usage, and one failed format fails the whole request. Preset endpoints, which Cloudflare calls Quick Actions, are billed on browser time alone, with no concurrency charge.

Our suggestion is to use /snapshot for reading pages and save /script for clicking or typing. Members who build on either endpoint can share the result on our projects page.

Sources

Build with people working on the same things

Join the Auckland Society for AI: NZ$599 a year with a ForgeBox and the members' projects area. Our Auckland meetups are open to everyone.

Become a member