Smart PMSTuya PMS Access Guide

Tuya PMS Access Guide

Last Updated on : 2022-03-02 03:46:32download

This topic describes how to access the Tuya PMS with an example, helping you access the Tuya PMS to implement the production process control.

The following information is described:

  • How to access the Tuya PMS from scratch.
  • Complete Postman API documentation.
  • A code demo. You need only to enter the authorization information and account information to complete debugging.

Preparation

Before accessing the Tuya PMS, make sure the following two steps are completed:

  • Preparation 1: Register and log in to the Tuya IoT Platform. After you create a product, you can view the product information and get the authorization information. For more information, see Quick Start.
  • Preparation 2: Register an account in the Tuya PMS. For more information about the operation, see Applying for a tool account.

Description

  • The request address in the example is the address in China. For more information about other addresses, see Request Structure.
  • In this topic, PMS means the Tuya PMS.

Representations

Before accessing the Tuya PMS, see the following General parameters and the explanation. In the text hereafter, General parameter 1 and General parameter 2 are used as the Representations.

Serial number Name Data type Description
General parameter 1 client_id String The access ID and client ID in the authorization information obtained through Preparation 1.
General parameter 2 secret String The Access Secret/Client Secret in the authorization information obtained through Preparation 1.
General parameter 3 username String The user name of the PMS obtained through Preparation 2.
General parameter 4 password String The password of the PMS obtained through Preparation 2.

Access process

The flowchart of the access to the Tuya PMS:
Tuya PMS Access Guide

A complete access process includes the following three steps:

  1. The authentication for the Tuya Cloud Development Platform.
  2. The authentication for the Tuya PMS.
  3. The service request to the Tuya PMS.

Step 1: Authentication for the Tuya Cloud Development Platform

The authentication for the Tuya PMS depends on the authentication for the Tuya Cloud Development Platform. The authentication for the Tuya Cloud Development Platform needs to be completed before the authentication for the Tuya PMS can be implemented.

  • Request type: Get

  • Request address: /v1.0/token? grant_type=1

  • Header parameter: For more information about the parameter, see Request parameters.

    Note:

    • The access_token is not required in this request;
    • For more information about the parameter client_id, see General parameter 1;
    • The calculation method for the parameter sign is: sign = HMAC-SHA256(client_id + t, secret).toUpperCase(). The secret is the General parameter 2. For more information about the signature method, see Signature algorithm for token management operations.
  • Request parameter: none

  • Response parameter: For more information about the response parameter, see Get a token.

  • Sample request:

    curl -H "client_id:e7r9rp3fab1jtlja761b" -H "sign:14ADCB6FEDC822CC2CBA8537770453E90683B9E9C5B96C851AEB78C4F078C739" -H "sign_method:HMAC-SHA256" -H "t:1614851428586" -H "lang:en" "https://openapi.tuyacn.com/v1.0/token? grant_type=1"
    
  • Sample response:

    {
        "result": {
            "access_token": "90987371be17bd35309ea325bd9b****",
            "expire_time": 7200,
            "refresh_token": "374e7ce2a93b590fb9566aff5119****",
            "uid": "ay15224842516504****"
        },
        "success": true,
        "t": 1616551714002
    }
    

The most important parameters in the information returned by the authentication request API are access_token and refresh_token, which will be used subsequently.

Step 2: Authentication for the Tuya PMS

In terms of the authentication for the Tuya PMS, you need to get a token for login in the first place and request a login API with the login token.

Get token for login

For more information about the API, see Get Token for Login.

  • Request type: Post

  • Request address: /v2.0/pms/production/login/token/get

  • Header parameter: For more information about the parameter, see Request parameters.

    Note:

    • The parameter access_token is the access_token of the response parameter in Step 1: Authentication for the Tuya Cloud Development Platform.
    • The calculation method for the parameter sign is slightly different from that in Step 1: Authentication for the Tuya Cloud Development Platform: sign = HMAC-SHA256(client_id + access_token + t, secret).toUpperCase(). The secret is the General parameter 2. For more information about the signature method, see Signature algorithm for service management operations.
    • The other parameters are the same as those in Step 1: Authentication for the Tuya Cloud Development Platform.
  • Request parameter: see Get Token for Login.

    Note: The username is General parameter 3.

  • Response parameter: see Get Token for Login.

  • Sample request:

    curl -H "Content-Type:application/json" -H "client_id:e7r9rp3fab1jtlja761b" -H "sign:6B401BB9CBB6FA3911C8E33F8EA6C0509224B6010C1E39F8AB727C2594FD5766" -H "sign_method:HMAC-SHA256" -H "t:1614914099164" -H "lang:en"  -H "access_token:fa8b752dc54c54e3e53823b4f18f9c76" -X POST --data '{"username":"325"}' "https://openapi.tuyacn.com/v2.0/pms/production/login/token/get"
    
  • Sample response:

    {
        "result": {
            "publicKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCh1mJNULvlxSb+gwQiMeYa6wpVofkueBuCq2SgPxTBcaN1wXCRe84+jWpbCaXcyVh9IOM9/oc5U/KvdemS9ihTxv/hfXS657OE5q7WM4SgSvFC1YEfvQjFCw1kg7w246FCHit/zOmWhCMB52r9FEm/fy8ijvR3f1tMmuz4LQd1bwID****",
            "token": "7e5117fecc8079e7f0d08a3fa088****"
        },
        "success": true,
        "t": 1616551772155
    }
    

The core response parameters of this request are token and publicKey, which will be used to log in to the PMS. The validity period of the token returned by the API operation is only 10 seconds.

Log in to the PMS

For more information about logging in to the PMS API, see Login to the Platform.

  • Request type: Post

  • Request address: /v2.0/pms/production/user/login

  • Header parameter: the same as that in Get token for login in the text above.

  • Request parameter: see Login to the Platform

    Note:

    • The username is the same as that in Get token for login in the text above.
    • The password is encrypted by RSA, and the padding type is RSA_PKCS1_PADDING: password = RSA (formerly password, publicKey).
      • The original password is General parameter 4.
      • The publicKey is the response parameter in Get token for login in the text above.
    • The token parameter is the response parameter in Get token for login in the text above.
  • Response parameter: see Login to the Platform

  • Sample request:

    curl -H "Content-Type:application/json" -H "client_id:e7r9rp3fab1jtlja761b" -H "sign:B3B970C5C3F2B90B98FED85BFDA250CFA66EC7837ADD86CB0FC747BFE0D0F60F" -H "sign_method:HMAC-SHA256" -H "t:1614916710311" -H "lang:en"  -H "access_token:fa8b752dc54c54e3e53823b4f18f9c76" -X POST --data '{"username":"325","password":"eTHqOJFhY8pDClWw1zOpPqEjwdoW2nL+RI93tPCnEd3flv/KI80naWrhx2y4PSyqMLUZxr4ZEEhrL7oE7aCEi/uPHLuo7opvdgEmS2Lyk97lWwPoFXmg2t65LtBGG869b8tQu2/72aqIsuVDkK47u3UKtTEGobb2U58IxW5CTWg=","token": "38af05ec752c24f8bdbe544b1179ad10"}' "https://openapi.tuyacn.com/v2.0/pms/production/user/login"
    
  • Sample response:

    {
        "result": {
            "accessToken": "affd61d250d8ed27de7a0d955a5a****",
            "refreshToken": "9a473a56ea86b82a3ced1f254b24****"
        },
        "success": true,
        "t": 1616551775549
    }
    

Through the response parameters, you can see the response data after you log in to the PMS contains accessToken and refreshToken. These two parameters differ from the response parameter in Step 1: Authentication for the Tuya Cloud Development Platform only in naming style (The name of this response value of this request is in camel case). In subsequent requests, the response value to this request will be used instead of the response value in Step 1: Authentication for the Tuya Cloud Development Platform for API requests.

Step 3: Business request

This section describes how to request service, using SN process information as an example. For more information, see API Overview:

  • Request type: Post

  • Request address: /v1.0/pms/station/sn/flows/get

  • Header parameter: the same as that in Get token for login in the text above.

    Note:

    • access_token needs to use the return parameter accessToken in the Log in to the PMS in the text above.
    • The calculation method for parameter sign is the same as Get token for login in the text above, but the access_token in it needs to be replaced with the return parameter accessToken in Log in to the PMS in the text above.
    • The structure of the request message header of all service APIs is the same.
  • Request parameter: see Query the Process of Serial Number.

  • Response parameter: see Query the Process of Serial Number.

  • Sample request:

    curl -H "Content-Type:application/json" -H "client_id:e7r9rp3fab1jtlja761b" -H "sign:DB52E1A1599CE59FCB5E6970365566BB2E00B8D37759401BFCE255ED06E2D790" -H "sign_method:HMAC-SHA256" -H "t:1614917378432" -H "lang:en"  -H "access_token:c24bb01ee09fc7884f94c761d17722cf" -X POST --data '{"sn":"YZCB04ZZR00001"}' "https://openapi.tuyacn.com/v1.0/pms/station/sn/flows/get"
    
  • Sample response:

    {
        "result": [
            {
                "craftLineCode": "j2***azn",
                "node": "firstTest",
                "nodeDesc": "Flashing authorization",
                "orderCode": "YZC***ZZR",
                "sn": "YZCB04***00001",
                "status": false,
                "time": 1612524780119
            }
        ],
        "success": true,
        "t": 1616551882179
    }
    

Step 4: (Optional) Refresh token

The validity period of the accessToken is two hours. After the expiration, you can refresh the token by refreshing the token API. The token can be refreshed only once a day.

  • Request type: Get
  • Request address: /v1.0/token/{refreshToken}
  • Header parameter: the same as that in Get token for login in the text above.
  • Request parameter: none
  • Response parameter: see Refresh token
  • Sample request:
    curl -H "client_id:e7r9rp3fab1jtlja761b" -H "sign:8480E421DD11E1ED972A6A121A4B78B4712D469859A204C366880DBF6D6D0081" -H "sign_method:HMAC-SHA256" -H "t:1614933327219" -H "lang:en" "https://openapi.tuyacn.com/v1.0/token/d415c1ecc7d742c5e1ca75634b75b73e"
    
  • Sample response:
    {
        "result": {
            "access_token": "0c0e099651b1060ffafb9bfdf419****",
            "expire_time": 7200,
            "refresh_token": "169fca7561b8e5d393517e961df2****",
            "uid": "bay15911e11580224Q8AWQx34f48f89fca11ee502a445280-000****"
        },
        "success": true,
        "t": 1616551974215
    }
    

Use Postman authentication

Comprehensive Postman documentation is provided. You need only to complete the send request of the first three APIs as follows to complete the API authentication.

Note: The interval between getting the login token and logging in to the PMS for send operation shall be no more than 10 seconds.

For more information about how the parameter is filled in the API, see Pre-request Script and Tests scripts of each API.

Tuya PMS Access Guide

Install and configure Postman

  1. Install Postman 7.12.0 or later version.
  2. Click Import on the upper left corner to import the API package of the Cloud Smart Manufacture.
    Tuya PMS Access Guide
  3. Click Manage Environments on the top right corner on the page to import the API environment package of the Cloud Smart Manufacture.
    Tuya PMS Access Guide
  4. Modify the environment variables to the authorization and account information you applied for. Tuya PMS Access Guide

Code demo

An access demo of the Java version is provided for easy access. Click Download.
Method entry: com.tuya.pms.openapi.util.PmsOpenApiHelper.

FAQ

Problem: How can I solve the issue if an invalid token is returned after logging in to the PMS API?
Possible cause: The parameter entering takes more than 10 seconds.
Solution: The valid period for a token is only 10 seconds. We recommend that you use the program or Postman for debugging.