Quick Start

Last Updated on2020-06-02 09:36:17

This guide is designed to help developers quickly understand how to create Apps and control devices through Tuya Cloud Platform to play smart. Let's start the application journey!

Prerequisites

  • Download and install and register "Tuya Smart" APP

  • Log in to Tuya Smart Development Platform https://iot.tuya.com, unregistered users sign up for a tuya account, and pass the enterprise authentication to become a platform developer. Tuya Smart Global Smart Platform

Operating procedures

Tuya Smart Global Smart Platform

Apply for cloud API authorization

  • Create SaaS

    Enter [SaaS Development] and click Create SaaS.

    saas_zh

  • Acquire Application Key

    Click to enter the SaaS you create, you can acquire the application key on the right of [basic info]

    secret_en

Follow-up device control depends on Access ID and Access Secret, please keep it safe!

Scan code authorization

You can use the "Graffiti Smart" APP scan code to connect to your project.

"Cloud Development" -> "Associated Device" -> "Associated Device under Tuya Smart App Account" image.png

image.png

Use Postman to simulate OpenApi to control the device

The App created through the Tuya cloud platform has a built-in Tuya SDK, which can control the device interactively with Tuya cloud platform. Next, we call OpenApi through Postman to simulate the control device.

  • Environmental preparation

    • Meet Postman

      Postman is a powerful Chrome plugin for webpage debugging and sending HTTP requests for webpages.

      Portal: https://www.jianshu.com/p/0c247b351f08         

    • Set up Postman environment

      To be good at work, we must first sharpen our tools. Before we begin, we must prepare the postman and Tuya cloud-related environments. - Download postman: https://www.getpostman.com > Postman version is preferably 7.12.0 and above - Import Tuya Cloud API Interface Package:         Tuya Cloud API Interface          - Import Tuya Cloud API Environment Package:         Tuya Cloud API Environment - Set environment variables - url: Tuya cloud service, taking China as an example: https://openapi.tuyacn.com              - client_id: equivalent to accessId in the developer platform

      - secret: equivalent to accessKey in developer platform
      
      - device_id: device ID, you can check the corresponding information of the device in the App Device ID)
      
      - The schema is the channel identifier of the App application. Please obtain it based on the package name. For example, your App package name is: com.aa.bb, then your schema is: aabb.
      

        Set environment variable          - OpenApi settings        - Request method

        - Supported request methods are as follows:

             - GET - PUT - POST - DELETE              > Please note: When the request method is POST, Content-Type needs to useapplication / json         

    - Request header settings
    
    Any interface needs to add the following parameters to the header:
    
    |   Parameter  | Type | Parameter position | Description | Required |
    | ------------ | ------ | ------ | ---------------------------- | ---- |
    |  client_id  | String | header | client_id | Yes |
    |     sign | String | header | Signature calculated using the specified signature algorithm: token related interface, business related interface | Yes |
    | sign_method | String | header | Signature Digest Algorithm, HMAC-SHA256 | Yes |
    | t | Long | header | 13-bit standard timestamp | Yes |
    | lang | String | header | language, default en | No |

        
- Signature method

Tuya Cloud currently provides two sets of signature algorithms according to different application scenarios:

- Token related interface (v1.0 / token & v1.0 / token / {refresh_token}): sign = HMAC-SHA256 (client_id + t, secret) .toUpperCase ()
        
- Business interface (non-token interface): sign = HMAC-SHA256 (client_id + access_token + t, secret) .toUpperCase ()
  • Postman control equipment in action

    • Add products and network in the App

    Take Wi-Fi socket as an example, refer to the distribution network

    • Get token

      Apply for a token to Tuya Cloud first, and then use this token to request other APIs, otherwise there will be permission problems.         Simple mode token

      grant_type = 1 means use simple mode grant

    • Query the instruction set supported by the device

      Obtain instructions supported by the device, and subsequent control devices can use this result.         Simple mode token         

    • controlling device

      According to the instructions supported by the device, different effects can be achieved by issuing control devices.         Simple mode token

FAQ

Q: How long is the token validity period?

Tuya: The token is valid for two hours and needs to be reapplied after it expires.

Q: What are clientId and secret and how do I get them?

Tuya: The clientId is the AccessId in the developer platform, and secret is the AccessKey in the developer platform.

Q: What is a schema and how to get it?

Tuya: The channel identifier of the App application, please obtain it based on the package name. For example, your App package name is: com.aa.bb, then your schema is: aabb.

Q: What is the data operation permission in the application scenario?

Tuya: In the application scenario, the device in the app, the developer has the operation permission.

Q: Why does the access interface encounter permission deny?

Tuya:

  1. Check whether the device has been removed;

  2. Whether the device is associated with the current developer clientId.

Concluding remarks

Through the above simple tutorial, I believe you have a general understanding of the capabilities provided by Tuya Cloud OpenAPI, and then you can code to implement Tuya Cloud API and carry out corresponding business development;

For more interfaces, please refer to the API interface list. If you have more customization requirements, please contact Tuya Business.

If you still have questions about the interface call, you can ask technical questions on the Tuya developer platform bill of lading. Before asking the questions, try to provide the interface request parameters.