Full source code
Clone and run in minutes. Next.js + Browser Use SDK v3.
- Home — the user types a task, the app creates a session and sends the task.
- Session — live browser preview, streaming messages, follow-ups, and recording download.
src/lib/api.ts.
Setup
api.ts
The API key uses
BROWSER_USE_API_KEY (no NEXT_PUBLIC_ prefix) so it stays server-side. All SDK calls go through server actions — never call the SDK directly from client components.1. Create a session
actions.ts
keepAlive: truekeeps the session open after each task so the user can send follow-ups (default isfalse).enableRecording: trueproduces an MP4 video of the browser session.liveUrlis returned immediately — no waiting or extra call needed.
liveUrl and the initial task via URL params), and the session page takes over from there:
page.tsx
2. Stream messages with for await
Instead of polling sessions.get() and sessions.messages() separately, use client.run() — it streams messages and resolves when the task completes:
session-context.tsx
for await loop yields each message as it arrives. When the loop ends, run.result contains the final session state (status, output, etc.). No separate status polling needed.
Wire it up in a useEffect to auto-run the initial task from URL params:
session-context.tsx
3. Follow-up tasks
Follow-ups call the samestreamTask function — the stream already includes the user message, so no optimistic insert is needed:
session-context.tsx
keepAlive: true when targeting an existing session, so follow-up tasks work without extra config.
4. Recording
Fetch the MP4 URL after the session ends (recording was enabled in step 1):session-context.tsx
waitForRecording polls for up to 15 seconds and returns presigned MP4 download URLs. Returns an empty array if the agent answered without opening a browser.
5. Stop a task
actions.ts
strategy: "task" stops only the current task, keeping the session alive for follow-ups.
6. Session page
The session page consumes everything through a context provider:session/[id]/page.tsx
Summary
| Method | Purpose |
|---|---|
client.sessions.create() | Create a session (returns liveUrl immediately) |
client.run() | Send a task and stream messages with for await |
client.sessions.stop() | Stop the current task |
client.sessions.waitForRecording() | Get MP4 recording URLs |