Skip to main content
This tutorial deploys a Stagehand script as a Function, gives it your API key through a Secret, 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 10 or later
  • The browse CLI:

Create a Functions project

1

Initialize the project

Create the project:
The initializer installs Stagehand and creates package.json, tsconfig.json, .env, and a starter index.ts.
2

Add your credentials

Export your Browserbase API key from Settings in the terminal where you run the development server:
Browserbase resolves the project from the API key. You don’t need a Project ID. You also don’t need a model API key, because Stagehand sends model calls through the Browserbase Model Gateway. Deployed Functions read the API key from a Secret instead.
3

Add the Stagehand extension

Stagehand needs its extension in the browser session. Upload the extension to your project:
Copy the id from the output. The Function uses it in the next step. If you use Playwright, skip this step.
4

Define the Function

Replace index.ts with a Function that returns the title of a page. For Stagehand, replace your-extension-id with the id from the previous step:
Node.js
The initializer creates a pnpm-workspace.yaml file in the project directory. It holds the pnpm setting that approves the esbuild build script, which pnpm 11 and later require.

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 from builtFunctions[].id. See Deploy Functions for multi-file projects.

Add the Secret

The Stagehand version reads your Browserbase API key from a Secret. If you use Playwright, skip this section. Create a project Secret for your Browserbase API key. The --env flag reads the value from your shell environment, so export the key first:
Attach the Secret to the Function with the Function ID and the id of the Secret:
The list output must show the Secret. If the build fails, open the build in the Browserbase dashboard to see the build logs. The logs show the install and build errors.

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.