All Products
Document Center

IoT Platform:.Use IoT Platform SDK for .NET

Last Updated:Mar 01, 2023

IoT Platform providesdevelopers with an SDK for .NET. This article describeshow to install and configure IoT Platform SDK for .NET.This article also describes how to use the SDK to call the API operations of IoT Platform.

Install the SDK

  1. Install a.NET development environment.

    IoT Platform SDK for .NET supports the following development environments:

    • ..NET Framework 4.5 and later

    • ..NET Standard 2.0 and later

    • C# 4.0 and later

    • Visual Studio 2010 and later

  2. Install IoT Platform SDK for .NET by using the NuGet package manager.

    In this example, Visual Studio is used.

    1. In the Solution Explorer pane of Visual Studio, right-click your project and select Manage NuGet Packages.

    2. In the NuGet Package Manager pane, click the Browse tab.

    3. On the Browse tab, enter aliyun-net-sdk in the search box and select the aliyun-net-sdk-iot package that is provided by Alibaba Cloud.

    4. Click Install.

Initialize the SDK

The following example shows how to initialize the SDK when IoT Platform is activated in the China (Shanghai) region:

First, create the clientProfile object to store SDK initialization information. Then, create the client instance from DefaultAcsClient. Use the DefaultAcsClient(clientProfile) method to load the SDK initialization information.

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
IClientProfile clientProfile = DefaultProfile.GetProfile("cn-shanghai", "<your-access-key-id>", "<your-access-key-secret>");
DefaultAcsClient client = new DefaultAcsClient(clientProfile);

The clientProfile object is used to store the SDK initialization information.

  • cn-shanghai: the region ID of your IoT Platform service.

    You can view the region in the upper-left corner of the IoT Platform console.

    For more information about region IDs, see Supported regions.

  • <your-access-key-id> and <your-access-key-secret>: the AccessKey ID and AccessKey secret of your Alibaba Cloud account.

    You can go to the Alibaba Cloud Management Console to create or view your AccessKey ID.

Initiate a request

The SDK encapsulates two classes for each API operation. The class names are in the ${Operation name}+"Request" and ${Operation name}+"Response" formats.

  • ${Operation name}+"Request": This class is used to call an API operation. You can use the request instance of this class and call the request.${Request parameters} method to specify the request parameters.

  • ${Operation name}+"Response": You can use the GetAcsResponse(request) method of the client instance that is created from DefaultAcsClient to obtain a response. You can use the response instance of this class and call the response.${Response parameters} method to obtain the response parameters.

    For example, the response.Success() method is called to obtain the Success parameter. This parameter is a common response parameter, which indicates whether the call is successful. Common response parameters also include RequestId, ErrorMessage, and Code.

For more information about the API operations of IoT Platform, see List of operations by function. For more information about the request and response parameters of each API operation, see the API documentation.

The following example shows how to call the Pub operation to publish a message to a topic. For more information about request parameters, see Pub.


In the following sample code, ${iotInstanceId} specifies the ID of an instance. You can view the ID of the instance on the Overview page in the IoT Platform console.

  • If your instance has an ID, you must specify the ID for this parameter. Otherwise, the call fails.

  • If no Overview page or ID is generated for your instance, you do not need to configure this parameter. You must delete the request code that is related to the IotInstanceId parameter or specify an empty string ("") for the parameter. Otherwise, the call fails.

For more information about IoT Platform instances, see Overview. For information about how to purchase an instance, see Purchase Enterprise Edition instances. For more information, see FAQ about IoT Platform instances.

PubRequest request = new PubRequest();
request.IotInstanceId = "<iotInstanceId>"; 
request.ProductKey = "<productKey>";
request.TopicFullName = "/<productKey>/<deviceName>/get";
byte[] payload = Encoding.Default.GetBytes("Hello World.");
String payloadStr = Convert.ToBase64String(payload);
request.MessageContent = payloadStr;
request.Qos = 0;
   PubResponse response = client.GetAcsResponse(request);
   Console.WriteLine("publish message result: " + response.Success);
catch (ServerException e)
catch (ClientException e)

Appendix: Sample code

You can view or download the sample code of API operations in IoT Platform SDK Sample Center. Sample code of SDKs for Java, Python, PHP, .NET, and Go is provided.

Alibaba Cloud OpenAPI Explorer provides online debugging tools for API operations. On the API Debugging page, you can search for API operations, call API operations, and generate sample code for API operations of different SDKs. On the right side of the page, you can view the sample code of an SDK on the Sample Code tab. On the Debugging Result tab, you can view the actual request URL and response in the JSON format.