RESTful API¶
It is a channel for devices to call platform services via a RESTful API that uses HTTP Protocol. It is suitable for use as a channel for integrating existing systems or new ones that are about to be developed, regardless of the programming language they are developed from (test the API at (ทดสอบการทำงานของ API ได้ที่ https://beta-api.nexiiot.io)). The APIs currently available are divided into 2 groups as follows:
Device API¶
It is an API related to Device. The domain name of the API is https://api.nexiiot.io/device. The details are as follows:
1. Requesting the Device Platform Connection Status (Device Status)
EndPoint |
|
Method |
GET |
Request Header |
Authorization : Device ClientID:Token |
Return |
|
example
GET /device/status HTTP/1.1
Host: api.nexiiot.io
Authorization: Device 777d5c96-4c83-4aa6-a273-5ee7c5f453b1:abcduKh8r2tP1zVc1W1nG8YWZeu21234
2. Publishing messages to various topics can be done in 2 ways
Type 1 is specifying the Topic in URL Path format.
EndPoint |
https://api.nexiiot.io/device/message/{any}/{topic} |
Method |
PUT |
Request Header |
Authorization : Device ClientID:Token |
Request Body |
Content-type : text/plain
| The text you want to publish to the Topic.
|
Return |
|
example
PUT /device/message/mythings/bedroom/light HTTP/1.1
Host: api.nexiiot.io
Authorization: Device 777d5c96-4c83-4aa6-a273-5ee7c5f453b1:abcduKh8r2tP1zVc1W1nG8YWZeu21234
ON
Type 2 is specifying a Topic through a Parameter (Query String).
EndPoint |
https://api.nexiiot.io/device/message |
Method |
PUT |
Request Header |
Authorization : Device ClientID:Token |
Parameter |
|
Return |
|
example
PUT /device/message?topic=mything/bedroom/light HTTP/1.1
Host: api.nexiiot.io
Authorization: Device 777d5c96-4c83-4aa6-a273-5ee7c5f453b1:abcduKh8r2tP1zVc1W1nG8YWZeu21234
OFF
3. Publishing a private message to a specific device. There are 2 ways to use the device.
Type 1 is specifying the Topic in URL Path format.
EndPoint |
https://api.nexiiot.io/device/private/{any}/{topic} |
Method |
PUT |
Request Header |
Authorization : Device ClientID of the device to which you want to send the message:Token of the device to which you want to send the message |
Request Body |
|
Return |
|
example
PUT /device/private/topic/for/me HTTP/1.1
Host: api.nexiiot.io
Authorization: Device 777d5c96-4c83-4aa6-a273-5ee7c5f453b1:abcduKh8r2tP1zVc1W1nG8YWZeu21234
Hello Device
Type 2 is specifying a Topic through a Parameter (Query String).
EndPoint |
https://api.nexiiot.io/device/private |
Method |
PUT |
Request Header |
Authorization : Device ClientID of the device to which you want to send the message:Token of the device to which you want to send the message |
Parameter |
|
Return |
|
ตัวอย่าง
PUT /device/private?topic=topic/for/me HTTP/1.1
Host: api.nexiiot.io
Authorization: Device 777d5c96-4c83-4aa6-a273-5ee7c5f453b1:abcduKh8r2tP1zVc1W1nG8YWZeu21234
Hello Device
Caution
Sending a message via the REST API with a Topic is slightly different from sending via the MQTT Protocol. If sending via the REST API, the Topic setting does not need to include "@msg" in front, but the system will automatically fill it in. If sending via the MQTT Protocol, you must include "@msg" in front of the Topic to be sent.
Sending a Private Message The device that is being sent the message must subscribe to the topic with the prefix @private in front of the topic that you want to subscribe to, such as @private/topic/for/me or using @private/# will allow you to receive a Private Message in every topic.
4. Reading Device Shadow Data
EndPoint |
https://api.nexiiot.io/device/shadow/data |
Method |
GET |
Request Header |
Authorization : Device ClientID:Token |
Return |
|
example
GET /device/shadow/data?alias=sensor HTTP/1.1
Host: api.nexiiot.io
Authorization: Device 777d5c96-4c83-4aa6-a273-5ee7c5f453b1:abcduKh8r2tP1zVc1W1nG8YWZeu21234
5. Writing data to Shadow Data using merge method
EndPoint |
https://api.nexiiot.io/device/shadow/data |
Method |
PUT |
Request Header |
Authorization : Device ClientID:Token |
Request Body |
The data to be written to Shadow Data is in JSON format as follows:
|
Return |
|
example
PUT /device/shadow/data?alias=test HTTP/1.1
Host: api.nexiiot.io
Authorization: Device 777d5c96-4c83-4aa6-a273-5ee7c5f453b1:abcduKh8r2tP1zVc1W1nG8YWZeu21234
{
"data": {
"temperature": 33.7,
"config": {"item1": "a", "item2": "b"},
"note": "test case"
}
}
6. Writing data to Shadow Data using overwrite method
EndPoint |
https://api.nexiiot.io/device/shadow/data |
Method |
POST |
Request Header |
Authorization : Device ClientID:Token |
Request Body |
The data to be written to Shadow Data is in JSON format as follows:
|
Return |
|
example
POST /device/shadow/data?alias=test HTTP/1.1
Host: api.nexiiot.io
Authorization: Device 777d5c96-4c83-4aa6-a273-5ee7c5f453b1:abcduKh8r2tP1zVc1W1nG8YWZeu21234
{
"data": {
"temperature": 33.7,
"config": {"item1": "a", "item2": "b"},
"note": "test case"
}
}
Shadow Batch Update¶
It is used in cases where the IoT Device cannot send data to the Cloud Platform at the specified time, such as due to internet connection problems, etc. This causes the IoT Device to store data in its own memory first, such as on an SD Card, etc. When the Cloud Platform can be connected, all stored data is sent to the Cloud Platform again. Values can be sent to the Platform at multiple points at the same time.
There are 3 ways to write batch shadows:
REST API is a data writing in batches, which is done through a REST API, which can be written in either a merge or overwrite format. The details are as follows:
EndPoint |
https://api.nexiiot.io/device/shadow/batch |
Method |
PUT (Merge) or POST (Overwrite) |
Request Header |
Authorization : Device ClientID:Token |
Request Body |
The data set to be written to Shadow is in JSON format as follows:
|
Return |
|
example
POST /device/shadow/batch HTTP/1.1
Host: api.nexiiot.io
Authorization: Device 777d5c96-4c83-4aa6-a273-5ee7c5f453b1:abcduKh8r2tP1zVc1W1nG8YWZeu21234
{
"batch" : [
{"data":{"temp":25.9, "humid":9.6}, "ts":-90000},
{"data":{"temp":25.3, "humid":9.8}, "ts":-60000},
{"data":{"temp":24.5, "humid":9.1}, "ts":-30000},
{"data":{"temp":26.8, "humid":8.2}, "ts":0 }
]
}
Note
The time of each data set is in milliseconds. You can use ts or timestamp as the field name. If the value is less than 1000 * 2^23 = 8388608000, it is considered a relative time with the current time. If the value is greater, it is considered an absolute time. You can use negative values to represent the past time, which is suitable for updating past data points. For example, if ts or timestamp is set to -90000 and the current timestamp is 1619075885, the time of the data point will be 1619075885 - 90000 = 1618985885 (90 seconds back from the present).
The merged field specified in the payload when writing to the Shadow determines the data writing method—whether to merge or overwrite.
If
merged : true
is set, the data will be merged.If
merged : false
is set, the data will be overwritten.If this field is not specified in the payload, the default value is true, meaning the data will be merged.
The operation of the Expression defined in the Schema and Trigger when writing to the Shadow in batch mode.
The Expression will still be calculated according to the formula defined for each data set, similar to a For Loop writing to the Shadow. However, when writing to the Shadow in batch mode, the Shadow read/write quota will only be deducted as 1 operation. The Shadow Expression quota will still be deducted according to the number of data sets. For example, if 100 data points are sent for storage and there is 1 field with an Expression set, the number of Shadow Expressions deducted will be 1 x 100 = 100 operations.
For Triggers, they will only operate on the data set with the latest value (the highest timestamp). The quota will be deducted as if writing only a single data set.
MQTT is a batch writing method that uses Topic and Payload. See Shadow Batch Update
CoAP API is a batch write operation that is performed via CoAP Protocol, which can be either merged or overwritten. See Shadow Batch Update
Caution
The limitation of writing to the Shadow in batch mode is that the number of data sets sent for writing at a time must not exceed 100 data sets (the batch field in the JSON array). For example, the payload sent for writing data is:
{ "ackid" : 1839, "batch" : [ {"data":{"temp":25.9, "humid":9.6}, "ts":-90000}, {"data":{"temp":25.3, "humid":9.8}, "ts":-60000}, {"data":{"temp":24.5, "humid":9.1}, "ts":-30000}, {"data":{"temp":26.8, "humid":8.2}, "ts":0}], "merged": true }
This means there are 4 data sets, for example. If the number of data sets sent exceeds the specified limit, none of the data will be saved, and a warning message will be returned in JSON format as follows:
{"ackid":140,"errcode":429,"message":"batch size exceeds 100","inputsize": 102}
This means that the Batch write with ackid 140 sent more than 100 data sets, specifically 102 data sets.
Data Store API¶
It is an API related to retrieving data stored in Timeseries Data. The API's domain name is https://ds.nexiiot.io/feed. The database used for storage is KairosDB. The query format of the various parameters sent is the same as KairosDB. The details are as follows:
EndPoint |
https://ds.nexiiot.io/feed/api/v1/datapoints/query |
Method |
POST |
Request Header |
Authorization : Bearer UserToken or Device ClientID:DeviceToken, Content-Type : |
Request Body |
Conditions used in queries are in JSON format and can be divided into 2 types:
|
Return |
|