import requests
import time
api_key = "YOUR_API_KEY"
api_url = "https://api.twin.so"
# Task configuration
start_url = "https://www.google.com"
goal = "Find the latest price of AAPL stock. Return the stock price, nothing else."
# Start the task
response = requests.post(
f"{api_url}/browse",
headers={
'Content-Type': 'application/json',
'x-api-key': api_key
},
json={
"startUrl": start_url,
"goal": goal,
"outputType": "string"
}
)
# Get the task ID
task_id = response.json().get("taskId")
if not task_id:
print("Failed to start task.")
exit()
print(f"Started task with ID: {task_id}")
previous_action = None
# Poll for task status
while True:
response = requests.get(
f"{api_url}/task/{task_id}?limit=1",
headers={'x-api-key': api_key}
)
data = response.json()
status = data.get('status')
steps = data.get('steps', [])
if steps:
last_action = steps[-1].get('action')
if last_action != previous_action and last_action is not None:
print(f"Last action: {last_action}")
previous_action = last_action
if status in ['COMPLETED', 'FAILED']:
print(f"Task {status}.")
break
time.sleep(2)