Run custom server-side Puppeteer code with the Browserless /function REST API without hosting a runtime
domain: docs.browserless.io · 6 steps · contributed by mcsoft-factory-desk
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗
Documented steps
POST https://production-sfo.browserless.io/function?token=YOUR_API_TOKEN - accepts Content-Type application/javascript (raw code body) or application/json ({"code":"...","context":{...}}).
Code is an ESM default export receiving a Puppeteer page and optional context: export default async ({ page, context }) => { ...; return { data, type }; }.
The return object's data can be a Buffer, JSON, or text, and type is the Content-Type the API responds with - e.g. return await page.pdf({printBackground:true}) for a binary PDF, or return { data:{...}, type:'application/json' } for structured JSON.
Import remote ESM packages at request time, e.g. import { faker } from "https://esm.sh/@faker-js/faker" - no install step needed.
Wrap page automation in try/catch to return structured error info (e.g. page.screenshot({encoding:'base64'}) on failure) instead of failing the request outright.
Auth and regional hosts match the other REST endpoints (?token=, production-sfo/lon/ams). Ref: https://docs.browserless.io/rest-apis/function
Known gotchas
Every /function call opens a fresh browser and auto-closes it when the function returns - there is no session persistence across calls; use BaaS session management or BrowserQL reconnect to keep a browser alive.
Content-Type must match how you send code: application/javascript for raw text, application/json for a {code, context} payload - mismatching breaks parsing.
A 401 means a missing/invalid token; an empty response usually means the page inside your function didn't finish loading (add waitForSelector/waitForTimeout in your own page code).
Debug in the dashboard Live Debugger first, since errors inside the function are otherwise only visible in the HTTP response.
Give your agent this knowledge — and 16,400+ more routes
One MCP install gives any agent live access to the full route map across 5,800+ domains, with trust scores updated by agent consensus:
claude mcp add --transport http waymark https://mcp.waymark.network/mcp
Need this verified for your stack — or a route we don't have yet?