-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.run_task_by_template.py
More file actions
92 lines (78 loc) · 4.19 KB
/
9.run_task_by_template.py
File metadata and controls
92 lines (78 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Description:
Start a new workflow task using an official template and return a task ID for progress tracking.
Documentation:
https://www.browseract.com/reception/integrations/api-workflow
curl -X POST 'https://api.browseract.com/v2/workflow/run-task-by-template' -H 'Authorization: Bearer app-abcdefghijklmn' -H 'Content-Type: application/json' -d '{"workflow_template_id": "1234567890","input_parameters": [{"name": "target_url","value": "https://www.google.com/search?q=iphone17"},{"name": "product_limit","value": "10"}],"callback_url":"https://www.mydomain.com/callback"}'
"""
import traceback
import requests
def main():
# API Key Required for API Call, generated from: https://www.browseract.com/reception/integrations
authorization = "app-abcdefghijklmn"
# workflow template ID, you can get it from: https://www.browseract.com/template?platformType=0
# Or use the API: GET /v2/workflow/list-official-workflow-templates
workflow_template_id = "1234567890"
try:
headers = {
"Authorization": f"Bearer {authorization}"
}
# define API parameters
data = {
# The workflow template ID used to create and spawn a new task.
"workflow_template_id": workflow_template_id,
# Parameters entered when running a workflow task,
# which are defined by the template
"input_parameters": [{
# First parameter's name
"name": "target_url",
# First parameter's value
"value": "https://www.google.com/search?q=iphone17",
},{
# Second parameter's name
"name": "product_limit",
# Second parameter's value
"value": "10",
}],
# HTTP/HTTPS URL to receive task completion notifications via POST request.
# The callback payload structure is identical to the "Get Task" API response.
# Triggered when: Task completes, fails, or is canceled.
# Requirements:
# - Valid HTTP/HTTPS URL (max 2048 characters)
# - Publicly accessible endpoint
# - Must return 2xx status within 30 seconds
# - Redirects (3xx) are not allowed
# Retry: Automatic retry (max 3 attempts) for 5xx errors only.
"callback_url": "https://www.mydomain.com/task_finish_callback",
# HTTP/HTTPS URL to receive task status change notifications via POST request.
# The callback payload structure is identical to the "Get Task" API response.
# Triggered when: Task running, paused, finished, canceled, failed.
# Requirements:
# - Valid HTTP/HTTPS URL (max 2048 characters)
# - Publicly accessible endpoint
# - Must return 2xx status within 30 seconds
# - Redirects (3xx) are not allowed
# Retry: Automatic retry (max 3 attempts) for 5xx errors only.
"status_change_callback_url": "https://www.mydomain.com/task_status_change_callback"
}
api_url = "https://api.browseract.com/v2/workflow/run-task-by-template"
response = requests.post(
api_url, json=data, headers=headers, timeout=30
)
if response.status_code == 200:
# success example:
# {'id': '12425895140306551', 'profileId': 'abcde'}
print("api-call-ok:", response.json())
task_id = response.json()["id"]
# Polling the task status until the task is completed or timed out.
# Please refer to "3.get_task.py" or "4.get_task_status.py"
else:
# error example:
# {'code': 401, 'msg': 'Invalid authorization', 'data': None, 'ts': 1759917250113, 'time': '2025-10-08 09:54:10', 'traceId': 'bcdef'}
# {'code': 10118, 'msg': 'Running tasks number exceeds.', 'data': None, 'ts': 1759917310153, 'time': '2025-10-08 09:55:10', 'traceId': 'cdefg'}
print(f"api-call-error: status={response.status_code}", response.json())
except Exception:
error = traceback.format_exc()
print(f"run-error: {error}")
if __name__ == "__main__":
main()