Last active
December 4, 2022 03:19
-
-
Save isleshocky77/dd590cd092cead9b1f120954a0d8d187 to your computer and use it in GitHub Desktop.
Sense API Usage with cURL (and jq)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SENSE_EMAIL="" | |
SENSE_PASSWORD="" | |
SENSE_API_URI="https://api.sense.com/apiservice/api/v1" | |
SENSE_ACCESS_TOKEN=$(curl -sq "$SENSE_API_URI/authenticate" \ | |
--data "email=$SENSE_EMAIL" \ | |
--data "password=$SENSE_PASSWORD" \ | |
| jq -r '.access_token') | |
SENSE_USER_ID=$(curl -sq "$SENSE_API_URI/authenticate" \ | |
--data "email=$SENSE_EMAIL" \ | |
--data "password=$SENSE_PASSWORD" \ | |
| jq -r '.user_id') | |
# Only working with the first (and maybe only Monitor) | |
SENSE_MONITOR_0_ID=$(curl -sq "$SENSE_API_URI/authenticate" \ | |
--data "email=$SENSE_EMAIL" \ | |
--data "password=$SENSE_PASSWORD" \ | |
| jq -r '.monitors[0].id') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get Always On | |
curl -sq --get "$SENSE_API_URI/app/monitors/$SENSE_MONITOR_0_ID/devices/always_on" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
| jq '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get Other | |
curl -sq --get "$SENSE_API_URI/app/monitors/$SENSE_MONITOR_0_ID/devices/unknown" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
| jq '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get First Device Detail | |
SENSE_DEVICE_0_ID="$(curl -sq --get "$SENSE_API_URI/app/monitors/$SENSE_MONITOR_0_ID/devices" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
| jq -r '.[0].id')" | |
# Get Active Device | |
SENSE_DEVICE_1_ID="$(curl -sq --get "$SENSE_API_URI/app/monitors/$SENSE_MONITOR_0_ID/devices" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
| jq -r 'first(.[] | select(.tags.Pending == "false" and .tags.Revoked == "false")).id')" | |
curl -sq --get "$SENSE_API_URI/app/monitors/$SENSE_MONITOR_0_ID/devices/$SENSE_DEVICE_0_ID" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
| jq '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get Devices | |
curl -sq --get "$SENSE_API_URI/app/monitors/$SENSE_MONITOR_0_ID/devices" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
| jq '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get Monitor History | |
curl -sq --get "$SENSE_API_URI/app/history/usage" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
--data "monitor_id=$SENSE_MONITOR_0_ID" \ | |
--data "granularity=minute" \ | |
--data "frames=5400" \ | |
--data "start=$(date -Iseconds --date="last week")" \ | |
| jq '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get Monitor Status | |
curl -sq "$SENSE_API_URI/app/monitors/$SENSE_MONITOR_0_ID/status" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
jq '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get Trends | |
curl -sq --get "$SENSE_API_URI/app/history/trends" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
--data "monitor_id=$SENSE_MONITOR_0_ID" \ | |
--data "device_id=$SENSE_DEVICE_1_ID" \ | |
--data "scale=hour" \ | |
--data "start=$(date -Iseconds --date="last week")" \ | |
| jq '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get TimeLine | |
curl -sq --get "$SENSE_API_URI/users/$SENSE_USER_ID/timeline" \ | |
-H "Authorization: Bearer $SENSE_ACCESS_TOKEN" \ | |
| jq '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get the realtime feed | |
curl \ | |
--include \ | |
--no-buffer \ | |
--header "Connection: Upgrade" \ | |
--header "Upgrade: websocket" \ | |
--header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \ | |
--header "Sec-WebSocket-Version: 13" \ | |
--output - \ | |
"https://clientrt.sense.com/monitors/$SENSE_MONITOR_0_ID/realtimefeed?access_token=$SENSE_ACCESS_TOKEN" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment