You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/browser-rendering/platform/stagehand.mdx
+43-14Lines changed: 43 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,25 +17,33 @@ import {
17
17
18
18
[Stagehand](https://www.stagehand.dev/) is an open-source, AI-powered browser automation library. Rather than dictating exact steps or specifying selectors, Stagehand enables developers to build more reliably and flexibly by combining code with natural-language instructions powered by AI. This makes your agents more resilient to website changes and easier to maintain.
19
19
20
+
This guide shows you how to deploy a Worker that uses Stagehand, Browser Rendering, and [Workers AI](/workers-ai/) to automate a web task.
21
+
20
22
## Use Stagehand in a Worker
21
23
22
-
In this example, you will search for a movie on https://www.themoviedb.org/ and return the movie's title, year, and director, as well as a screenshot of the webpage.
24
+
In this example, you will use Stagehand to search for a movie on [The Movie Database](https://www.themoviedb.org/), extract its details (title, year, and director), and return the information along with a screenshot of the webpage.
25
+
26
+
If you want to skip the steps and get started quickly, select **Deploy to Cloudflare** below.
27
+
28
+
[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/playwright-mcp/tree/main/cloudflare/example)
23
29
24
30
After you deploy, you can interact with the Worker using this URL pattern:
25
31
```
26
32
https://<your-worker>.workers.dev
27
33
```
28
34
29
-
If you want to skip the steps and get started quickly, select **Deploy to Cloudflare** below.
35
+
#### 1. Set up your project
30
36
31
-
[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/playwright-mcp/tree/main/cloudflare/example)
37
+
???DON'T THEY NEED TO INSRALL BROWSERBASE STAGEHAND TOO?
32
38
33
-
1.Install dependencies:
39
+
Install the necessary dependencies:
34
40
```bash
35
41
npm ci
36
42
```
37
43
38
-
2. Include the [browser rendering](/browser-rendering/) binding in your wrangler configuration file:
44
+
#### 2. Configure your Worker
45
+
46
+
Update your wrangler configuration file to include the bindings for Browser Rendering and [Workers AI](/workers-ai/):
39
47
<WranglerConfig>
40
48
```jsonc
41
49
{
@@ -62,7 +70,7 @@ If you want to skip the steps and get started quickly, select **Deploy to Cloudf
62
70
```
63
71
</WranglerConfig>
64
72
65
-
3.Edit the code:
73
+
#### 3. Write the Worker code
66
74
67
75
```ts title="src/index.ts"
68
76
@@ -140,34 +148,56 @@ If you want to skip the steps and get started quickly, select **Deploy to Cloudf
140
148
},
141
149
};
142
150
```
143
-
4. Build the project:
151
+
#### 4. Build the project
152
+
144
153
```bash
145
154
npm run build
146
155
```
147
-
5. Deploy to Cloudflare Workers:
156
+
#### 5. Deploy to Cloudflare Workers
157
+
158
+
After you deploy, you can interact with the Worker using this URL pattern:
159
+
```
160
+
https://<your-worker>.workers.dev
161
+
```
162
+
148
163
```bash
149
164
npm run deploy
150
165
```
151
166
152
-
## Use a custom model
167
+
## Use Cloudflare AI Gateway
168
+
169
+
[AI Gateway](/ai-gateway/) is a service that adds observability to your AI applications. By routing your requests through AI Gateway, you can monitor and debug your AI applications.
170
+
171
+
You can use AI Gateway with both Workers AI and third-party models.
153
172
154
-
Instead of using a model from [Workers AI](/workers-ai/), you can use a custom model for which you supply your own credentials.
173
+
To use AI Gateway, first create a gateway in the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/ai-gateway), then add the `baseURL` as shown below.
155
174
156
-
In this example, you will configure Stagehand to use [OpenAI](https://openai.com/). You will need an OpenAI API key. Cloudflare recommends storing your API key with [secrets](/workers/configuration/secrets/).
175
+
???I SWITCHED THE ORDER AROUND. WE SHOULD FIRST SHOW AI GATEWAY WHILE STILL USING WORKERS AI
You can configure Stagehand to use models from third-party providers, including OpenAI or Anthropic, by providing your own credentials.
170
191
192
+
???DOES THIS ONLY WORK WITH CERTAIN PROVIDERS? ONLY MODELS THAT AIGATEWAY SUPPORTS?
193
+
194
+
In this example, you will configure Stagehand to use [OpenAI](https://openai.com/). You will need an OpenAI API key. Cloudflare recommends storing your API key as a [secret](/workers/configuration/secrets/).
195
+
196
+
```bash
197
+
npx wrangler secret put OPENAI_API_KEY
198
+
```
199
+
200
+
Then, configure Stagehand with your provider, model, and API key.
171
201
172
202
```typescript
173
203
const stagehand =newStagehand({
@@ -176,7 +206,6 @@ const stagehand = new Stagehand({
0 commit comments