Defining Functions
Basic Function
Function parameters
- Function name - Unique identifier for your Function (used in the “invoke function” HTTP request)
- Handler - Async function that receives:
ctx- Context object with session informationparams- Parameters passed when invoking the Function
- Options - Optional configuration object for session settings
Context object
Thectx parameter provides access to the browser session:
Function response
Return any JSON-serializable data from your Function:Session configuration
Configure browser session settings using the third parameter:Available options
Most session creation options are supported:- browserSettings - Configure Verified, CAPTCHA solving, and browser behavior
- proxies - Enable proxy settings for IP rotation
- viewport - Set custom screen dimensions
- context - Use browser contexts for session persistence
- extensions - Load custom extensions
Complete example
Here’s a full example that fills out a contact form:Publishing Functions
Multiple Functions (single file)
Define multiple Functions in a single file:index.ts
pnpm bb publish index.ts.
Multiple Functions (multiple files)
Define multiple Functions in multiple files:index.ts
screenshot-fn.ts
pnpm bb publish index.ts.
Invoke a Function
Get build result
Retrieve information about the Function(s) built or updated by a build:Pass parameters to Functions
Access parameters in your Function handler:Override session settings at invocation time
ThesessionConfig you set in defineFn applies to every invocation as the default,
but you can override most session settings per invocation by passing
sessionCreateParams in the invoke request body. The runtime deep-merges your
invocation-time fields over the deploy-time defaults: values you pass at invoke win,
and any deploy-time fields you don’t override still apply.
This is useful when you want a single deployed Function to accept a different
browser context, proxy configuration,
or user metadata per call:
sessionCreateParams mirror the body of
Create a Session — browserSettings, proxies,
extensionId, userMetadata, and so on. You can’t configure region or
keepAlive per invocation. Functions accept timeout, but cap it between 60 and
900 seconds (15 minutes) and default it to 900.
See Invoke a Function for the full schema.
Best practices
Error handling
Browserbase runners catch unhandled errors and report them. If you want to gracefully handle errors (for example, return information on the failure), wrap your automation code in try-catch blocks:Timeouts
Set appropriate timeouts for navigation and actions when using Playwright:Logging
Use console methods for debugging - logs are captured and available in invocation logs:Session cleanup
Browser sessions automatically close when your Function completes. No manual cleanup is required.Monitoring and debugging
Every Function invocation creates a browser session that you can inspect as with any other Browserbase session:- Session recordings - View the Function execution in the Session Inspector
- Console logs - See all logged messages during execution
- Network activity - Inspect HTTP requests and responses
- Performance metrics - Monitor execution time and resource usage
Session recordings
Learn how to debug Functions using session recordings
Secrets
Secrets management for Functions environments is coming soon! This will allow you to securely store and access sensitive values like API keys, tokens, and credentials within your deployed Functions without passing them as parameters.Availability: Support for Secrets is a top priority on the current roadmap.
This feature isn’t yet available in beta; contact support@browserbase.com to get
on the waitlist and stay tuned for the latest updates.
Next steps
Session configuration
Learn about all available session configuration options
Verified
Configure browser identity for your functions
Browser contexts
Persist authentication and session state
API reference
Complete Browserbase API documentation