All Products
Document Center

IoT Platform:Use IoT Platform SDK for Go

Last Updated:Aug 07, 2023

IoT Platform provides developers with an SDK for Go. This article describes how to install and configure IoT Platform SDK for Go. This article also describes how to use the SDK to call the API operations of IoT Platform.

Install the SDK

  1. Install Go.

    Go 1.6 and later are supported. To obtain a Go installation package, visit the official Go website.

  2. After Go is installed, create a system variable named GOPATH and set the value to your code directory.

    To obtain more information about the GOPATH variable, run the go help gopath command.

  3. Run the following command to install Alibaba Cloud SDK for Go:

    go get -u

    For more information, see Alibaba Cloud SDK for Go.

  4. Run the following command to import the files that are related to IoT Platform SDK for Go to a Go file:

    import ""

Initialize the SDK

package main

import ""

func main() {

    client, err := sdk.NewClientWithAccessKey("<your regionId>", "<your accessKey>", 
                                              "<your accessSecret>")
    if err != nil {
        // Handle exceptions




The ID of the region where IoT Platform resides. For example, the ID of the China (Shanghai) region is cn-shanghai. For more information, see Supported regions.


The AccessKey ID of your Alibaba Cloud account.

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


The AccessKey secret that is used in pair with the AccessKey ID.

Initiate a request

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.

request := iot.CreatePubRequest()
request.AcceptFormat = "json"
request.IotInstanceId = "<your iotInstanceId>"
request.ProductKey = "<your productKey>"
request.TopicFullName = fmt.Sprintf("/%s/%s/user/get", "<your productKey>", "<your deviceName>")
request.MessageContent = base64.StdEncoding.EncodeToString([]byte("hello world"))
request.Qos = "0"
response, err := client.Pub(request)
if err != nil {
fmt.Printf("response is %#v\n", response)

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.