How to access Cayenne API using Insomnia, Node-red and Thunkable

#1

In this topic we will learn how to access the cayenne API from 3 different platforms:

  1. Insomnia: is a cross-platform GraphQL and REST client, available for Mac, Windows, and Linux
  2. Node-red: Is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways.
  3. 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.

image

image

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 http://www.utilities-online.info/base64/#.XIkQx88zZR0 and enter your MQTT username:password and encode it.

image

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

image
image
image

Get the refresh 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=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'] = '&lt;app key&gt;';\nmsg.payload['client_secret'] = '&lt;app secret&gt;';\nmsg.payload['refresh_token'] = '&lt;refresh token&gt;';\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

image

Publish data.

  1. 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 &lt;api_token&gt;’;\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/&lt;client_id&gt;/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 &lt;front_end_token&gt;’;
return msg;

3) Thunkable

image

Control Actuator

  1. 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
    }’
    
  2. 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"]]}]

  1. Thunkable

image

Get History.

  1. Insomnia

    curl --request GET \
    --url ‘https://platform.mydevices.com/v1.1/telemetry/clientid/sensors/thingid/summaries?type=latest' \
    --header 'authorization: Bearer FE Token'
    
  2. 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"]]}]

  1. Thunkable

image

2 Likes

CayExcel - REST API Tutorial / Demo using Excel VBA
CayExcel - REST API Tutorial / Demo using Excel VBA
How can I read a field and show it on another site?