You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/iot-central/core/tutorial-use-rest-api.md
+68-94Lines changed: 68 additions & 94 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Tutorial - Use the REST API to manage an application
3
3
description: In this tutorial you use the REST API to create and manage an IoT Central application, add a device, and configure data export.
4
4
author: dominicbetts
5
5
ms.author: dobett
6
-
ms.date: 03/04/2024
6
+
ms.date: 08/20/2024
7
7
ms.topic: tutorial
8
8
ms.service: iot-central
9
9
ms.custom: devx-track-azurecli
@@ -41,38 +41,21 @@ You use the Azure CLI to make the REST API calls and to generate the bearer toke
41
41
42
42
## Authorize the REST API
43
43
44
-
Before you can use the REST API, you must configure the authorization. The REST API calls in this tutorial use one of three authorization types:
44
+
Before you can use the REST API, you must configure the authorization. The REST API calls in this tutorial use one of two authorization types:
45
45
46
-
- A bearer token that authorizes access to `https://management.azure.com`. You use this bearer token when you create and delete and IoT Central application. An IoT Central application is an Azure resource.
47
46
- A bearer token that authorizes access to `https://apps.azureiotcentral.com`. You use this bearer token to create the API tokens in the IoT Central application.
48
47
- Administrator and operator API tokens that authorize access to capabilities in your IoT Central application. You use these tokens for most the API calls in this tutorial. These tokens only authorize access to one specific IoT Central application.
49
48
50
-
Generate the bearer tokens and get the subscription ID you need to use the REST API:
49
+
Run the following Azure CLI commands to generate a bearer token that authorizes access to `https://apps.azureiotcentral.com`:
51
50
52
-
-**bearerToken**: Run the following Azure CLI commands to generate a bearer token that authorizes access to `https://management.azure.com`:
53
-
54
-
```azurecli
55
-
az login
56
-
az account get-access-token --resource https://management.azure.com
57
-
```
58
-
59
-
> [!TIP]
60
-
> You may need to run `az login` even if you're using the Cloud Shell.
61
-
62
-
Make a note of this management bearer token, you use it later in the tutorial.
63
-
64
-
- **bearerTokenApp**: Run the following Azure CLI commands to generate a bearer token that authorizes access to `https://apps.azureiotcentral.com`:
65
-
66
-
```azurecli
67
-
az account get-access-token --resource https://apps.azureiotcentral.com
68
-
```
69
-
70
-
> [!TIP]
71
-
> If you started a new instance of your shell, run `az login` again.
51
+
```azurecli
52
+
az account get-access-token --resource https://apps.azureiotcentral.com
53
+
```
72
54
73
-
Make a note of this application bearer token, you use it later in the tutorial.
55
+
> [!TIP]
56
+
> If you started a new instance of your shell, run `az login` again.
74
57
75
-
- **subscriptionId**: Your subscription ID was included in the output from the two previous commands. Make a note of the subscription ID, you use it later in the tutorial.
58
+
Make a note of the `accessToken` value, you use it later in the tutorial.
76
59
77
60
> [!NOTE]
78
61
> Bearer tokens expire after an hour. If they expire, run the same commands to generate new bearer tokens.
@@ -91,6 +74,7 @@ Use the following command to generate an IoT Central application with a random n
91
74
92
75
```azurecli
93
76
appName=app-rest-$(date +%s)
77
+
94
78
az iot central app create --name $appName --resource-group iot-central-rest-tutorial --subdomain $appName
95
79
```
96
80
@@ -100,17 +84,12 @@ Make a note of the application name, you use it later in this tutorial.
100
84
101
85
Use the following data plane requests to create the application API tokens in your IoT Central application. Some of the requests in this tutorial require an API token with administrator permissions, but the majority can use operator permissions:
102
86
103
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Create an operator token** request.
104
-
1. Select **Send**.
105
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Create an admin token** request.
106
-
1. Select **Send**.
107
-
108
-
If you want to see these tokens in your IoT central application, open the application and navigate to **Security > Permissions > API tokens**.
109
-
110
87
To create an operator token called `operator-token` by using the Azure CLI, run the following command. The role GUID is the ID of the operator role in all IoT Central applications:
az rest --method put --uri https://$appName.azureiotcentral.com/api/apiTokens/admin-token?api-version=2022-07-31 --headers Authorization="Bearer $bearerTokenApp" "Content-Type=application/json" --body '{"roles": [{"role": "ca310b8d-2f4a-44e0-a36e-957c202cd8d4"}]}'
124
105
```
125
106
126
107
Make a note of the admin token the command returns, you use it later in the tutorial. The token looks like `SharedAccessSignature sr=2...`.
127
108
109
+
If you want to see these tokens in your IoT central application, open the application and navigate to **Security > Permissions > API tokens**.
110
+
128
111
## Register a device
129
112
130
113
You must register a device with IoT Central before it can connect. Use the following requests to register your device in your application and retrieve the device credentials. The first request creates a device with **phone-001** as the device ID:
131
114
132
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Add a device** request.
133
-
1. Select **Send**. In the response, notice that the device isn't provisioned.
134
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Get device credentials** request.
135
-
1. Select **Send**.
136
-
1. The **Visualize** tab shows the **ID Scope** and **Primary key** values that the device needs to able to connect.
az rest --method put --uri https://$appName.azureiotcentral.com/api/devices/phone-001?api-version=2022-07-31 --headers Authorization="$operatorToken" "Content-Type=application/json" --body '{"displayName": "My phone app","simulated": false,"enabled": true}'
120
+
121
+
az rest --method get --uri https://$appName.azureiotcentral.com/api/devices/phone-001/credentials?api-version=2022-07-31 --headers Authorization="$operatorToken" "Content-Type=application/json"
122
+
```
123
+
124
+
Make a note of the `idScope` and `primaryKey` values the command returns, you use them later in the tutorial.
137
125
138
126
## Provision and connect a device
139
127
@@ -158,78 +146,64 @@ To connect the **IoT Plug and Play** app to your Iot Central application:
158
146
159
147
To verify the device is now provisioned, you can use the REST API:
160
148
161
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Get a device** request.
162
-
1. Select **Send**. In the response, notice that the device is now provisioned. IoT Central also assigned a device template to the device based on the model ID sent by the device.
az rest --method get --uri https://$appName.azureiotcentral.com/api/devices/phone-001?api-version=2022-07-31 --headers Authorization="$operatorToken" "Content-Type=application/json"
154
+
```
155
+
156
+
Make a note of the `template` value the command returns, you use it later in the tutorial.
163
157
164
158
You can use the REST API to manage device templates in the application. For example, to view the device templates in the application:
165
159
166
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **List device templates** request.
az rest --method get --uri https://$appName.azureiotcentral.com/api/deviceTemplates?api-version=2022-07-31 --headers Authorization="$operatorToken" "Content-Type=application/json"
165
+
```
168
166
169
167
## Query and control the device
170
168
171
169
You can use the REST API to query telemetry from your devices. The following request returns the accelerometer data from all devices that share a specific device template ID:
172
170
173
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Run a query** request.
deviceTemplateId=<the device template Id you made a note of previously>
175
+
q1='{"query": "SELECT $id as ID, $ts as timestamp, sensors.accelerometer FROM '
176
+
q2=' WHERE WITHIN_WINDOW(P1D) AND sensors.accelerometer <> NULL"}'
177
+
query="$q1 $deviceTemplateId $q2"
178
+
echo $query
179
+
180
+
az rest --method post --uri https://$appName.azureiotcentral.com/api/query?api-version=2022-10-31-preview --headers Authorization="$operatorToken" "Content-Type=application/json" --body "$query"
181
+
```
175
182
176
183
You can use the REST API to read and set device properties. The following request returns all the property values from the **Device Info** component that the device implements:
177
184
178
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Get properties from a component** request.
179
-
1. Select **Send**.
180
-
181
-
You can use the REST API to call device commands. The following request calls a command that switches on your smartphone light on twice for three seconds. For the command to run, your smartphone screen must be on with the **IoT Plug and Play** app visible:
182
-
183
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Call command** request.
184
-
1. Select **Send**.
185
-
186
-
## Export telemetry
187
-
188
-
You can use the REST API to configure and manage your IoT Central application. The following steps show you how to configure data export to send telemetry values to a webhook. To simplify the setup, this article uses a **RequestBin** webhook as the destination. **RequestBin** is a non-Microsoft service.
189
-
190
-
To create your test endpoint for the data export destination:
191
-
192
-
1. Navigate to [RequestBin](https://requestbin.com/).
193
-
1. Select **Create a RequestBin**.
194
-
1. Sign in with one of the available methods.
195
-
1. Copy the URL of your RequestBin endpoint.
196
-
1. In Postman, open the **IoT Central REST tutorial** collection and navigate to the collection variables.
197
-
1. Paste the URL of your RequestBin endpoint into the **Current value** column for **webHookURL** in the collection variables.
198
-
1. Save the changes.
199
-
200
-
To configure the export destination in your IoT Central application by using the REST API:
201
-
202
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Create a webhook export destination** request.
203
-
1. Select **Send**.
204
-
205
-
To configure the export definition in your IoT Central application by using the REST API:
206
-
207
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Create a telemetry export definition** request.
208
-
1. Select **Send**. Notice that the status is **Not started**.
It might take a couple of minutes for the export to start. To check the status of the export by using the REST API:
189
+
az rest --method get --uri https://$appName.azureiotcentral.com/api/devices/phone-001/components/device_info/properties?api-version=2022-07-31 --headers Authorization="$operatorToken" "Content-Type=application/json"
190
+
```
211
191
212
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Get an export by ID** request.
213
-
1. Select **Send**. When the status is **healthy**, IoT Central is sending telemetry to your webhook.
192
+
You can use the REST API to call device commands. The following request calls a command that switches on your smartphone light on twice for three seconds. For the command to run, your smartphone screen must be on with the **IoT Plug and Play** app visible:
214
193
215
-
The app on your smartphone doesn't send telemetry unless the screen is on and the **IoT Plug and Play** app is visible.
When your smartphone app is sending telemetry, navigate to your RequestBin to view the exported telemetry.
198
+
az rest --method post --uri https://$appName.azureiotcentral.com/api/devices/phone-001/commands/lightOn?api-version=2022-07-31 --headers Authorization="$operatorToken" "Content-Type=application/json" --body '{"duration": 3, "delay": 1, "pulses": 2}'
199
+
```
218
200
219
201
## Clean up resources
220
202
221
-
If you've finished with the IoT Central application you used in this tutorial, you can use the REST API to delete it:
203
+
If you've finished with the IoT Central application you used in this tutorial, you can delete it:
222
204
223
-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Delete an IoT central application** request.
224
-
1. Select **Send**.
225
-
226
-
> [!TIP]
227
-
> This request uses a bearer token that you generated at the start of the tutorial. Bearer tokens expire after hour. You may need to generate a new bearer token that authorizes access to `https://apps.azureiotcentral.com`.
228
-
229
-
## Next steps
230
-
231
-
<!-- TODO: Fix this -->
232
-
If you'd prefer to continue through the set of IoT Central tutorials and learn more about building an IoT Central solution, see:
205
+
```azurecli
206
+
appName=<the app name generated previously>
233
207
234
-
> [!div class="nextstepaction"]
235
-
> [Create a gateway device template](./tutorial-define-gateway-device-type.md)
208
+
az iot central app delete --name $appName --resource-group iot-central-rest-tutorial
0 commit comments