Device Creation

Node-RED Example

Function Node Code

  • The following code can be used as a function node.
  • It creates a new and empty Device using the "light" plan.
  • You will need billing enabled on the workspace and the token you are using for this action needs to have device creation and billing rights.
1
// Datacake API Token
2
var token = "yourdatacaketoken";
3
4
// Datacake Product UUID
5
var productUUID = "datacakeproductid";
6
7
// Datacake Workspace UUID
8
var workspaceUUID = "workspaceid";
9
10
// GraphQL API URL
11
msg.url = "https://api.datacake.co/graphql/";
12
13
// GraphQL Header
14
msg.headers = {
15
"Authorization": "Token "+token,
16
"Content-Type": "application/json"
17
};
18
19
var devEUI = "ABCD01020304DEFG";
20
var devName = "My LoRaWAN Device";
21
22
// Query Insert
23
var devicesString = '[';
24
devicesString += '{';
25
devicesString += 'devEui: "';
26
devicesString += devEUI
27
devicesString += '", name: "';
28
devicesString += devName;
29
devicesString += '"}';
30
devicesString += "]";
31
32
// GraphQL Query to create a new and empty LoRaWAN Device
33
var query = `mutation {
34
createLoraDevices(input:{
35
workspace:"${workspaceUUID}",
36
plan:"light",
37
planCode:"",
38
productKind:NEW,
39
devices:${devicesString},
40
}) {
41
ok
42
devices {
43
id
44
verboseName
45
serialNumber
46
}
47
}
48
}
49
}`;
50
51
msg.payload = {"query": query};
52
msg.body = {"query": query};
53
54
return msg;
Copied!

Node-RED Flow

  • You can copy the following JSON snippet and import this into your Node-RED Flow.
nodered-export.json
1
[{"id":"4d205328.8b217c","type":"tab","label":"Create Device via GraphQL","disabled":false,"info":""},{"id":"58d8e67b.c544f8","type":"function","z":"4d205328.8b217c","name":"Create Device Mutation","func":"// Datacake API Token\nvar token = \"yourdatacaketoken\";\n\n// Datacake Product UUID\nvar productUUID = \"datacakeproductid\";\n\n// Datacake Workspace UUID\nvar workspaceUUID = \"workspaceid\";\n\n// GraphQL API URL\nmsg.url = \"https://api.datacake.co/graphql/\";\n\n// GraphQL Header\nmsg.headers = {\n \"Authorization\": \"Token \"+token,\n \"Content-Type\": \"application/json\"\n};\n\nvar devEUI = \"ABCD01020304DEFG\";\nvar devName = \"My LoRaWAN Device\";\n\n// Query Insert\nvar devicesString = '[';\ndevicesString += '{';\ndevicesString += 'devEui: \"';\ndevicesString += devEUI\ndevicesString += '\", name: \"';\ndevicesString += devName;\ndevicesString += '\"}';\ndevicesString += \"]\";\n\n// GraphQL Query to create a new and empty LoRaWAN Device\nvar query = `mutation {\n createLoraDevices(input:{\n workspace:\"${workspaceUUID}\",\n plan:\"light\",\n planCode:\"\",\n productKind:NEW,\n devices:${devicesString},\n }) {\n ok\n devices {\n id\n verboseName\n serialNumber\n }\n }\n }\n}`;\n\nmsg.payload = {\"query\": query};\nmsg.body = {\"query\": query};\n\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":120,"wires":[["737b20cd.93c3d"]]},{"id":"737b20cd.93c3d","type":"http request","z":"4d205328.8b217c","name":"","method":"POST","ret":"obj","paytoqs":true,"url":"","tls":"ff660e1f.b655c","persist":false,"proxy":"","authType":"","x":590,"y":120,"wires":[["3a37f823.ee1eb8"]]},{"id":"3a37f823.ee1eb8","type":"debug","z":"4d205328.8b217c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":120,"wires":[]},{"id":"2251e6d7.17e33a","type":"inject","z":"4d205328.8b217c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":120,"wires":[["58d8e67b.c544f8"]]},{"id":"ff660e1f.b655c","type":"tls-config","z":"","name":"","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"","servername":"","verifyservercert":true}]
Copied!