Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Include streaming example

magmueller64f27574b01f
diff --git a/docs/cloud/implementation.mdx b/docs/cloud/implementation.mdx
index 48dfa067d..ea5a1508d 100644
--- a/docs/cloud/implementation.mdx
+++ b/docs/cloud/implementation.mdx
@@ -8,48 +8,68 @@ This guide shows how to implement common API patterns using Python. We'll create
## Basic Implementation
-Here's a simple implementation using Python's `requests` library:
+Here's a simple implementation using Python's `requests` library to stream the task steps:
```python
-import requests
+import json
import time
-API_KEY = "your_api_key_here"
-BASE_URL = "https://api.browser-use.com/api/v1"
-HEADERS = {"Authorization": f"Bearer {API_KEY}"}
-
-def create_task(instructions):
- """Create a new browser automation task"""
- response = requests.post(
- f"{BASE_URL}/run-task",
- headers=HEADERS,
- json={"task": instructions}
- )
- return response.json()["id"]
-
-def get_task_status(task_id):
- """Get current task status"""
- response = requests.get(
- f"{BASE_URL}/task/{task_id}/status",
- headers=HEADERS
- )
- return response.json()
-
-def get_task_details(task_id):
- """Get full task details including output"""
- response = requests.get(
- f"{BASE_URL}/task/{task_id}",
- headers=HEADERS
- )
- return response.json()
-
-def wait_for_completion(task_id, poll_interval=2):
- """Poll task status until completion"""
- while True:
- status = get_task_status(task_id)
- if status in ["finished", "failed", "stopped"]:
- return get_task_details(task_id)
- time.sleep(poll_interval)
+import requests
+
+API_KEY = 'your_api_key_here'
+BASE_URL = 'https://api.browser-use.com/api/v1'
+HEADERS = {'Authorization': f'Bearer {API_KEY}'}
+
+
+def create_task(instructions: str):
+ """Create a new browser automation task"""
+ response = requests.post(f'{BASE_URL}/run-task', headers=HEADERS, json={'task': instructions})
+ return response.json()['id']
+
+
+def get_task_status(task_id: str):
+ """Get current task status"""
+ response = requests.get(f'{BASE_URL}/task/{task_id}/status', headers=HEADERS)
+ return response.json()
+
+
+def get_task_details(task_id: str):
+ """Get full task details including output"""
+ response = requests.get(f'{BASE_URL}/task/{task_id}', headers=HEADERS)
+ return response.json()
+
+
+def wait_for_completion(task_id: str, poll_interval: int = 2):
+ """Poll task status until completion"""
+ count = 0
+ unique_steps = []
+ while True:
+ details = get_task_details(task_id)
+ new_steps = details['steps']
+ # use only the new steps that are not in unique_steps.
+ if new_steps != unique_steps:
+ for step in new_steps:
+ if step not in unique_steps:
+ print(json.dumps(step, indent=4))
+ unique_steps = new_steps
+ count += 1
+ status = details['status']
+
+ if status in ['finished', 'failed', 'stopped']:
+ return details
+ time.sleep(poll_interval)
+
+
+def main():
+ task_id = create_task('Open https://www.google.com and search for openai')
+ print(f'Task created with ID: {task_id}')
+ task_details = wait_for_completion(task_id)
+ print(f"Final output: {task_details['output']}")
+
+
+if __name__ == '__main__':
+ main()
+
```
## Task Control Example