title | description | author | ms.author | ms.service | services | ms.devlang | ms.topic | ms.date | ms.custom |
---|---|---|---|---|---|---|---|---|---|
Create an Azure IoT hub using the resource provider REST API | Microsoft Docs |
Learn how to use the resource provider C# REST API to create and manage an IoT Hub programmatically. |
kgremban |
kgremban |
iot-hub |
iot-hub |
csharp |
conceptual |
08/08/2017 |
devx-track-csharp |
[!INCLUDE iot-hub-resource-manager-selector]
You can use the IoT Hub resource provider REST API to create and manage Azure IoT hubs programmatically. This tutorial shows you how to use the IoT Hub resource provider REST API to create an IoT hub from a C# program.
[!INCLUDE updated-for-az]
To complete this tutorial, you need the following:
-
Visual Studio.
-
An active Azure account. If you don't have an account, you can create a free account in just a couple of minutes.
-
Azure PowerShell 1.0 or later.
[!INCLUDE iot-hub-prepare-resource-manager]
-
In Visual Studio, create a Visual C# Windows Classic Desktop project using the Console App (.NET Framework) project template. Name the project CreateIoTHubREST.
-
In Solution Explorer, right-click on your project and then click Manage NuGet Packages.
-
In NuGet Package Manager, check Include prerelease, and on the Browse page search for Microsoft.Azure.Management.ResourceManager. Select the package, click Install, in Review Changes click OK, then click I Accept to accept the licenses.
-
In NuGet Package Manager, search for Microsoft.IdentityModel.Clients.ActiveDirectory. Click Install, in Review Changes click OK, then click I Accept to accept the license.
[!IMPORTANT] The Microsoft.IdentityModel.Clients.ActiveDirectory NuGet package and Azure AD Authentication Library (ADAL) have been deprecated. No new features have been added since June 30, 2020. We strongly encourage you to upgrade, see the migration guide for more details.
-
In Program.cs, replace the existing using statements with the following code:
using System; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using Microsoft.Azure.Management.ResourceManager; using Microsoft.Azure.Management.ResourceManager.Models; using Microsoft.IdentityModel.Clients.ActiveDirectory; using Newtonsoft.Json; using Microsoft.Rest; using System.Linq; using System.Threading;
-
In Program.cs, add the following static variables replacing the placeholder values. You made a note of ApplicationId, SubscriptionId, TenantId, and Password earlier in this tutorial. Resource group name is the name of the resource group you use when you create the IoT hub. You can use a pre-existing or a new resource group. IoT Hub name is the name of the IoT Hub you create, such as MyIoTHub. The name of your IoT hub must be globally unique. Deployment name is a name for the deployment, such as Deployment_01.
static string applicationId = "{Your ApplicationId}"; static string subscriptionId = "{Your SubscriptionId}"; static string tenantId = "{Your TenantId}"; static string password = "{Your application Password}"; static string rgName = "{Resource group name}"; static string iotHubName = "{IoT Hub name including your initials}";
[!INCLUDE iot-hub-pii-note-naming-hub]
[!INCLUDE iot-hub-get-access-token]
Use the IoT Hub resource provider REST API to create an IoT hub in your resource group. You can also use the resource provider REST API to make changes to an existing IoT hub.
-
Add the following method to Program.cs:
static void CreateIoTHub(string token) { }
-
Add the following code to the CreateIoTHub method. This code creates an HttpClient object with the authentication token in the headers:
HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
-
Add the following code to the CreateIoTHub method. This code describes the IoT hub to create and generates a JSON representation. For the current list of locations that support IoT Hub see Azure Status:
var description = new { name = iotHubName, location = "East US", sku = new { name = "S1", tier = "Standard", capacity = 1 } }; var json = JsonConvert.SerializeObject(description, Formatting.Indented);
-
Add the following code to the CreateIoTHub method. This code submits the REST request to Azure. The code then checks the response and retrieves the URL you can use to monitor the state of the deployment task:
var content = new StringContent(JsonConvert.SerializeObject(description), Encoding.UTF8, "application/json"); var requestUri = string.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/providers/Microsoft.devices/IotHubs/{2}?api-version=2021-04-12", subscriptionId, rgName, iotHubName); var result = client.PutAsync(requestUri, content).Result; if (!result.IsSuccessStatusCode) { Console.WriteLine("Failed {0}", result.Content.ReadAsStringAsync().Result); return; } var asyncStatusUri = result.Headers.GetValues("Azure-AsyncOperation").First();
-
Add the following code to the end of the CreateIoTHub method. This code uses the asyncStatusUri address retrieved in the previous step to wait for the deployment to complete:
string body; do { Thread.Sleep(10000); HttpResponseMessage deploymentstatus = client.GetAsync(asyncStatusUri).Result; body = deploymentstatus.Content.ReadAsStringAsync().Result; } while (body == "{\"status\":\"Running\"}");
-
Add the following code to the end of the CreateIoTHub method. This code retrieves the keys of the IoT hub you created and prints them to the console:
var listKeysUri = string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Devices/IotHubs/{2}/IoTHubKeys/listkeys?api-version=2021-04-12", subscriptionId, rgName, iotHubName); var keysresults = client.PostAsync(listKeysUri, null).Result; Console.WriteLine("Keys: {0}", keysresults.Content.ReadAsStringAsync().Result);
You can now complete the application by calling the CreateIoTHub method before you build and run it.
-
Add the following code to the end of the Main method:
CreateIoTHub(token.AccessToken); Console.ReadLine();
-
Click Build and then Build Solution. Correct any errors.
-
Click Debug and then Start Debugging to run the application. It may take several minutes for the deployment to run.
-
To verify that your application added the new IoT hub, visit the Azure portal and view your list of resources. Alternatively, use the Get-AzResource PowerShell cmdlet.
Note
This example application adds an S1 Standard IoT Hub for which you are billed. When you are finished, you can delete the IoT hub through the Azure portal or by using the Remove-AzResource PowerShell cmdlet when you are finished.
Now you have deployed an IoT hub using the resource provider REST API, you may want to explore further:
-
Read about the capabilities of the IoT Hub resource provider REST API.
-
Read Azure Resource Manager overview to learn more about the capabilities of Azure Resource Manager.
To learn more about developing for IoT Hub, see the following articles:
To further explore the capabilities of IoT Hub, see: