Skip to main content
This tutorial deploys a Playwright script as a Function, then invokes it through the Browserbase API.
Functions are currently only available in the us-west-2 region.

Prerequisites

You need:
  • A Browserbase account
  • A Browserbase API key from Settings
  • Node.js and pnpm

Create a Functions project

1

Initialize the project

Run the Functions initializer:
The initializer creates package.json, tsconfig.json, .env, and a starter index.ts.
2

Add your Browserbase API key

Add your API key to .env:
Browserbase resolves the project from the API key. You don’t need a Project ID.
3

Define the Function

Replace index.ts with a Function that returns the title of a page:
Node.js
Functions don’t support pnpm workspaces. Keep pnpm-lock.yaml or package-lock.json in the Function project directory, not only at a workspace root.

Test the Function locally

1

Start the development server

Run:
The server listens on http://127.0.0.1:14113 and reloads when you change the Function files.
2

Invoke the local Function

In another terminal, invoke the Function by the name passed to defineFn:
The local server runs the handler synchronously. The response contains:

Publish the Function

Publish the entrypoint:
The command builds each Function reachable from index.ts and prints the Function IDs. Save the ID for page-title. See Deploy Functions for multi-file projects.

Invoke the deployed Function

Production invocations run asynchronously. Start an invocation with the Function ID:
The API returns 202 Accepted with an invocation object. Copy its id, then poll the invocation:
When status is COMPLETED, results contains the value returned by the handler.

Next steps

Write a Function

Configure parameters, browser sessions, and multiple Functions.

Deploy Functions

Publish from the CLI or the Playground.

Invoke a Function

Override session settings and handle asynchronous results.