Updated on 2024-06-27 GMT+08:00

Deliver a Message to a Device

Function

This API is used by an application to deliver a message to a device. After an application delivers a message to the platform, the platform returns a response to the application and then sends the message to the device. The response returned by the platform may not be the device receiving result. Call the API Pushing a Device Message Status Change Notification for the platform to push the device receiving result to the application. Notes:

  • This API is only used to deliver messages to MQTT devices.

  • This API supports message delivery to a single device. For details about how to deliver messages to multiple devices, 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/{device_id}/messages

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.

device_id

Yes

String

Parameter description: ID of the device to which the message is delivered. The ID is unique and is allocated by the platform during device registration. Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

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. In the returned response header, X-Subject-Token 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

message_id

No

String

Parameter description: message ID, which is user-defined (UUID is recommended). The ID must be unique under a device. Otherwise, an error is returned. Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

Maximum: 128

name

No

String

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

Maximum: 128

message

Yes

Object

Parameter description: message content. String and JSON formats are supported.

properties

No

PropertiesDTO object

Parameter description: property parameter of the message delivered to the device.

encoding

No

String

Parameter description: encoding format for the message content. The default value is none. Options:

  • none

  • base64

Default: none

payload_format

No

String

Parameter description: valid payload format. This parameter is valid when the encoding format for the message content is none. The default value is standard, indicating that the message content is encapsulated in the standard format. Options:

  • standard

  • raw: The message content is directly delivered as the payload.

Default: standard

topic

No

String

Parameter description: (optional) suffix of the custom topic through which the message is delivered to the device. This parameter is applicable only to MQTT devices. You can only enter topics configured on the tenant product page. Otherwise, the verification will fail. The prefix that the platform adds to a message topic is $oc/devices/{device_id}/user/. You can add a part to the prefix. For example, if you add messageDown to the prefix, the topic is $oc/devices/{device_id}/user/messageDown. Replace device_id with the actual gateway ID of the device. If you specify the topic, messages are delivered to the device through this topic. If you do not specify the topic, messages are delivered to the device through the default topic. The default topic is $oc/devices/{device_id}/sys/messages/down. Either this parameter or the topic_full_name parameter can be specified.

Maximum: 128

topic_full_name

No

String

Parameter description: (optional) name of the topic through which the message is delivered to the device. You can specify a custom topic. The platform does not check whether the topic is defined on the platform and transparently transmits the topic to the device. The device needs to subscribe to the specified topic first. Either this field or the topic parameter can be specified.

Maximum: 128

ttl

No

Integer

Parameter description: aging time of the caches of delivered messages on the platform, in minutes. The default value is 1440. The value of ttl must be a multiple of 5, that is, the granularity is 5 minutes. If this parameter is set to 0, messages are not cached. The maximum cache duration is 1,440 minutes. For longer cache duration, you need to submit a request in advance, or the delivery will fail.

Minimum: 0

Maximum: 10080

Default: 1440

Table 4 PropertiesDTO

Parameter

Mandatory

Type

Description

correlation_data

No

String

Parameter description: data in the request and response modes of MQTT 5.0. This parameter is optional. You can use this parameter to configure data in the request and response modes of MQTT. Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

response_topic

No

String

Parameter description: response topic in the request and response modes of MQTT 5.0. This parameter is optional. You can use this parameter to configure the response topic in the request and response modes of MQTT. Value: The value can contain a maximum of 128 characters. Only letters, digits, and special characters (_-?=$#+/) are allowed.

Maximum: 128

user_properties

No

Array of UserPropDTO objects

Parameter description: custom property. This parameter is optional. You can use this parameter to configure custom properties. A maximum of 20 customized attributes can be configured.

Table 5 UserPropDTO

Parameter

Mandatory

Type

Description

prop_key

No

String

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

prop_value

No

String

Parameter description: value of the custom property. Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and question marks (?) are allowed. '#().,& %@!-.

Response Parameters

Status code: 201

Table 6 Response body parameters

Parameter

Type

Description

message_id

String

Message ID, which is specified by you (UUID is recommended). The message ID must be unique under a device. If the ID is not specified, the platform automatically generates one.

result

MessageResult object

Result of delivering the message. The value is in JSON format.

Table 7 MessageResult

Parameter

Type

Description

status

String

Message status, including PENDING, DELIVERED, FAILED, and TIMEOUT. If the device is offline, the platform caches the message and returns PENDING. The platform delivers the message after the device reports data. If the device is online, the platform directly delivers the message. If the message is successfully delivered, the platform returns DELIVERED. If the message fails to be delivered, the platform returns FAILED. If the message is not delivered to the device within the default time (one day), the platform sets the message status to TIMEOUT. In addition, an application can subscribe to the message execution result. The platform pushes the result to the application.

created_time

String

UTC time when the message was created. The value is in the format of yyyyMMdd'T'HHmmss'Z'.

finished_time

String

UTC time when the message was delivered to the device, failed to be delivered, or timed out. The value is in the format of yyyyMMdd'T'HHmmss'Z'.

Example Requests

  • Creates a message and delivers it through the default topic of the platform.

    POST https://{endpoint}/v5/iot/{project_id}/devices/{device_id}/messages
    
    {
      "message_id" : "99b32da9-cd17-4cdf-a286-f6e849cbc364",
      "name" : "messageName",
      "message" : "HelloWorld"
    }
  • Creates a message and delivers it through a custom topic on the platform.

    POST https://{endpoint}/v5/iot/{project_id}/devices/{device_id}/messages
    
    {
      "message_id" : "99b32da9-cd17-4cdf-a286-f6e849cbc364",
      "name" : "messageName",
      "message" : "HelloWorld",
      "topic" : "testTopic"
    }
  • Creates a message and delivers it through a custom topic.

    POST https://{endpoint}/v5/iot/{project_id}/devices/{device_id}/messages
    
    {
      "message_id" : "99b32da9-cd17-4cdf-a286-f6e849cbc364",
      "name" : "messageName",
      "message" : "HelloWorld",
      "topic_full_name" : "/test/customTopic/testTopic"
    }

Example Responses

Status code: 201

Created

{
  "message_id" : "b1224afb-e9f0-4916-8220-b6bab568e888",
  "result" : {
    "status" : "PENDING",
    "created_time" : "20151212T121212Z",
    "finished_time" : "20151212T121213Z"
  }
}

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.