Overview
Functions let you deploy your existing browser automation scripts to Browserbase’s serverless infrastructure. Instead of running scripts on your own servers, you can publish them as cloud functions that are invoked via API. This guide shows you how to take an existing automation script and deploy it as a Function using your preferred framework.Prerequisites
Before deploying, ensure you have:- An existing TypeScript browser automation script using Stagehand, Playwright, or Puppeteer
- A Browserbase account with your API key and Project ID (available in Settings)
Create Function Scaffolding
First, initialize a new Functions project. This creates the directory structure, installs dependencies, and sets up the required configuration files.Initialize Your Project
Run the following command to create a new functions project:This creates a
my-functions-project directory with:- A configured
package.jsonwith required dependencies - A
tsconfig.jsonfor TypeScript support - A template
.envfile for your credentials - A starter
index.tsfile with an example function
Configure Environment Variables
Open the
.env file and add your Browserbase credentials:Get your API key and Project ID from the Browserbase Dashboard Settings.
Adapting Your Script
The key difference between running locally and deploying as a Function is how you connect to the browser. In a function, Browserbase automatically creates a session and provides the connection details through thecontext parameter.
Using the Function Context
Every Function receives acontext object containing session information:
Framework Integration
Choose your framework below to see how to adapt your existing script for deployment.- Stagehand
- Playwright
- Puppeteer
Stagehand is the recommended framework for Functions. Use Function:
env: "LOCAL" mode and pass the CDP connection URL from context.session.connectUrl via localBrowserLaunchOptions.Local Script:Using
env: "LOCAL" with localBrowserLaunchOptions.cdpUrl connects Stagehand directly to the browser session via CDP. You can still use other Stagehand options alongside this configuration. See the Stagehand documentation for all available configuration options.Testing Locally
Before deploying, test your function locally using the development server:The local development server creates real Browserbase sessions using your credentials. This ensures your function behaves the same locally and in production.