Develop a Data Service API
In the IoT Platform Data Service, data development tasks in Insights use standard SQL statements to analyze device data and store the analysis results in a custom storage table. You can encapsulate these storage tables as APIs, which developers can easily call. These APIs can respond to device requests or support server-side data integration. This topic describes how to develop a custom Data Service API.
Create a custom storage table
Create a custom storage table to store the analysis results of SQL data development tasks. In this example, you will store the daily average temperature and humidity of a server room.
-
On the Instance Overview page of the IoT Platform Console, click the card of your target Enterprise Edition instance.
-
In the left-side navigation pane, choose Data Service > Data Storage.
-
On the Data Storage page, click the Offline Storage tab.
-
On the Offline Storage tab, click the Custom Storage Table tab to add a custom storage table.
-
On the Custom Storage Table tab, click Create Custom Storage Table.
-
In the Create Custom Storage Table dialog box, configure the following parameters. Use the default values for other parameters.
For details about the parameters, see Create a custom storage table.
Parameter
Description
Display Name
The display name of the storage table. In this example, enter raspberrypi4.
Table Identifier
The identifier for the storage table. In this example, enter raspberrypi4.
Data Retention Period
The length of time to retain data in the storage table. Select 1 Month.
The system automatically deletes data older than the specified retention period based on the time field in the table.
-
Click Confirm.
-
-
On the Custom Storage Table tab, find the table you created and click View in the Actions column.
-
Click the Table Schema tab to configure the table fields.
Add the following fields to store the daily average temperature and humidity of the server room. For detailed instructions, see Manage custom storage tables.

Create a data development task
Create a data development task that uses an SQL statement to query temperature and humidity data for the server room and calculate the daily average values.
-
On the Instance Overview page of the IoT Platform Console, click the card of your target Enterprise Edition instance.
-
In the left-side navigation pane, choose Data Service > Insights.
-
On the Insights page, click New SQL Analysis.
-
In the New SQL Analysis dialog box, enter a Data Development Name (for example, Test) and a Description.
-
Click OK.
-
On the Insights page, find the SQL analysis task that you created and click Development Workbench in the Actions column.
-
-
Write an SQL statement to query data for a Product.
NoteYou can locate your target Product (for example, Raspberry Pi) under the Platform Device Table Device Data Table menu and copy the table name.
In the SQL editor, enter a query statement, such as
SELECT * FROM '${pk.ProductKey}'. After you run the query, the Results tab below displays device data, including fields such astemperatureandhumidity. -
Similarly, create an SQL analysis task (for example, Test) to query the average temperature and humidity for a specific day.
The following is an example SQL statement:
SELECT AVG(temperature) AS avg_temperature, AVG(humidity) AS avg_humidity FROM '${pk.ProductKey}' WHERE $event_date = 'Target_Date'. After you run the query, the Results tab displays the calculated average temperature and humidity values. -
Click Publish and Run to store the results in the custom storage table. Then, click Validate Settings.

-
After validation succeeds, click Publish Task.
After you publish the task, you can no longer edit the SQL statement in the SQL Data Service Workbench.

Generate an API
The following steps show how to use the Test data development task to generate an API that retrieves daily average temperature and humidity data.
-
In the left-side navigation pane of the IoT Platform Console, choose .
-
On the Data API tab, click the Custom Service API tab, and then click New API.
-
In the New API wizard, configure the API parameters.
-
Configure the basic API information, and then click Next.

-
Configure the request parameters and response parameters.

-
In the Test API section, enter the request parameters and click Start Test.
A success message indicates that the API service is running as expected.
-
-
After you configure and test the API, click Publish to generate the data API.
After you publish the API, you can access and call it directly.
For more details about configuring and using custom APIs, see Custom Service API.


