Skip to content

Commit e1b6089

Browse files
committed
Remove Postman references
1 parent b1b6a0f commit e1b6089

File tree

1 file changed

+68
-94
lines changed

1 file changed

+68
-94
lines changed

articles/iot-central/core/tutorial-use-rest-api.md

Lines changed: 68 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Tutorial - Use the REST API to manage an application
33
description: In this tutorial you use the REST API to create and manage an IoT Central application, add a device, and configure data export.
44
author: dominicbetts
55
ms.author: dobett
6-
ms.date: 03/04/2024
6+
ms.date: 08/20/2024
77
ms.topic: tutorial
88
ms.service: iot-central
99
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
4141

4242
## Authorize the REST API
4343

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:
4545

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.
4746
- 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.
4847
- 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.
4948

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`:
5150

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+
```
7254

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.
7457
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.
7659

7760
> [!NOTE]
7861
> 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
9174

9275
```azurecli
9376
appName=app-rest-$(date +%s)
77+
9478
az iot central app create --name $appName --resource-group iot-central-rest-tutorial --subdomain $appName
9579
```
9680

@@ -100,17 +84,12 @@ Make a note of the application name, you use it later in this tutorial.
10084

10185
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:
10286

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-
11087
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:
11188

11289
```azurecli
113-
$appName=<the app name generated previously>
90+
appName=<the app name generated previously>
91+
bearerTokenApp=<the bearer token generated previously>
92+
11493
az rest --method put --uri https://$appName.azureiotcentral.com/api/apiTokens/operator-token?api-version=2022-07-31 --headers Authorization="Bearer $bearerTokenApp" "Content-Type=application/json" --body '{"roles": [{"role": "ae2c9854-393b-4f97-8c42-479d70ce626e"}]}'
11594
```
11695

@@ -120,20 +99,29 @@ To create an admin token called `admin-token` by using the Azure CLI, run the fo
12099

121100
```azurecli
122101
$appName=<the app name generated previously>
102+
$bearerTokenApp=<the bearer token generated previously>
103+
123104
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"}]}'
124105
```
125106

126107
Make a note of the admin token the command returns, you use it later in the tutorial. The token looks like `SharedAccessSignature sr=2...`.
127108

109+
If you want to see these tokens in your IoT central application, open the application and navigate to **Security > Permissions > API tokens**.
110+
128111
## Register a device
129112

130113
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:
131114

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.
115+
```azurecli
116+
appName=<the app name generated previously>
117+
operatorToken=<the operator token generated previously>
118+
119+
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.
137125

138126
## Provision and connect a device
139127

@@ -158,78 +146,64 @@ To connect the **IoT Plug and Play** app to your Iot Central application:
158146

159147
To verify the device is now provisioned, you can use the REST API:
160148

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.
149+
```azurecli
150+
appName=<the app name generated previously>
151+
operatorToken=<the operator token generated previously>
152+
153+
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.
163157

164158
You can use the REST API to manage device templates in the application. For example, to view the device templates in the application:
165159

166-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **List device templates** request.
167-
1. Select **Send**.
160+
```azurecli
161+
appName=<the app name generated previously>
162+
operatorToken=<the operator token generated previously>
163+
164+
az rest --method get --uri https://$appName.azureiotcentral.com/api/deviceTemplates?api-version=2022-07-31 --headers Authorization="$operatorToken" "Content-Type=application/json"
165+
```
168166

169167
## Query and control the device
170168

171169
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:
172170

173-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Run a query** request.
174-
1. Select **Send**.
171+
```azurecli
172+
appName=<the app name generated previously>
173+
operatorToken=<the operator token generated previously>
174+
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+
```
175182

176183
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:
177184

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**.
185+
```azurecli
186+
appName=<the app name generated previously>
187+
operatorToken=<the operator token generated previously>
209188
210-
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+
```
211191

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:
214193

215-
The app on your smartphone doesn't send telemetry unless the screen is on and the **IoT Plug and Play** app is visible.
194+
```azurecli
195+
appName=<the app name generated previously>
196+
operatorToken=<the operator token generated previously>
216197
217-
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+
```
218200

219201
## Clean up resources
220202

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:
222204

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>
233207
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
209+
```

0 commit comments

Comments
 (0)