Prerequisites
You need:- A Browserbase account
- A Browserbase API key from Settings
- Node.js and pnpm 10 or later
- The
browseCLI:
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:- Stagehand
- Playwright
Node.js
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 The local server runs the handler synchronously. The response contains:
defineFn:Publish the Function
Publish the entrypoint: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:
id of the Secret:
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:202 Accepted with an invocation object. Copy its id, then poll the invocation:
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.