Skip to content

data stream object

Jason Carvalho edited this page Jan 25, 2021 · 5 revisions

Data Stream API endpoints: object

All the main CRUD (create, read, update, delete) operations are handled using the object endpoint of the API.

Read

Read docs...

Write

Pushing new data to a dataset is performed through a HTTP POST request, using Basic Authentication, with the dataset access key being supplied as both the authentication username and password. The URL for pushing data using the API is as follows:

https://<api-factory location>/object/<dataset-id>/

Data should be formatted as a JSON document and passed as the HTTP request body, using application/json as a Content-Type HTTP request header. A typical JSON document to be added to a dataset might look as follows:

{   
  "sensorId": "Sensor123",   
  "sensorStatus": "on", 
  "sensorType": "humidity",
  "humidity": 68
}

Note that your datapoint may be given a unique ID of your choosing, by specifying the attribute _id within your document body. If the _id field is omitted, a unique value will be assigned to your datapoint automatically.

Examples

cURL

curl -X POST \ 
  https://api.mksmart.org/data/object/baff61f3-47f7-4f8b-81db-9588c7e44762 \ 
  -H 'Content-Type: application/json' \   
  -d '{"sensorId": "Sensor123", "sensorStatus": "on", "sensorType": "humidity", "humidity": 68}' \
  -u 3486133f-fbb2-428d-8e83-d7aa39860cf5

PHP (cURL)

	
$curl = curl_init();
$username = "3486133f-fbb2-428d-8e83-d7aa39860cf5";
$password = "";
 
curl_setopt_array($curl, array(
 CURLOPT_URL =&gt; "https://api.mksmart.org/data/object/baff61f3-47f7-4f8b-81db-9588c7e44762",
 CURLOPT_USERPWD =&gt; ($username . ":" . $password),
 CURLOPT_RETURNTRANSFER =&gt; true,
 CURLOPT_ENCODING =&gt; "",
 CURLOPT_MAXREDIRS =&gt; 10,
 CURLOPT_TIMEOUT =&gt; 30,
 CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST =&gt; "POST",
 CURLOPT_POSTFIELDS =&gt; "{\"sensorId\": \"Sensor123\", \"sensorStatus\": \"on\",\"sensorType\": \"humidity\",\"humidity\": 68}",
 CURLOPT_HTTPHEADER =&gt; array(
 "Content-Type: application/json"
 ),
));
 
$response = curl_exec($curl);
$err = curl_error($curl);
 
curl_close($curl);
 
if ($err) {
 echo "cURL Error #:" . $err;
} else {
 echo $response;
}

Python (requests)

import requests
 
url = "https://api.mksmart.org/data/object/baff61f3-47f7-4f8b-81db-9588c7e44762"
 
auth = ("3486133f-fbb2-428d-8e83-d7aa39860cf5","")
payload = "{\"sensorId\": \"Sensor123\", \"sensorStatus\": \"on\", \"sensorType\": \"humidity\",\"humidity\": 68,}"
headers = {
 'Content-Type': "application/json"
 }
 
response = requests.post(url, data=payload, headers=headers, auth=auth)
 
print(response.text)

Additional annotations to your data

Note that, as data gets added to your dataset, the API will automatically annotate your JSON documents with some additional values. These help add context to your data and can be useful when constructing queries to return specific ranges of data. These additional fields can be distinguished from your own fields as they are preceded with an underscore:
_datasetid: The UUID of the dataset the JSON document was added to
_timestamp: The UNIX timestamp indicating when the data point was added to the dataset
_timestamp_year: The year component of the timestamp
_timestamp_month: The month component of the timestamp
_timestamp_day: The day (of the month) component of the timestamp
_timestamp_hour: The hour component of the timestamp
_timestamp_minute: The minute component of the timestamp
_timestamp_second: The second component of the timestamp
Be aware that the timestamp values added to your data reflect the time at which the JSON document was added to your dataset via the API, not the time at which a sensor value, for example, was read from a sensor. This should be considered when batch processing large amounts of data and writing to your dataset in bulk.

A data point that has been annotated with additional fields may look as follows:

{
  "sensorId": "Sensor123",   
  "sensorStatus": "on", 
  "sensorType": "humidity",
  "humidity": 68,
  "_datasetid": "707d796b-7311-4c74-9255-02bced129463",
  "_timestamp": 1556110378,
  "_timestamp_year": "2019",
  "_timestamp_month": "04",
  "_timestamp_day": "24",
  "_timestamp_hour": "13",
  "_timestamp_minute": "52",
  "_timestamp_second": "58"
}

Update

Update docs...

Delete

Delete docs...

API Factory

Installation

Data Stream API

Management API

Add-ons

Examples

Clone this wiki locally