defineFn from @browserbasehq/sdk-functions to register a named handler:
- Stagehand
- Playwright
Node.js
defineFn arguments
defineFn accepts a name, a handler, and an optional configuration object.
Name
The name identifies a Function within a Browserbase project. Each Function in an entrypoint must have a unique name. Publishing another definition with the same name creates a new version of that Function. Callers continue to use the same Function ID.Handler
The async handler receivescontext and params.
context contains:
Node.js
session.id. Playwright and Puppeteer connect with session.connectUrl. Use session.id to inspect the session or add it to your logs.
secrets maps the name of each Secret attached to the Function to its value. If you haven’t attached any Secrets, secrets is an empty object. The local development server always passes an empty object.
params contains the object supplied in the invoke request. The request can contain up to 64 KB of serialized JSON.
The handler can return a JSON-serializable string, number, boolean, array, or object. Browserbase stores non-empty return values in the invocation’s results field. A handler that returns null or undefined completes without results.
Configuration
The optional third argument supports:parametersSchemato validate invocation parameters with Zod.sessionConfigto set the default browser session configuration for every invocation.
Validate parameters
DefineparametersSchema with Zod:
Node.js
Configure the browser session
SetsessionConfig when every invocation needs the same browser settings:
Node.js
region or keepAlive. The invocation timeout must be between 60 and 900 seconds.
A Function that uses Stagehand also sets extensionId in sessionConfig. See Connect a browser library.
Callers can override supported defaults for one invocation with sessionCreateParams.
Connect a browser library
- Stagehand
- Playwright
- Puppeteer
Stagehand needs its extension in the browser session, and a session cannot get an extension after it starts. Upload the extension one time for your project, then use the Connect Stagehand to the Function’s session by its ID:Use
id from the output as extensionId:Node.js
browserbase.connect() with the session ID. Don’t use localBrowser.connect() with context.session.connectUrl. That call loads the extension from your local disk, and the Function’s browser runs on Browserbase.The initializer installs the zod version that Stagehand uses. In an existing project, install it yourself. If your project has a different zod version, the schemas that you give to extract() don’t type-check:A Function can also run a custom browser agent loop. Connect its browser tool to
context.session.connectUrl. If you don’t need to own the model loop, use Browserbase Agents.Use Secrets
Keep API keys in encrypted project Secrets, not in your code. A Function reads each attached Secret fromcontext.secrets. The Stagehand examples read BROWSERBASE_API_KEY this way.
Stagehand doesn’t need a model API key. Without a model option, Stagehand sends model calls through the Browserbase Model Gateway, which picks a model for each call. Browserbase charges for the tokens.
Create each Secret one time for your project. The name of the Secret is its key in context.secrets:
builtFunctions[].id in the publish output. Attached Secrets take effect on the next invocation.
The local development server doesn’t pass Secrets. The Stagehand examples fall back to process.env, so export the same values in your shell.
Publish multiple Functions
The publish command follows the entrypoint’s import graph. EachdefineFn call in that graph becomes a Function.
For a single file, define each Function in the entrypoint:
Node.js
Node.js
Handle errors and logs
An unhandled error marks the invocation as failed. Catch an error only when your Function can add useful context or return a valid partial result. Useconsole.log, console.warn, and console.error for runtime logs. Browserbase captures this output in the invocation logs.
Browserbase closes the browser session when the handler completes. You don’t need to close it in your code.
Deploy Functions
Publish from the CLI or the Playground.
Invoke a Function
Pass parameters and retrieve asynchronous results.
Functions limits
Check bundle, timeout, storage, and region constraints.