Integration Guide

Last Updated on : 2022-04-07 10:01:44download

This topic describes the request structure, authentication method, request signatures, and other technical knowledge involved in the use of Tuya commercial lighting APIs to speed up your API calls.

In fact, you can call the commercial lighting APIs in the same way as you call other open APIs of Tuya. You only need to contact your account manager and subscribe to the commercial lighting SaaS services.

Reference

Things to note

  • Subscribe to API services: Make sure that you have subscribed to the required API services before you make API requests.
  • Subscribe to SaaS services: Make sure that you have contacted your account manager and subscribed to the required commercial lighting SaaS services before you make API requests.
  • Certain APIs require the HTTP client to support the GET method that carries the body parameter. Therefore, you must select a suitable HTTP request framework.

Download resources

Download Postman script

Download Java SDK

SDK update log
Edition Update time Update content
1.2.1 April 7, 2022 ProductProperty adds a field dpId.
1.2.0 November 27, 2021 Updated some domain definitions.
1.1.2 November 24, 2021 Fixed the issue of the wrong DeviceApi#getDetail access modifier.
1.1.1 November 18, 2021 Fixed the issue of wrong AlarmListItem attribute type.
1.1 November 5, 2021 Updated the signature algorithm.
1.0 October 10, 2021 Added existing APIs.

Guide of SDK for Java

The SDK based on Apache HttpClient

The SDK is suitable for Java developers who use Apache HttpClient and do not want to introduce additional dependencies. To implement the SDK, Apache HttpClient is used as the HTTP request client, and FastJson is used to handle serialization and deserialization. The latest updated OpenAPI will be provided as soon as possible.

(Recommended) If you use Apache Maven to manage your Java projects, just add the dependencies to the pom.xml file of your project.

<dependency>
   <groupId>com.tuya</groupId>
   <artifactId>lighting-open-api-java-sdk</artifactId>
   <version>{version}</version>
</dependency>

<repositories>
   <repository>
       <id>tuya-maven</id>
       <url>https://maven-other.tuya.com/repository/maven-public/</url>
   </repository>
</repositories>

If you do not download the .jar package from the Maven repository, you need to create a lib folder in the project root directory, put the downloaded lighting-open-api-java-sdk-{version}.jar in the lib folder, and then add the following dependencies to pom.xml. Otherwise, a NoClassDefFoundError exception will be reported.

<dependency>
    <groupId>com.tuya</groupId>
    <artifactId>lighting-open-api-java-sdk</artifactId>
    <version>{version}</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/lighting-open-api-java-sdk-{version}.jar</systemPath>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.68</version>
</dependency>

The procedure is as follows.

public class Main {
    public static void main(String[]args){
        TuyaOpenApiConfiguration tuyaOpenApiConfiguration = new TuyaOpenApiConfiguration();
        tuyaOpenApiConfiguration.setUrl("https://openapi.tuyacn.com");
        tuyaOpenApiConfiguration.setClientId("****rgswael9wqsz****");
        tuyaOpenApiConfiguration.setClientSecret("****d09f429248caab6017554f93****");
        HttpClientUtils.setTuyaOpenApiConfiguration(tuyaOpenApiConfiguration);
        ProjectApi projectApi = new ProjectApi();
        TokenApi tokenApi = new TokenApi();
        BaseResponse<TokenResult> accessToken = tokenApi.getAccessToken(1);
        HttpClientUtils.setTokenResult(accessToken.getResult());
        BaseResponse<PageResult<ProjectListItem>> response = projectApi.page(1, 10, null);
    }
}

All the APIs are shown as follows.

APIs: TokenApi, AccountApi, ProjectApi, ConstructionTaskApi, DeviceApi, AlarmApi, GroupApi, SceneApi, LinkageApi, TemplateApi, and EnergyStaticApi.