Skip to main content

Check out the Stagehand Next.js Quickstart

Clone our GitHub repo to get started with Stagehand in Next.js.

Add Stagehand to an existing Next.js project

If you’d like to add Stagehand to an existing Next.js project, you can do so by installing the dependencies:

Write a server action

Next, let’s define our main function as a server action in app/stagehand/main.ts. This file will have the following three functions:
  1. main: Run the main Stagehand script
  2. runStagehand: Initialize and run the main function
  3. startBBSSession: Start a Browserbase session
app/stagehand/main.ts

Create a client component

Next, let’s create a client component that will start a Browserbase session and run the main function with the server actions we just defined. We’ll first create a Browserbase session and embed the session in an iframe before running the main function.
app/components/stagehandEmbed.tsx

Use the StagehandEmbed component

Now, we can use the StagehandEmbed component in our app.
app/page.tsx