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

Create a Rule Triggering Condition

Function

This API is used by an application to create a rule triggering condition on the IoT platform.

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}/routing-rule/rules

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. This parameter is required only when the API is called from the management plane in the physical multi-tenant scenario. You can log in to the IoTDA console and choose Overview in the navigation pane to view the instance ID.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

rule_name

No

String

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

Minimum: 1

Maximum: 256

description

No

String

Parameter description: custom rule description.

Minimum: 0

Maximum: 256

subject

Yes

RoutingRuleSubject object

Parameter description: resource event details.

app_type

No

String

Parameter description: application scope of the tenant rule. The default value is GLOBAL. Options:

  • GLOBAL: The rule takes effect for all resources under the tenant.

  • APP: The rule takes effect for resources in a resource space. If this parameter is set to APP, the rule to create takes effect in the resource space specified by app_id carried in the request. If app_id is not carried, the rule to create takes effect in the default resource space.

app_id

No

String

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

select

No

String

Parameter description: custom SQL select statement. The value can contain up to 2,500 characters. This parameter is used only by users of the Standard and Enterprise editions. For details about the available parameters, see the request parameters of data forwarding APIs in the help document, for example, notify_data.body.

Minimum: 0

Maximum: 2500

where

No

String

Parameter description: user-defined SQL where statement. The maximum length is 2,500 characters. This parameter is used only by users of the standard and enterprise editions. For details about the available parameters, see the request parameters of data forwarding APIs in the help document, for example, notify_data.body.

Minimum: 0

Maximum: 2500

Table 4 RoutingRuleSubject

Parameter

Mandatory

Type

Description

resource

Yes

String

Parameter description: resource name. Options:

  • device: device.

  • device.property: device property.

  • device.message: device message.

  • device.message.status: device message status.

  • device.status: device status.

  • batchtask: batch task.

  • product: product.

  • device.command.status: asynchronous command status of the device.

Minimum: 1

Maximum: 50

event

Yes

String

Parameter description: resource event. Value: The value range varies by resource type. event must be associated with resource. The mapping between event and resource is as follows:

  • device maps to create (device addition).

  • device maps to delete (device deletion).

  • device maps to update (device data change).

  • device.status maps to update (device status change).

  • device.property maps to report (device property reporting).

  • device.message maps to report (device message reporting).

  • device.message.status maps to update (device message status change).

  • batchtask maps to update (batch task status change).

  • product maps to create (product addition).

  • product maps to delete (product deletion).

  • product maps to update (product update).

  • device.command.status maps to update (update of the device asynchronous command status).

Minimum: 1

Maximum: 50

Response Parameters

Status code: 201

Table 5 Response body parameters

Parameter

Type

Description

rule_id

String

Unique ID of a rule triggering condition. The value is allocated by the platform during rule triggering condition creation.

rule_name

String

Custom rule name.

Minimum: 1

Maximum: 256

description

String

Custom rule description.

Minimum: 1

Maximum: 256

subject

RoutingRuleSubject object

Resource change event.

app_type

String

Application scope of the tenant rule. Options:

  • GLOBAL: The rule takes effect for all resources under the tenant.

  • APP: The rule takes effect for resources in a resource space.

app_id

String

Resource space ID.

select

String

Custom SQL select statement. The value can contain up to 2500 characters. This parameter is used only by users of the Standard and Enterprise editions.

Minimum: 0

Maximum: 2500

where

String

Custom SQL where statement. The value can contain up to 2500 characters. This parameter is used only by users of the Standard and Enterprise editions.

Minimum: 0

Maximum: 2500

active

Boolean

Whether the rule triggering condition is activated.

Table 6 RoutingRuleSubject

Parameter

Type

Description

resource

String

Parameter description: resource name. Options:

  • device: device.

  • device.property: device property.

  • device.message: device message.

  • device.message.status: device message status.

  • device.status: device status.

  • batchtask: batch task.

  • product: product.

  • device.command.status: asynchronous command status of the device.

Minimum: 1

Maximum: 50

event

String

Parameter description: resource event. Value: The value range varies by resource type. event must be associated with resource. The mapping between event and resource is as follows:

  • device maps to create (device addition).

  • device maps to delete (device deletion).

  • device maps to update (device data change).

  • device.status maps to update (device status change).

  • device.property maps to report (device property reporting).

  • device.message maps to report (device message reporting).

  • device.message.status maps to update (device message status change).

  • batchtask maps to update (batch task status change).

  • product maps to create (product addition).

  • product maps to delete (product deletion).

  • product maps to update (product update).

  • device.command.status maps to update (update of the device asynchronous command status).

Minimum: 1

Maximum: 50

Example Requests

  • Creates a trigger condition (device creation notification) for the rule.

    POST https://{endpoint}/v5/iot/{project_id}/routing-rule/rules
    
    {
      "rule_name" : "rulename",
      "subject" : {
        "resource" : "device",
        "event" : "create"
      },
      "app_type" : "GLOBAL",
      "description" : "description"
    }
  • Creates a trigger condition (property reporting) for the rule.

    POST https://{endpoint}/v5/iot/{project_id}/routing-rule/rules
    
    {
      "rule_name" : "rulename",
      "subject" : {
        "resource" : "device.property",
        "event" : "report"
      },
      "app_type" : "GLOBAL",
      "description" : "description"
    }
  • Creates a trigger condition (message reporting) for the rule. (Topics are filtered based on SQL statements. The Basic edition does not support the SQL filtering capability.)

    POST https://{endpoint}/v5/iot/{project_id}/routing-rule/rules
    
    {
      "rule_name" : "rulename",
      "subject" : {
        "resource" : "device.message",
        "event" : "report"
      },
      "app_type" : "GLOBAL",
      "description" : "description",
      "select" : "notify_data.header as header,notify_data.body as body,'12345678901234abcd' as id",
      "where" : "notify_data.body.topic = '$oc/devices/646c7579a5adc915f8966e8b_8514932826827763/user/testmsg'"
    }

Example Responses

Status code: 201

Created

{
  "rule_id" : "5bcaddda-75bf-4623-8c8d-26175c41fcca",
  "rule_name" : "rulename",
  "description" : "description",
  "subject" : {
    "resource" : "device",
    "event" : "create"
  },
  "app_type" : "GLOBAL",
  "app_id" : "1a7ffc5cd89c44dd8265b1653d951ce0",
  "select" : "*",
  "where" : "product_id='d89c-44dd-8265-b1653d951ce0'",
  "active" : false
}

Status Codes

Status Code

Description

201

Created

400

Bad Request

401

Unauthorized

403

Forbidden

500

Internal Server Error

Error Codes

See Error Codes.