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:- npm
- pnpm
- yarn
Write a server action
Next, let’s define ourmain
function as a server action in app/stagehand/main.ts
. This file will have the following three functions:
main
: Run the main Stagehand scriptrunStagehand
: Initialize and run themain
functionstartBBSSession
: 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 themain
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