All Products
Search
Document Center

IoT Platform:Use IoT Platform SDK for Python

Last Updated:Jun 10, 2026

IoT Platform provides a cloud SDK for Python. Learn how to install, configure, and use the Python SDK to call cloud APIs.

Install the SDK

  1. Install Python.

    Download Python from the official Python website. The SDK supports Python 2.7.x and 3.x.

  2. Install pip. Skip this step if pip is already installed.

    Download pip from the official pip website.

  3. Install the IoT Python SDK with administrator permissions.

    For SDK usage instructions, visit aliyun-python-sdk-iot.

    sudo pip install aliyun-python-sdk-core
    sudo pip install aliyun-python-sdk-iot
  4. Import the IoT Python SDK modules.

    from aliyunsdkcore import client
    from aliyunsdkiot.request.v20180120 import RegisterDeviceRequest
    from aliyunsdkiot.request.v20180120 import PubRequest
    ...

Initialize the SDK

The following code shows an example of how to initialize the SDK to call an API in the China (Shanghai) region.

Initialize the SDK client with client.AcsClient(accessKeyId, accessKeySecret, 'cn-shanghai'):

import os
accessKeyId = os.getenv('ACCESS_KEY_ID')
accessKeySecret = os.getenv('ACCESS_KEY_SECRET')
clt = client.AcsClient(accessKeyId, accessKeySecret, 'cn-shanghai')

Parameter

Description

clt

Initializes the SDK client. cn-shanghai is the region code for your IoT Platform service.

Check your service region in the upper-left corner of the IoT Platform console.

Region codes: Supported regions.

Call an API

The SDK encapsulates each API request as a ${API name}Request class. Set request parameters with set_${request parameter name}, then call do_action_with_exception(request) on the SDK client clt to get the response.

For a list of IoT Platform cloud APIs, see API list. For descriptions of the request parameters in `request` and the response parameters in `response`, see the corresponding API reference.

This topic uses the Pub operation as an example to show how to publish a message to a topic. For information about the request parameters, see Pub.

request = PubRequest.PubRequest()
request.set_accept_format('json')  # Set the data format of the response. The default format is XML. This example sets the format to JSON.
request.set_IotInstanceId('iotInstanceId') 
request.set_ProductKey('productKey')
request.set_TopicFullName('/productKey/deviceName/user/get')  # The full name of the topic to which the message is sent.
request.set_MessageContent('aGVsbG8gd29ybGQ=')  # hello world Base64 string
request.set_Qos(0)
result = clt.do_action_with_exception(request)
print('result : ' + result)

Appendix: Sample code

You can go to the IoT Platform Cloud SDK Example Center to view or download sample code for API calls. The sample code provides examples for the Java, Python, PHP, .NET, and Go SDKs.

The Alibaba Cloud OpenAPI Developer Portal provides an online API debugging tool. On the API debugging page, you can quickly search for and test API calls. The system automatically generates sample SDK code for different languages based on the parameters that you enter. The sample code is displayed on the SDK Example tab on the right side of the page. On the Call Result tab, you can view the request URL and the response in JSON format.