Skip to content

Commit c63b197

Browse files
[docs]: adding extended example to typescript quickstart + change to use port 3002 for dev (#896)
# why It's hard to find a quickstart for TS if you want to add it to an existing project/script. # what changed Adding extended example to typescript quickstart + change to use port 3002 for dev # test plan Sending to my friend to see if its good enough --------- Co-authored-by: miguel <[email protected]>
1 parent a6eaea8 commit c63b197

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

docs/get_started/quickstart.mdx

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,87 @@ Use the package manager of your choice to install the dependencies. We also have
174174
</Step>
175175
</Steps>
176176

177+
### Adding Stagehand to an existing project:
178+
179+
<Steps>
180+
181+
<Step title="Install Stagehand">
182+
183+
```bash
184+
npm install @browserbasehq/stagehand
185+
```
186+
187+
</Step>
188+
189+
<Step title="Set up environment variables">
190+
191+
Create a `.env` file or export environment variables:
192+
193+
```bash
194+
export BROWSERBASE_API_KEY="your_browserbase_api_key"
195+
export BROWSERBASE_PROJECT_ID="your_browserbase_project_id"
196+
export OPENAI_API_KEY="your_openai_api_key" # Configurable to other models
197+
```
198+
199+
</Step>
200+
201+
<Step title="Create your first script">
202+
203+
Create a file `index.ts`:
204+
205+
```typescript
206+
// index.ts
207+
import { Stagehand, ConstructorParams } from "@browserbasehq/stagehand";
208+
import dotenv from "dotenv";
209+
import { z } from "zod";
210+
211+
dotenv.config();
212+
213+
export async function main() {
214+
const config: ConstructorParams = {
215+
env: "BROWSERBASE", // or "LOCAL"
216+
apiKey: process.env.BROWSERBASE_API_KEY,
217+
projectId: process.env.BROWSERBASE_PROJECT_ID,
218+
verbose: 1,
219+
};
220+
221+
const stagehand = new Stagehand(config);
222+
223+
try {
224+
await stagehand.init();
225+
const page = stagehand.page;
226+
227+
await page.goto("https://docs.stagehand.dev/");
228+
await page.act("click the quickstart link");
229+
230+
const result = await page.extract({
231+
instruction: "extract the main heading of the page",
232+
schema: z.object({
233+
heading: z.string(),
234+
}),
235+
});
236+
237+
console.log(`Extracted: ${result.heading}`);
238+
} catch (error) {
239+
console.error(error);
240+
} finally {
241+
await stagehand.close();
242+
}
243+
}
244+
245+
main();
246+
```
247+
</Step>
248+
249+
<Step title="Run the script">
250+
251+
```bash
252+
npx tsx index.ts
253+
```
254+
255+
</Step>
256+
</Steps>
257+
177258
</Tab>
178259

179260
<Tab title="Python">

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"dev": "mintlify dev --no-open",
7+
"dev": "mintlify dev --no-open --port 3002",
88
"upgrade": "mintlify upgrade"
99
},
1010
"keywords": [],

examples/example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ async function example(stagehand: Stagehand) {
2323
await stagehand.init();
2424
await example(stagehand);
2525
await stagehand.close();
26-
})();
26+
})();

0 commit comments

Comments
 (0)