-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateBulkFlowcodes.js
75 lines (64 loc) · 2.46 KB
/
createBulkFlowcodes.js
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
// Example JavaScript script demonstrating the functionality of bulk creating Flowcode (dynamic QR codes) via the Flowcode API.
// The script makes a POST request to the /v4/codes/bulk endpoint in the createFlowcodes function below.
// Before running this script, simply replace the 'apikey' value with your own API key.
// For more information and full documentation of all the Flowcode API endpoints, please visit our Developer Portal: https://developer.flowcode.com/
// Function to create many Flowcodes using the Bulk Code Creation API endpoint
async function createFlowcodes(apikey, codes, folderName) {
// Bulk Flowcode endpoint URL
const url = "https://gateway.flowcode.com/v4/codes/bulk";
const requestBody = {
codes: codes,
folder_name: folderName
};
const headers = {
"Content-Type": "application/json",
"apikey": apikey
};
// Initiate a POST API request to /v4/codes/bulk
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(requestBody)
});
// Throw an error if response was not successful
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Error: ${response.status} - ${errorText}`);
}
return await response.json();
}
// Example usage
async function main() {
const apikey = 'your_api_key'; // Please use your API key here
if (!apikey) {
throw new Error("API key not found. Please enter your valid API key.");
}
// New folder name where Flowcodes will be placed
const folderName = "My Cool Flowcodes";
const flowcodesData = [
// Add the Flowcodes you wish to create here, as JSON data
{
destination: {
destination_type: "URL",
redirect_value: { url: "https://www.flowcode.com/page/devapi" }
},
code_name: "My Cool Flowcode 1"
},
{
destination: {
destination_type: "URL",
redirect_value: { url: "https://www.flowcode.com/page/devapi-2" }
},
code_name: "My Cool Flowcode 2"
}
];
// Run the function to create the Flowcodes
try {
const result = await createFlowcodes(apikey, flowcodesData, folderName);
console.log("Success!", result);
} catch (error) { // Handle exceptions
console.error(`Request error: ${error.message}`);
}
}
// Run the example
main();