# main.py
import os
import asyncio
import requests
from browser_use import Agent, Browser, ChatBrowserUse
async def example():
# 1) Create a Browser Cash session and get its CDP URL
resp = requests.post(
"https://api.browser.cash/v1/browser/session",
headers={
"Authorization": f"Bearer {os.getenv('BROWSER_CASH_API_KEY')}",
"content-type": "application/json",
},
json={},
)
resp.raise_for_status()
cdp_url = resp.json()["cdpUrl"]
# 2) Create a Browser Use Browser object with the Browser Cash browser URL
browser = Browser(cdp_url=cdp_url)
llm = ChatBrowserUse()
# 3) Create and run an agent with the browser
agent = Agent(
task="Find the number of stars of the browser-use repo",
llm=llm,
browser=browser,
)
history = await agent.run()
# 4) Stop the Browser Cash session
requests.delete(
"https://api.browser.cash/v1/browser/session",
headers={
"Authorization": f"Bearer {os.getenv('BROWSER_CASH_API_KEY')}",
},
params={"sessionId": resp.json()["sessionId"]},
)
# 5) Return the agent's history
return history
if __name__ == "__main__":
history = asyncio.run(example())