Help Center/ IoT Device Access/ API Reference/ API Reference on the Application Side/ Examples/ Example 1: Creating Devices in Batches Using a Template
Updated on 2023-02-16 GMT+08:00

Example 1: Creating Devices in Batches Using a Template

Scenarios

This topic describes how to create devices in batches using APIs. For details about how to call APIs, see Calling APIs.

The platform allows you to create devices in batches using request parameters or a template. This topic describes how to create devices in batches using a template.

Involved APIs

Procedure

  1. Determine the product to which devices to create belong.

    1. Query the product list
      • API

        URL: GET /v5/iot/{project_id}/products

        For details, see Query the Product List.

      • Example request
        GET https://{Endpoint}/v5/iot/{project_id}/products?limit={limit}&marker={marker}&app_id={app_id}&offset={offset}
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
      • Example response

        Status Code: 200 OK

        Content-Type: application/json
        
        {
          "products" : [ {
            "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
            "app_name" : "testAPP01",
            "product_id" : "5ba24f5ebbe8f56f5a14f605",
            "name" : "Thermometer",
            "device_type" : "Thermometer",
            "protocol_type" : "CoAP",
            "data_format" : "binary",
            "manufacturer_name" : "ABC",
            "industry" : "smartCity",
            "description" : "this is a thermometer produced by Huawei",
            "create_time" : "20190303T081011Z"
          } ],
          "page" : {
            "count" : 10,
            "marker" : "5c90fa7d3c4e4405e8525079"
          }
        }
    2. Select a product based on project requirements and record the value of product_id.

  2. Download the batch task template.

    Click here to download the template.

  3. Set parameters of devices to create in the template.

    Edit the template downloaded in 2 by referring to Create a Device.

    Example:

  4. Update a batch task file.

    1. Update a batch task file.
      • API

        URL: POST /v5/iot/{project_id}/batchtask-files

        For details, see Upload a Batch Task File.

      • Example request
        POST https://{Endpoint}/v5/iot/{project_id}/batchtask-files 
        Content-Type: multipart/form-data 
        X-Auth-Token: ******** 
        Instance-Id: ********
      • Example response

        Status Code: 201 Created

        Content-Type: application/json
        
        {
          "file_id" : "0c3c77dd-42a2-4309-9e10-da2e8bf64ac3",
          "file_name" : "BatchCreateDevices_test01.xlsx",
          "upload_time" : "20200617T081608Z"
        }
    2. Record the value of file_id.

  5. Create a batch task.

    1. Create a batch task.
      • API

        URL: POST /v5/iot/{project_id}/batchtasks

        For details, see Create a Batch Task.

      • Example request
        POST https://{Endpoint}/v5/iot/{project_id}/batchtasks
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
        
        {
          "app_id" : "Ev8FVvCfOdQDzrFrxSOemiw_aMca",
          "task_name" : "BatchCommandTask",
          "task_type" : "softwareUpgrade",
          "targets" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ],
          "targets_filter" : {
            "group_ids" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ]
          },
          "document" : {
            "package_id" : "32822e5744a45ede319d2c50"
          },
          "task_policy" : {
            "schedule_time" : "20151212T121212Z",
            "retry_count" : 5,
            "retry_interval" : 60
          },
          "document_source" : "jeQDJQZltU8iKgFFoW060F5SGZka"
        }
      • Example response

        Status Code: 201 Created

        Content-Type: application/json
        
        {
          "task_id" : "5c8ba99030344005c02316ad",
          "task_name" : "testname",
          "task_type" : "softwareUpgrade",
          "targets" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ],
          "targets_filter" : {
            "group_ids" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ]
          },
          "document" : {
            "package_id" : "32822e5744a45ede319d2c50"
          },
          "task_policy" : {
            "schedule_time" : "20151212T121212Z",
            "retry_count" : 5,
            "retry_interval" : 60
          },
          "status" : "Success",
          "status_desc" : "string",
          "task_progress" : {
            "total" : 0,
            "processing" : 0,
            "success" : 0,
            "fail" : 0,
            "waitting" : 0,
            "fail_wait_retry" : 0,
            "stopped" : 0
          },
          "create_time" : "20151212T121212Z"
        }
    2. Record the value of task_id.

  6. Query the batch task.

    1. Query the batch task.
      • API

        URL: GET /v5/iot/{project_id}/batchtasks/{task_id}

        For details, see Query a Batch Task.

      • Example request
        GET https://{Endpoint}/v5/iot/{project_id}/batchtasks/{task_id}?limit={limit}&marker={marker}&offset={offset}
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
      • Example response

        Status Code: 200 OK

        Content-Type: application/json
        
        {
          "batchtasks" : {
            "task_id" : "5c8ba99030344005c02316ad",
            "task_name" : "testname",
            "task_type" : "softwareUpgrade",
            "targets" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ],
            "targets_filter" : {
              "group_ids" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ]
            },
            "document" : {
              "package_id" : "32822e5744a45ede319d2c50"
            },
            "task_policy" : {
              "schedule_time" : "20151212T121212Z",
              "retry_count" : 5,
              "retry_interval" : 60
            },
            "status" : "Success",
            "status_desc" : "string",
            "task_progress" : {
              "total" : 0,
              "processing" : 0,
              "success" : 0,
              "fail" : 0,
              "waitting" : 0,
              "fail_wait_retry" : 0,
              "stopped" : 0
            },
            "create_time" : "20151212T121212Z"
          },
          "task_details" : [ {
            "target" : "e495cf17-ff79-4294-8f64-4d367919d665",
            "status" : "Success",
            "output" : "success",
            "error" : {
              "error_code" : "IOTDA.000002",
              "error_msg" : "The request is unauthorized."
            }
          } ],
          "page" : {
            "count" : 10,
            "marker" : "5c90fa7d3c4e4405e8525079"
          }
        }
    2. Check the task execution result.

      Check whether the batch device creation task is complete based on the response.