-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.runTaskByTemplate.js
More file actions
119 lines (102 loc) · 5.13 KB
/
9.runTaskByTemplate.js
File metadata and controls
119 lines (102 loc) · 5.13 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* 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"}'
*/
const https = require('https');
function main() {
// API Key Required for API Call, generated from: https://www.browseract.com/reception/integrations
const authorization = "app-abcdefghijklmn";
// workflow template ID, you can get it from: https://www.browseract.com/reception/workflow-list
// Or use the API: GET /v2/workflow/list-official-workflow-templates
const workflowTemplateId = "1234567890";
try {
// Create request data
const data = JSON.stringify({
// The workflow template ID used to create and spawn a new task.
"workflow_template_id": workflowTemplateId,
// 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"
});
const options = {
hostname: 'api.browseract.com',
port: 443,
path: '/v2/workflow/run-task-by-template',
method: 'POST',
headers: {
'Authorization': `Bearer ${authorization}`,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
const req = https.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
if (res.statusCode === 200) {
// success example:
// {'id': '12425895140306551', 'profileId': 'abcde'}
console.log('api-call-ok:', responseData);
const response = JSON.parse(responseData);
const taskId = response.id;
console.log('Task ID:', taskId);
// Polling the task status until the task is completed or timed out.
// Please refer to "3.getTask.js" or "4.getTaskStatus.js"
} else {
// error example:
// {"code": 401, "msg": "Invalid authorization", "data": null, "ts": 1759917250113, "time": "2025-10-08 09:54:10", "traceId": "bcdef"}
// {"code": 10118, "msg": "Running tasks number exceeds.", "data": null, "ts": 1759917310153, "time": "2025-10-08 09:55:10", "traceId": "cdefg"}
console.log(`api-call-error: status=${res.statusCode}`, responseData);
}
});
});
req.on('error', (error) => {
console.log('run-error:', error.message);
});
req.write(data);
req.end();
} catch (error) {
console.log('run-error:', error.message);
}
}
main();