In this topic we will learn how to access the cayenne API from 3 different platforms:
- Insomnia: is a cross-platform GraphQL and REST client, available for Mac, Windows, and Linux
- Node-red: Is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways.
- Thunkable: Is a platform where anyone can build their own mobile apps. Available for iOS and Android.
Get The Keys.
you need to get the APP KEY and APP SECRET from cayenne dashboard by clicking on Create app and Generate API Keys.
Where APP key == client_id
and APP secret == client secret
.
Get front end token
You need to get the base64 string of your username and password. Go to Base64 encode and base64 decode online and enter your MQTT username:password and encode it.
Get the JWT access token
1) Insomnia.
curl --request POST \
--url https://accounts.mydevices.com/auth/realms/cayenne/protocol/openid-connect/token \
--header 'content-type: application/x-www-form-urlencoded' \
--data 'grant_type=password&client_id=APP key&client_secret=APP Secret&username=email_id&password=passwordâ
In the above curl you need to add the following: app key, app secret, your cayenne username and password.
2) Node-red.
[{"id":"54410621.96d1f8","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"8a3392ed.acac1","type":"inject","z":"54410621.96d1f8","name":"Get Access Token.","topic":"","payload":"{}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":232,"wires":[["a8014528.b74688"]]},{"id":"54c4d950.77bc58","type":"debug","z":"54410621.96d1f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":823.5,"y":222,"wires":[]},{"id":"baad932f.35aec","type":"change","z":"54410621.96d1f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.access_token","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640.5,"y":222,"wires":[["54c4d950.77bc58"]]},{"id":"a8e98562.d7ca58","type":"http request","z":"54410621.96d1f8","name":"","method":"POST","ret":"obj","url":"https://accounts.mydevices.com/auth/realms/cayenne/protocol/openid-connect/token","tls":"","x":464,"y":223,"wires":[["baad932f.35aec"]]},{"id":"a8014528.b74688","type":"function","z":"54410621.96d1f8","name":"SET","func":"msg.payload = {};\nmsg.payload['grant_type'] = 'password';\nmsg.payload['client_id'] = '<app key>';\nmsg.payload['client_secret'] = '<app secret>';\nmsg.payload['username'] = '<email>';\nmsg.payload['password'] = '<password>';\nmsg.headers = {};\nmsg.headers['content-type'] = 'application/x-www-form-urlencoded';\nreturn msg;","outputs":1,"noerr":0,"x":302.5,"y":233,"wires":[["a8e98562.d7ca58"]]}]
You need to edit the set node and add your app key, app secret, your cayenne username and password.
3) Thunkable
Get the refresh token.
-
Insomnia
curl --request POST
âurl https://accounts.mydevices.com/auth/realms/cayenne/protocol/openid-connect/token
âheader âcontent-type: application/x-www-form-urlencodedâ
âdata 'grant_type=refresh_token&client_id= &client_secret=&refresh_token=<refresh_token>â
2) Node-red
[{"id":"54410621.96d1f8","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"8a3392ed.acac1","type":"inject","z":"54410621.96d1f8","name":"Get Access Token.","topic":"","payload":"{}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":232,"wires":[["a8014528.b74688"]]},{"id":"54c4d950.77bc58","type":"debug","z":"54410621.96d1f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":823.5,"y":222,"wires":[]},{"id":"baad932f.35aec","type":"change","z":"54410621.96d1f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.access_token","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640.5,"y":222,"wires":[["54c4d950.77bc58"]]},{"id":"a8e98562.d7ca58","type":"http request","z":"54410621.96d1f8","name":"","method":"POST","ret":"obj","url":"https://accounts.mydevices.com/auth/realms/cayenne/protocol/openid-connect/token","tls":"","x":464,"y":223,"wires":[["baad932f.35aec"]]},{"id":"a8014528.b74688","type":"function","z":"54410621.96d1f8","name":"SET","func":"msg.payload = {};\nmsg.payload['grant_type'] = 'refresh_token';\nmsg.payload['client_id'] = '<app key>';\nmsg.payload['client_secret'] = '<app secret>';\nmsg.payload['refresh_token'] = '<refresh token>';\nmsg.headers = {};\nmsg.headers['content-type'] = 'application/x-www-form-urlencoded';\nreturn msg;â,"outputs":1,"noerr":0,"x":302.5,"y":233,"wires":[["a8e98562.d7ca58"]]}]
3) Thunkable
Publish data.
-
Insomnia
curl --request POST \ --url https://api.mydevices.com/things/clientid \ --header 'authorization: Basic token' \ --header 'content-type: application/json' \ --data '[{ "value": 12, "channel": 11, "unit": "c", "type": "temp" }]'
2) Node-red
[{"id":"54410621.96d1f8","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"a7c1b2f7.8aa7e","type":"function","z":"54410621.96d1f8","name":"","func":"var access = msg.payload;\nmsg.payload = [{\n \"value\": 12,\n \"channel\": 11,\n \"unit\": \"c\",\n \"type\": \"temp\"\n}];\nmsg.headers = {};\nmsg.headers['Authorization'] = âBasic <api_token>â;\nmsg.headers['content-type'] = 'application/json'\nreturn msg;","outputs":1,"noerr":0,"x":311,"y":379,"wires":[["a56542d5.6cba9"]]},{"id":"a56542d5.6cba9","type":"http requestâ,"z":"54410621.96d1f8","name":"","method":"POST","ret":"txt","url":"https://api.mydevices.com/things/<client_id>/data","tls":"","x":481,"y":381,"wires":[["30f3f465.7d167c"]]},{"id":"30f3f465.7d167c","type":"debug","z":"54410621.96d1f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":666,"y":384,"wires":[]},{"id":"61891662.991c48","type":"inject","z":"54410621.96d1f8","name":"","topic":"","payload":"{}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":125.5,"y":372,"wires":[["a7c1b2f7.8aa7e"]]}]
You need to edit the function node:
[{
"value": 12,
"channel": 11,
"unit": "c",
"type": "temp"
}]
msg.headers = {};
msg.headers['Authorization'] = 'Bearer <front_end_token>â;
return msg;
3) Thunkable
Control Actuator
-
Insomnia
curl --request POST \ --url https://platform.mydevices.com/v1.1/things/device_id/cmd \ --header 'authorization: Bearer JWT token' \ --header 'content-type: application/json' \ --data '{ "channel": 1, "value": 1 }â
-
Node-red
[{"id":"65f7a535.a68acc","type":"function","z":"1c439d4.cbaba63","name":"","func":"var access = msg.payload;\nmsg.payload = {\n \"channel\": \"1\",\n \"value\": 1\n};\nmsg.headers = {};\nmsg.headers['Authorization'] = 'Bearer JWT token';\nmsg.headers['Content-Type'] = 'application/json';\nreturn msg;","outputs":1,"noerr":0,"x":302,"y":175,"wires":[["456f8bbd.161344"]]},{"id":"456f8bbd.161344","type":"http requestâ,âz":"1c439d4.cbaba63","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://platform.mydevices.com/v1.1/things/device_id/cmd","tls":"","proxy":"","x":472,"y":177,"wires":[["5b6dfaf8.901a44"]]},{"id":"5b6dfaf8.901a44","type":"debug","z":"1c439d4.cbaba63","name":"","active":true,"console":false,"complete":"payload","x":677,"y":180,"wires":[]},{"id":"b082d35d.50d73","type":"inject","z":"1c439d4.cbaba63","name":"","topic":"","payload":"{}","payloadType":"json","repeat":"","crontab":"","once":false,"x":116.5,"y":168,"wires":[["65f7a535.a68acc"]]}]
- Thunkable
Get History.
-
Insomnia
curl --request GET \ --url âhttps://platform.mydevices.com/v1.1/telemetry/clientid/sensors/thingid/summaries?type=latest' \ --header 'authorization: Bearer FE Token'
-
Node-red
[{"id":"a18cabde.5b2b38","type":"function","z":"23dcad07.045b32","name":"","func":"var access = msg.payload;\nmsg.headers = {};\nmsg.headers['Authorization'] = âBearer token';\nreturn msg;","outputs":1,"noerr":0,"x":323,"y":121,"wires":[["a1fc2447.b6cb08"]]},{"id":"a1fc2447.b6cb08","type":"http request","z":"23dcad07.045b32","name":"","method":"GET","ret":"txt","url":"https://platform.mydevices.com/v1.1/telemetry/deviceId/sensors/sensorIDsummaries?type=latest","tls":"","x":493,"y":123,"wires":[["e5d991cb.6c62f"]]},{"id":"e5d991cb.6c62f","type":"debug","z":"23dcad07.045b32","name":"","active":true,"console":false,"complete":"payload","x":698,"y":126,"wires":[]},{"id":"9323b1ae.79286","type":"inject","z":"23dcad07.045b32","name":"","topic":"","payload":"{}","payloadType":"json","repeat":"","crontab":"","once":false,"x":137.5,"y":114,"wires":[["a18cabde.5b2b38"]]}]
- Thunkable