Updated on 2024-12-02 GMT+08:00

Create a Device

Function

This API is used by an application to create a device on the IoT platform. Only the created device can connect to the IoT platform.

  • You can specify the gateway_id parameter to create a device under a parent device. Currently, up to two levels of child devices are supported.

  • This API supports initial device configuration. The API reads the product details provided in the product_id parameter in the request. If the default value of a product property is defined, the default value is written to the device shadow.

  • You can also use the shadow parameter to specify the initial configuration for the device. After the initial configuration is specified, the system compares the property values specified by service_id and the desired section with the default values of the corresponding properties in the product. If they are different, the property values specified by the shadow parameter are written to the device shadow.

  • This API can be used to create a single device. For details about how to register devices in batches, see Creating a Batch Task.

Debugging

You can debug this API through automatic authentication in API Explorer or use the SDK sample code generated by API Explorer.

URI

POST /v5/iot/{project_id}/devices

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Parameter description: project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

No

String

Parameter description: user token. You can obtain the token by calling the IAM API Obtaining a User Token Through Password Authentication. X-Subject-Token in the response header returned by the API is the desired user token. For details about how to obtain the token, see Token Authentication.

Instance-Id

No

String

Parameter description: instance ID. Unique identifier of each instance in the physical multi-tenant scenario. Mandatory for professional editions and recommended in other cases. Log in to the IoTDA console and choose Overview in the navigation pane to view the instance ID. For details, see Viewing Instance Details.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

device_id

No

String

** Parameter description**: device ID, which is globally unique and uniquely identifies a device. If this parameter is carried, the platform will use the parameter value as the device ID. Otherwise, the platform will allocate a device ID, which is in the format of product_idnode_id. Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (), and hyphens (-) are allowed. You are advised to use at least 4 characters.

node_id

Yes

String

Parameter description: device identifier. This parameter is set to the IMEI, MAC address, or serial number.

The device node ID contains 1 to 64 characters, including letters, digits, hyphens (-), and underscores (). Note: Information cannot be modified once it is hardcoded to NB-IoT modules. Therefore, the node ID of an NB-IoT device must be globally unique. Value: The value can contain a maximum of 64 characters. Only letters, digits, underscores (), and hyphens (-) are allowed. You are advised to use at least 4 characters.

device_name

No

String

** Parameter description**: device name, which uniquely identifies a device in a resource space.

Value: The value can contain a maximum of 256 characters. Only letters, digits, and special characters (_?'#().,&%@!-) are allowed. You are advised to use at least 4 characters.

product_id

Yes

String

Parameter description: unique ID of the product associated with the device. The value is allocated by the platform after the product is created. For details, see Creating a Product.

Value: The value can contain a maximum of 36 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

auth_info

No

AuthInfo object

Parameter description: access authentication information about the device.

description

No

String

Parameter description: device description.

Value: The value can contain a maximum of 2048 characters. Only letters, digits, and special characters (_?'#().,&%@!-) are allowed.

gateway_id

No

String

Parameter description: gateway ID, which is the device ID of the parent device. If this parameter is carried, a child device is created under the parent device, and the child device is not directly connected to the platform. In this case, the parent device must already exist on the platform. After the child device is created, the gateway ID of the child device is the value of this parameter. If this parameter is not carried, a device that is directly connected to the platform is created. After the device is created, device_id and gateway_id of the device are the same. Note: The platform supports up to two levels of child devices.

Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

app_id

No

String

Parameter description: resource space ID. This parameter is optional. If you have multiple resource spaces, you can use this parameter to specify the resource space to which the device to create will belong. If this parameter is not specified, the device to create will belong to the default resource space.

Value: The value can contain a maximum of 36 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

extension_info

No

Object

Parameter description: extended information, which can be customized. If this parameter is specified for a child device during device creation, the platform will notify the gateway of the extended information using the MQTT API for notifying a gateway of new child device connection. The maximum size of the value is 1 KB.

shadow

No

Array of InitialDesired objects

Parameter description: initial configuration of the device. You can use this parameter to specify the initial configuration for the device. The system compares the property values specified by service_id and the desired section with the default values of the corresponding properties in the product. If they are different, the property values specified by the shadow parameter are written to the device shadow. The value of service_id and the properties in the desired section must be defined in the product model.

Table 4 AuthInfo

Parameter

Mandatory

Type

Description

auth_type

No

String

Parameter description: authentication type. If auth_type is not specified, the secret authentication is used.

Options:

  • SECRET: The secret authentication is used.

  • CERTIFICATES: The certificate authentication is used.

secret

No

String

Parameter description: device secret. This parameter is mandatory when auth_type is set to SECRET. Note: Due to protocol restrictions, NB-IoT device secrets must be hexadecimal values. This parameter is not returned when you query a device list.

Value: The value can contain 8 to 32 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

fingerprint

No

String

Parameter description: certificate fingerprint. This parameter is mandatory when auth_type is set to CERTIFICATES. If this parameter is not specified during device registration, the certificate fingerprint used during the first device access is used.

Value: The value is a string of 40 or 64 hexadecimal characters.

secure_access

No

Boolean

Parameter description: whether the device is connected to the platform using a secure protocol.

Options:

  • true: The device is connected to the platform using a secure protocol.

  • false: The device is connected to the platform using an insecure protocol. Devices connected to the platform using insecure protocols may be spoofed. You are advised not to use this value.

timeout

No

Integer

Parameter description: validity period for device access, in seconds. The default value is 0.

If the device has not been connected to the IoT platform and activated within the validity period, the IoT platform deletes the registration information of the device. If this parameter is set to 0 (recommended), the platform does not delete the device registration information.

Note: This parameter takes effect only for directly connected devices.

Table 5 InitialDesired

Parameter

Mandatory

Type

Description

service_id

Yes

String

Parameter description: device service ID, which is defined in the product model associated with the device.

Value: The value can contain a maximum of 32 characters. Only letters, digits, and special characters (_?'#().,&%@!-) are allowed.

desired

Yes

Object

Parameter description: initial device properties in JSON format. The data is in key-value pairs. Each key is a parameter name of a property (property_name) in the product model. Currently, only one-layer structure is supported, as shown in the example request. The property value is compared with the default value of the corresponding property of the product. If they are different, the property value set in this parameter is written to the device shadow. To delete the entire desired section, enter an empty object (for example, "desired":{}). To delete the desired value of a property, set the property value to null (for example, {"temperature":null}).

Response Parameters

Status code: 201

Table 6 Response body parameters

Parameter

Type

Description

app_id

String

Resource space ID.

app_name

String

Resource space name.

device_id

String

Device ID, used to uniquely identify a device. The value of this parameter is specified during device registration or allocated by the platform. If the value is allocated by the platform, the value is in the format of [product_id]_[node_id].

node_id

String

Device identifier. This parameter is set to the IMEI, MAC address, or serial number.

gateway_id

String

Gateway ID, which is the device ID of the parent device. The gateway ID is the same as the device ID if the device is a directly connected device. If the device is an indirectly connected device, the gateway ID is the device ID of the directly connected device with which it associates.

device_name

String

Device name.

node_type

String

Device node type.

  • ENDPOINT: an indirectly connected device.

  • GATEWAY: a directly connected device or gateway.

  • UNKNOWN: The device node type is unknown.

description

String

Device description.

fw_version

String

Firmware version of the device.

sw_version

String

Software version of the device.

device_sdk_version

String

Device SDK information.

auth_info

AuthInfoRes object

Access authentication information about the device.

product_id

String

Unique ID of the product associated with the device.

product_name

String

Name of the product associated with the device.

status

String

Device status.

  • ONLINE: The device is online.

  • OFFLINE: The device is offline.

  • ABNORMAL: The device is abnormal.

  • INACTIVE: The device is not activated.

  • FROZEN: The device is frozen.

create_time

String

Time when the device was registered on the platform. The value is in the format of yyyyMMdd'T'HHmmss'Z', for example, 20151212T121212Z.

connection_status_update_time

String

Time of the last device connection status change. Such statuses include ONLINE, OFFLINE, and ABNORMAL. The value is in the format of yyyy-MM-dd'T'HH:mm:ss.SSS'Z', for example, 2015-12-12T12:12:122Z.

active_time

String

Time when a device was activated. The value is in the format of yyyy-MM-dd'T'HH:mm:ss.SSS'Z', for example, 2015-12-12T12:12:122Z.

tags

Array of TagV5DTO objects

List of device tags.

extension_info

Object

Extended device information, which can be customized. If this parameter is specified for a child device during device creation, the platform will notify the gateway of the extended information using the MQTT API for notifying a gateway of new child device connection.

Table 7 AuthInfoRes

Parameter

Type

Description

auth_type

String

Parameter description: authentication type. If auth_type is not specified, the secret authentication is used.

Options:

  • SECRET: The secret authentication is used.

  • CERTIFICATES: The certificate authentication is used.

secret

String

Parameter description: device secret. This parameter is mandatory when auth_type is set to SECRET. Note: Due to protocol restrictions, NB-IoT device secrets must be hexadecimal values. This parameter is not returned when you query a device list.

Value: The value can contain 8 to 32 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

secondary_secret

String

** Parameter description**: secondary device secret used when the master secret fails to pass the authentication. This parameter is valid when auth_type is set to SECRET. Unavailable for devices accessed using CoAP. Note: Due to protocol restrictions, NB-IoT device secrets must be hexadecimal values. This parameter is not returned when you query a device list.

Value: The value can contain 8 to 32 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

fingerprint

String

Parameter description: certificate fingerprint. This parameter is valid when auth_type is set to CERTIFICATES. If this parameter is not specified during device registration, the certificate fingerprint used during the first device access is used.

Value: The value is a string of 40 or 64 hexadecimal characters.

secondary_fingerprint

String

** Parameter description**: secondary certificate fingerprint used when the master fingerprint fails to pass the authentication. This parameter is valid when auth_type is set to CERTIFICATES.

Value: The value is a string of 40 or 64 hexadecimal characters.

secure_access

Boolean

Parameter description: whether the device is connected to the platform using a secure protocol.

Options:

  • true: The device is connected to the platform using a secure protocol.

  • false: The device is connected to the platform using an insecure protocol. Devices connected to the platform using insecure protocols may be spoofed. You are advised not to use this value.

timeout

Integer

Parameter description: validity period for device access, in seconds. The default value is 0.

If the device has not been connected to the IoT platform and activated within the validity period, the IoT platform deletes the registration information of the device. If this parameter is set to 0 (recommended), the platform does not delete the device registration information.

Note: This parameter is only available for directly connected devices.

Table 8 TagV5DTO

Parameter

Type

Description

tag_key

String

Parameter description: tag key, which is unique for a resource. If the specified key already exists, the value of the existing tag is overwritten. If the specified key does not exist, a new tag is added.

Value: The value can contain a maximum of 64 characters. Only letters, digits, underscores (_), periods (.), and hyphens (-) are allowed.

tag_value

String

Parameter description: tag value.

Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), periods (.), and hyphens (-) are allowed.

Example Requests

  • Creates a device and sets the authentication type to secret authentication.

    POST https://{endpoint}/v5/iot/{project_id}/devices
    
    {
      "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
      "node_id" : "ABC123456789",
      "device_name" : "dianadevice",
      "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
      "auth_info" : {
        "auth_type" : "SECRET",
        "secret" : "3b935a250c50dc2c6d481d048cefdc3c",
        "secure_access" : true
      },
      "description" : "watermeter device",
      "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
      "extension_info" : {
        "aaa" : "xxx",
        "bbb" : 0
      },
      "shadow" : [ {
        "service_id" : "WaterMeter",
        "desired" : {
          "temperature" : "60"
        }
      } ]
    }
  • Creates a device and set the authentication type to certificate authentication.

    POST https://{endpoint}/v5/iot/{project_id}/devices
    
    {
      "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
      "node_id" : "ABC123456789",
      "device_name" : "dianadevice",
      "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
      "auth_info" : {
        "auth_type" : "CERTIFICATES",
        "fingerprint" : "dc0f1016f495157344ac5f1296335cff725ef22f",
        "secure_access" : true
      },
      "description" : "watermeter device",
      "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
      "extension_info" : {
        "aaa" : "xxx",
        "bbb" : 0
      },
      "shadow" : [ {
        "service_id" : "WaterMeter",
        "desired" : {
          "temperature" : "60"
        }
      } ]
    }
  • Creates a non-directly connected device.

    POST https://{endpoint}/v5/iot/{project_id}/devices
    
    {
      "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
      "node_id" : "ABC123456789",
      "device_name" : "dianadevice",
      "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
      "description" : "watermeter device",
      "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
      "gateway_id" : "5a332c1a-d14f-489c-a8e7-4753b1bb0872",
      "extension_info" : {
        "aaa" : "xxx",
        "bbb" : 0
      },
      "shadow" : [ {
        "service_id" : "WaterMeter",
        "desired" : {
          "temperature" : "60"
        }
      } ]
    }

Example Responses

Status code: 201

Created

{
  "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
  "app_name" : "testAPP01",
  "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
  "node_id" : "ABC123456789",
  "gateway_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
  "device_name" : "dianadevice",
  "node_type" : "ENDPOINT",
  "description" : "watermeter device",
  "fw_version" : "1.1.0",
  "sw_version" : "1.1.0",
  "auth_info" : {
    "auth_type" : "SECRET",
    "secret" : "3b93****fdc3c",
    "fingerprint" : "dc0f****f22f",
    "secure_access" : true,
    "timeout" : 0
  },
  "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
  "product_name" : "Thermometer",
  "status" : "INACTIVE",
  "create_time" : "20190303T081011Z",
  "connection_status_update_time" : null,
  "active_time" : null,
  "tags" : [ {
    "tag_key" : "testTagName",
    "tag_value" : "testTagValue"
  } ],
  "extension_info" : {
    "aaa" : "xxx",
    "bbb" : 0
  }
}

Status Codes

Status Code

Description

201

Created

400

Bad Request

401

Unauthorized

403

Forbidden

404

Not Found

500

Internal Server Error

Error Codes

See Error Codes.